图片上传jQuery插件(兼容IE8)
 
html
 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传图片</title>
<link rel="stylesheet" href="uploadImg.css" />
</head>
<body>
<!--上传图片容器 start-->
<div class="picBox"></div>
<!--上传图片容器 end--> <script src="http://apps.bdimg.com/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script src="uploadImg.js" type="text/javascript"></script>
<script type="text/javascript">
$(".picBox").uploadImg({
"picNum": 1,//上传图片张数
"width": 200,//图片宽度
"height": 100//图片高度
});
</script>
</body>
</html>

css

 .fileInput{
display: block;
height:1px;
overflow: hidden;
opacity: 0;
filter:alpha(opacity=0);
}
.previewBox{
margin:15px 0;
}
.previewBox img{
margin-right:10px;
}
.uploadBtn{
border:1px solid #eee;
background: #fff;
color:#666;
font-size: 16px;
line-height: 1.6;
padding:6px 15px;
}

js

 /*
*上传图片并本地预览插件
*兼容IE8
*obj ----上传图片容器
*picNum ----上传图片的张数
*width ----图片宽度
*height ----图片宽度
*/
(function($) {
// 构造函数
function UploadImg(obj,opt) {
debugger;
this.obj = obj;
this.$obj = $(obj);
this.defaultOpt = {
"picNum": 1,
"width": 100,
"height": 100
};
this.num = 0;
this.options = $.extend({},this.defaultOpt,opt);
this.init();
};
// 初始化html
UploadImg.prototype.init = function() {
var html = '<input type="file" class="fileInput">'
+'<div><button type="button" class="uploadBtn">上传图片</button></div>'
+'<div class="previewBox"></div>';
this.$obj.append($(html));
this.bindEvent();
}
//绑定事件
UploadImg.prototype.bindEvent = function() {
var self = this;
this.$obj.on("click.choose",".uploadBtn",function() {
$(".fileInput").trigger("click");
});
$(".fileInput").on("change.upload",function() {
self.operationImg(this);
});
this.$obj.off(".choose,.upload");
}
//检查图片格式
UploadImg.prototype.isImg = function(url) {
var result = /.+\.(jpg|png|jpeg|gif)$/.test(url);
if(!result) {
alert("您选择的图片格式有误,请重新选择");
return false;
} else {
return true;
}
},
//添加预览图片到页面上
UploadImg.prototype.addImgHtml = function(url) {
if(this.options.picNum == 1) {
if($(".previewBox")) {
$(".previewBox").html("<img src="+ url +" width="+ this.options.width +" height="+ this.options.height +">");
}
} else {
if($(".previewBox") && this.num < this.options.picNum) {
$(".previewBox").append("<img src="+ url +" width="+ this.options.width +" height="+ this.options.height +">");
this.num++;
}
}
},
//兼容IE处理
UploadImg.prototype.previewImgIE = function(obj) {
obj.select();
$(obj).blur();
if(document.selection) {
var url = document.selection.createRange().text;
if(this.isImg(url)) {
var imgWrap = "<div class='imgWrap'></div>";
if(this.options.picNum == 1) {
$(".previewBox").html($(imgWrap));
} else if(this.options.picNum > 1 && this.num < this.options.picNum) {
$(".previewBox").append($(imgWrap));
this.num++;
} else {
return;
};
$(".imgWrap").css({
"width":this.options.width,
"height":this.options.height,
"display":"inline-block",
"margin-right":"10px",
"*display":"inline",
"*zoom":1
});
$(".imgWrap:last").css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale,src=\""+url+"\")");
}
}
},
//正常处理
UploadImg.prototype.previewImg = function(obj) {
var file = obj.files[0];
var self = this;
if(this.isImg(file.name)) {
var reader = new FileReader();
reader.onload = function(evt) {
self.addImgHtml(evt.target.result);
};
reader.readAsDataURL(file);
} else {
alert("您输入的图片格式有误,请重新输入");
return false;
}
};
//上传图片操作;
UploadImg.prototype.operationImg = function(fileObj) {
if(fileObj.files && fileObj.files[0]) {
//html5 files API
this.previewImg(fileObj);
} else {
//兼容IE
this.previewImgIE(fileObj);
}
}
//绑定插件
$.fn.uploadImg= function(options) {
return this.each(function() {
new UploadImg(this,options);
});
}
})(jQuery);

