首頁 > web前端 > js教程 > 主體

jquery實作相容瀏覽器的圖片上傳本機預覽功能_jquery

WBOY
發布: 2016-05-16 17:20:00
原創
1228 人瀏覽過
一、圖片上傳實現本地預覽

由於上傳圖片功能,現在大多數都需要在本地實現預覽,為了能夠更好的讓用戶體驗到效果,實現成品的證明,需要相容好幾種瀏覽器,所有透過各個例子整合了這個範例插件,相容於火狐、Google、ie8,其他的沒有進行測試過
複製程式碼 程式碼如下:

(function($){
jQuery.fn.extend({
uploadPreview: function(opts){
opts = jQuery.extend({
width: 0,
height: 0,
imgPreview: null,
imgType: ["gif", "jpeg", "jpg", "bmp", "png "],
callback: function(){ return false; }
}, opts || {});

var _self = this;
var _this = $(this);
var imgPreview = $(opts.imgPreview);
//設定樣式
autoScaling = function(){
imgPreview.css({"margin-left": 0,"margin-top" : 0,"width":opts.width,"height":opts.height});
imgPreview.show();
}
//file按鈕出發事件
_this.change( function(){
if (this.value) {
if (!RegExp(".(" opts.imgType.join("|") ")$", "i").test(this. value.toLowerCase())) {
alert("圖片類型必須是" opts.imgType.join(",") "中的一種");
this.value = "";
return false;
}
if ($.browser.msie) {//判斷ie
var path = $(this).val();
if (/"wW"/.test (path)) {
path = path.slice(1,-1);
}
imgPreview.attr("src",path);
imgPreview.css({"margin-left ": 0,"margin-top": 0,"width":opts.width,"height":opts.height});
setTimeout("autoScaling()", 100);
}
else {
if ($.browser.version imgPreview.attr('src', this.files.item(0).getAsDataURL());
}
else {
oFReader = new FileReader(), rFilter = /^(?:image/bmp|image/cis-cod|image/gif|image/ief|image/jpeg|image/jpeg|image/jpeg|image/ pipeg|image/png|image/svg xml|image/tiff|image/x-cmu-raster|image/x-cmx|image/x-icon|image/x-portable-anymap|image/x-portable-bitmap |image/x-portable-graymap|image/x-portable-pixmap|image/x-rgb|image/x-xbitmap|image/x-xpixmap|image/x-xwindowdump)$/i;
oFReader. onload = function(oFREvent){
imgPreview.attr('src', oFREvent.target.result);
};
var oFile = this.files[0];
oFReader.readAsDataURL( oFile);
}
imgPreview.css({"margin-left": 0,"margin-top": 0,"width":opts.width,"height":opts.height});
setTimeout("autoScaling()", 100);
}
}
opts.callback();
});
}
});
} )(jQuery);

二、調用方法
複製代碼程式碼如下:

jQuery(function(){
jQuery("#idFile1").uploadPreview({
width: 100,
height: 100, 100,view> : "#idImg1",
imgType: ["bmp", "gif", "png", "jpg"],
callback: function() {
ip1();
return false ;
}
});
);

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!