js實(shí)現(xiàn)上傳圖片及時(shí)預(yù)覽
本文實(shí)例為大家分享了javascript圖片預(yù)覽功能實(shí)現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下
先為大家分享一段關(guān)于js圖片預(yù)覽的代碼,兼容火狐和谷歌瀏覽器
/* 案例展示圖片預(yù)覽 */ $(function(){ $("#file0").bind("change",function(){ clickupLoad(); }); }); function clickupLoad(){ var imgObject = document.getElementById('file0'); var getImageSrc = getFullPath(imgObject); // 本地路徑 var srcs = window.URL.createObjectURL(imgObject.files[0]); var pos = getImageSrc.lastIndexOf("."); var lastname = getImageSrc.substring(pos, getImageSrc.length) // 圖片后綴] if(srcs!=""){ $("#yulan2").attr("src",srcs); }else{ alert("請(qǐng)選擇一張圖片"); } } function getFullPath(obj) { //得到圖片的完整路徑 if (obj) { if (window.navigator.userAgent.indexOf("MSIE") >= 1){ obj.select(); return document.selection.createRange().text; }else if(window.navigator.userAgent.indexOf("Firefox") >= 1) { if (obj.files) { return window.URL.createObjectURL(obj.files[0]); } return obj.value; } return obj.value; } }
實(shí)例代碼實(shí)現(xiàn)js上傳圖片及時(shí)預(yù)覽:
<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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
- JS HTML5拖拽上傳圖片預(yù)覽
- javascript實(shí)現(xiàn)input file上傳圖片預(yù)覽效果
- js實(shí)現(xiàn)上傳圖片預(yù)覽的方法
- 上傳圖片預(yù)覽JS腳本 Input file圖片預(yù)覽的實(shí)現(xiàn)示例
- javascript上傳圖片前預(yù)覽圖片兼容大多數(shù)瀏覽器
- js實(shí)現(xiàn)上傳圖片之上傳前預(yù)覽圖片
- javascript實(shí)現(xiàn)上傳圖片并預(yù)覽的效果實(shí)現(xiàn)代碼
- js 上傳圖片預(yù)覽問(wèn)題
- JS實(shí)現(xiàn)上傳圖片的三種方法并實(shí)現(xiàn)預(yù)覽圖片功能
相關(guān)文章
javascript數(shù)據(jù)類型驗(yàn)證方法
這篇文章主要為大家分享了一個(gè)簡(jiǎn)單的javascript數(shù)據(jù)類型驗(yàn)證方法,感興趣的小伙伴們可以參考一下2015-12-12JavaScript使用鍵盤(pán)輸入控制實(shí)現(xiàn)數(shù)字驗(yàn)證功能
根據(jù)鍵盤(pán)輸入的keycode來(lái)判斷輸入的是什么類型來(lái)實(shí)現(xiàn)數(shù)字驗(yàn)證功能,就簡(jiǎn)單幾行代碼就可以實(shí)現(xiàn),對(duì)js數(shù)字驗(yàn)證功能感興趣的朋友一起看下吧2016-08-08微信小程序中子頁(yè)面向父頁(yè)面?zhèn)髦祵?shí)例詳解
這篇文章主要介紹了微信小程序中子頁(yè)面向父頁(yè)面?zhèn)髦祵?shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03JavaScript實(shí)現(xiàn)京東購(gòu)物放大鏡和選項(xiàng)卡效果的方法分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)京東購(gòu)物放大鏡和選項(xiàng)卡效果的方法,結(jié)合實(shí)例形式分析了javascript基于事件響應(yīng)、數(shù)值計(jì)算與頁(yè)面元素動(dòng)態(tài)修改實(shí)現(xiàn)圖片放大功能以及tab選項(xiàng)卡切換效果相關(guān)操作技巧,需要的朋友可以參考下2018-07-07javascript中的toFixed固定小數(shù)位數(shù) 簡(jiǎn)單實(shí)例分享
這篇文章介紹了toFixed固定小數(shù)位數(shù)的簡(jiǎn)單例子,有需要的朋友可以參考一下2013-07-07無(wú)循環(huán) JavaScript(map、reduce、filter和find)
本文由淺入深地介紹了map、reduce、filter和find函數(shù),如何一步一步把循環(huán)從代碼中抽離掉2017-04-04