图片上传jQuery插件(兼容IE8)的更多相关文章

  1. MVC4中基于bootstrap和HTML5的图片上传Jquery自定义控件

    场景:mvc4中上传图片,批量上传,上传前浏览,操作.图片进度条. 解决:自定义jquery控件 没有解决:非图片上传时,会有浏览样式的问题; 解决方案; 1.样式 – bootstrap 的css和 ...

  2. jquery.attach附件上传jquery插件

    html: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv=&qu ...

  3. 图片上传JS插件梳理与学习

    项目的oper端和seller端,用了两个不同插件,简单了解一下: 一.seller端:AjaxUpload.js seller端使用的是 AjaxUpload.js ,封装好的一个库.调用时传入参数 ...

  4. 适应各浏览器图片裁剪无刷新上传jQuery插件(转)

    看到一篇兼容性很强的图片无刷新裁剪上传的帖子,感觉很棒.分享下!~ 废话不多说,上效果图. 一.首先建立如下的一个page <!DOCTYPE html> <html xmlns=& ...

  5. 另一种图片上传 jquery.fileupload.js

    今天遇到另外一种上传图片方法 用jquery.fileupload.js <input type="file" name="file[]" multipl ...

  6. 头像截取 图片上传 js插件

    先看一下整体效果 页面html <div class="row"> <div class="tabs-container"> <u ...

  7. 阿里云OSS图片上传plupload.js结合jq-weui 图片上传的插件

    项目中用到了oss上传,用的plupload,奈何样式上不敢恭维,特别是放在移动端上使用.于是自己把它移植到了jq weui的上传图片组件上. 更改:选择照片后确认即及时上传至oss服务器,不限制上传 ...

  8. uploadify插件实现多个图片上传并预览

    使用uploadify插件可方便实现图片上传功能.兼容ie6.ie7. 上传成功之后使用插件的回调函数读取json数据,根据url实现图片预览. 效果图: 点击浏览文件上传图片,图片依次在右侧显示预览 ...

  9. 图片上传,支持同步/异步、预览(MVC、uploadify异步提交、js预览、ajaxSubmit异步提交)兼容大部分浏览器,含代码

    图片上传代码,支持同步/异步和图片的预览 主要用了两种方式,可兼容大部分浏览器. 第一种使用uploadify异步上传,上传后返回图片路径显示到页面. 每二种使用ajaxSubmit异步上传,为兼容I ...

随机推荐

  1. spring boot与spring mvc的区别

    Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向横切面 ...

  2. BootStrap使用

    BootStrap简单使用 <深入理解BootStrap>这本书对BootStrap进行了全面的讲解包括设计思想以及源码解析对没有接触过的很有帮助 BootStrap可以说是最简单的一类框 ...

  3. L198

    One of the most common birth defects throughout the world is a cleft lip. Babies born with a cleft l ...

  4. Chrome浏览器中使用 iframe 嵌入网页导致视频不能全屏的问题解决方法

    今天无意中测试了下在 iframe 中嵌入视频, 发现全屏按钮在 Chrome 浏览器中居然无效, 试了好几个视频网站的视频都不能全屏, 但在其他浏览器中似乎都很正常, 应该是 Chrome 60 新 ...

  5. [置顶] Android 打包apk无敌报错

    前言: 这个问题从昨天上午一直到现在,请教了很多大佬,都没有给出确定的解决方案,可能他们也没碰到过可能,不过还是挺感谢他们的建议,一直到今天中午午休,我一直都在想这个问题,就是下面的这个,看了国内很多 ...

  6. Swift 标签控制器(tabbar添加提醒和控制器)

    // Override point for customization after application launch. //初始化window, 大小为设备物理大小 self.window = U ...

  7. cousera 深度学习 吴恩达 第一课 第二周 学习率对优化结果的影响

    本文代码实验地址: https://github.com/guojun007/logistic_regression_learning_rate cousera 上的作业是 编写一个 logistic ...

  8. Spring Model存储值在jsp EL表达式中不能正确显示(原样显示)问题

    这几天我搭了一个SpringMvc环境,写了一个Controller,并且Controller里面有一个很简单的映射到jsp页面的方法,如下: 这里的Map<String,String>其 ...

  9. jquery类的创建方式及关键字new的原理

    一.由jQuery创建类引发的问题 在用jQuery选择器时候,可以通过下面两种方式获取元素,并得到一个jQuery对象. var d1 = jQuery('#demo01'); var d2 = n ...

  10. 【转】VC++10(VS2010)IDE各种使用技巧

    原文网址:http://www.cnblogs.com/sunrisezhang/articles/2802397.html 一个好的coder,他首先必须是一个熟练工.对于C++程序员来说,只有掌握 ...