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("請選擇一張圖片");
}
}
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>
以上就是本文的全部內(nèi)容,希望對大家學(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ù)覽問題
- JS實(shí)現(xiàn)上傳圖片的三種方法并實(shí)現(xiàn)預(yù)覽圖片功能
相關(guān)文章
實(shí)現(xiàn)圖片預(yù)加載的三大方法及優(yōu)缺點(diǎn)分析
本文介紹了實(shí)現(xiàn)圖片預(yù)加載的三大方法(CSS/JAVASCRIPT/AJAX)以及這三種方法的優(yōu)缺點(diǎn)分析,是篇非常值得推薦的文章,小伙伴們可要仔細(xì)研究下。2014-11-11
bootstrap常用組件之頭部導(dǎo)航實(shí)現(xiàn)代碼
這篇文章主要介紹了bootstrap常用組件之頭部導(dǎo)航實(shí)現(xiàn)代碼,然后對個(gè)別常用屬性進(jìn)行了解釋,需要的的朋友參考下吧2017-04-04
doctype后如何獲得body.clientHeight的方法
doctype后如何獲得body.clientHeight的方法...2007-07-07
微信小程序獲取手機(jī)網(wǎng)絡(luò)狀態(tài)的方法【附源碼下載】
這篇文章主要介紹了微信小程序獲取手機(jī)網(wǎng)絡(luò)狀態(tài)的方法,涉及微信小程序wx.getNetworkType函數(shù)檢查網(wǎng)絡(luò)連接狀態(tài)的相關(guān)使用技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12
JavaScript實(shí)現(xiàn)打印星型金字塔功能實(shí)例分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)打印星型金字塔功能,結(jié)合具體實(shí)例形式分析了javascript針對輸出任意給定行數(shù)星型金字塔圖形的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
js實(shí)現(xiàn)密碼強(qiáng)度檢驗(yàn)
這篇文章主要為大家詳細(xì)介紹了js密碼強(qiáng)度檢驗(yàn)的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

