js上傳圖片預(yù)覽的實(shí)現(xiàn)方法
更新時間:2017年05月09日 15:13:23 作者:猛男史86
這篇文章主要為大家詳細(xì)介紹了js上傳圖片預(yù)覽的實(shí)現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js上傳圖片預(yù)覽的方法,供大家參考,具體內(nèi)容如下
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>圖片上傳本地預(yù)覽</title>
<style type="text/css">
#preview{width:260px;height:190px;border:1px solid #000;overflow:hidden;}
#imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);}
</style>
<script type="text/javascript">
//圖片上傳預(yù)覽 IE是用了濾鏡。
function previewImage(file)
{
var MAXWIDTH = 260;
var MAXHEIGHT = 180;
var div = document.getElementById('preview');
if (file.files && file.files[0])
{
div.innerHTML ='<img id=imghead>';
var img = document.getElementById('imghead');
img.onload = function(){
var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
img.width = rect.width;
img.height = rect.height;
// img.style.marginLeft = rect.left+'px';
img.style.marginTop = rect.top+'px';
}
var reader = new FileReader();
reader.onload = function(evt){img.src = evt.target.result;}
reader.readAsDataURL(file.files[0]);
}
else //兼容IE
{
var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
file.select();
var src = document.selection.createRange().text;
div.innerHTML = '<img id=imghead>';
var img = document.getElementById('imghead');
img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"'></div>";
}
}
function clacImgZoomParam( maxWidth, maxHeight, width, height ){
var param = {top:0, left:0, width:width, height:height};
if( width>maxWidth || height>maxHeight )
{
rateWidth = width / maxWidth;
rateHeight = height / maxHeight;
if( rateWidth > rateHeight )
{
param.width = maxWidth;
param.height = Math.round(height / rateWidth);
}else
{
param.width = Math.round(width / rateHeight);
param.height = maxHeight;
}
}
param.left = Math.round((maxWidth - param.width) / 2);
param.top = Math.round((maxHeight - param.height) / 2);
return param;
}
</script>
</head>
<body>
<div id="preview">
<img id="imghead" width=100 height=100 border=0 src='<%=request.getContextPath()%>/images/defaul.jpg'>
</div>
<input type="file" onchange="previewImage(this)" />
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- javascript HTML5文件上傳FileReader API
- 借助FileReader實(shí)現(xiàn)將文件編碼為Base64后通過AJAX上傳
- 談?wù)劵趇frame、FormData、FileReader三種無刷新上傳文件的方法
- File, FileReader 和 Ajax 文件上傳實(shí)例分析(php)
- node.js讀取文件到字符串的方法
- node.js實(shí)現(xiàn)逐行讀取文件內(nèi)容的代碼
- PHP 讀取文件內(nèi)容代碼(txt,js等)
- JSP讀取文件實(shí)例
- JavaScript使用readAsDataUrl方法預(yù)覽圖片
- JavaScript通過filereader接口讀取文件
相關(guān)文章
A標(biāo)簽觸發(fā)onclick事件而不跳轉(zhuǎn)的多種解決方法
一個標(biāo)簽僅僅是要觸發(fā)onclick行為,遇到了A標(biāo)簽觸發(fā)onclick事件時不執(zhí)行跳轉(zhuǎn),下面與大家分享下四種解決方法,感興趣的朋友可以參考下哈2013-06-06
淺談Javascript數(shù)據(jù)屬性與訪問器屬性
下面小編就為大家?guī)硪黄獪\談Javascript數(shù)據(jù)屬性與訪問器屬性。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07
JS實(shí)現(xiàn)超簡單的漢字轉(zhuǎn)拼音功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)超簡單的漢字轉(zhuǎn)拼音功能,結(jié)合實(shí)例形式分析了javascript漢字轉(zhuǎn)換成拼音的函數(shù)定義與使用技巧,需要的朋友可以參考下2016-12-12
webpack?output.library的16?種取值方法示例
這篇文章主要為大家介紹了webpack?output.library的16?種取值方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
基于JavaScript實(shí)現(xiàn)交互式博客
在Web開發(fā)中,JavaScript(JS)是一種至關(guān)重要的編程語言,它使網(wǎng)頁具有交互性,本文主要介紹了如何使用JavaScript實(shí)現(xiàn)一個交互式博客,需要的可以了解下2024-01-01
uniapp組件傳值的方法(父傳子,子傳父,對象傳值)實(shí)戰(zhàn)案例
現(xiàn)在的前端開發(fā)中基本上都是組件化開發(fā)的,下面這篇文章主要給大家介紹了關(guān)于uniapp組件傳值(父傳子,子傳父,對象傳值)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03

