图片上传jQuery插件(兼容IE8)
<!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)的更多相关文章
- MVC4中基于bootstrap和HTML5的图片上传Jquery自定义控件
场景:mvc4中上传图片,批量上传,上传前浏览,操作.图片进度条. 解决:自定义jquery控件 没有解决:非图片上传时,会有浏览样式的问题; 解决方案; 1.样式 – bootstrap 的css和 ...
- jquery.attach附件上传jquery插件
html: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv=&qu ...
- 图片上传JS插件梳理与学习
项目的oper端和seller端,用了两个不同插件,简单了解一下: 一.seller端:AjaxUpload.js seller端使用的是 AjaxUpload.js ,封装好的一个库.调用时传入参数 ...
- 适应各浏览器图片裁剪无刷新上传jQuery插件(转)
看到一篇兼容性很强的图片无刷新裁剪上传的帖子,感觉很棒.分享下!~ 废话不多说,上效果图. 一.首先建立如下的一个page <!DOCTYPE html> <html xmlns=& ...
- 另一种图片上传 jquery.fileupload.js
今天遇到另外一种上传图片方法 用jquery.fileupload.js <input type="file" name="file[]" multipl ...
- 头像截取 图片上传 js插件
先看一下整体效果 页面html <div class="row"> <div class="tabs-container"> <u ...
- 阿里云OSS图片上传plupload.js结合jq-weui 图片上传的插件
项目中用到了oss上传,用的plupload,奈何样式上不敢恭维,特别是放在移动端上使用.于是自己把它移植到了jq weui的上传图片组件上. 更改:选择照片后确认即及时上传至oss服务器,不限制上传 ...
- uploadify插件实现多个图片上传并预览
使用uploadify插件可方便实现图片上传功能.兼容ie6.ie7. 上传成功之后使用插件的回调函数读取json数据,根据url实现图片预览. 效果图: 点击浏览文件上传图片,图片依次在右侧显示预览 ...
- 图片上传,支持同步/异步、预览(MVC、uploadify异步提交、js预览、ajaxSubmit异步提交)兼容大部分浏览器,含代码
图片上传代码,支持同步/异步和图片的预览 主要用了两种方式,可兼容大部分浏览器. 第一种使用uploadify异步上传,上传后返回图片路径显示到页面. 每二种使用ajaxSubmit异步上传,为兼容I ...
随机推荐
- Sizzle源码分析:二 词法分析
上一篇我们了解了Sizzle的整体流程,下面我开始一点点分析各个流程,我们进行查询的第一步就是词法分析tokenize,同样先了解下思路,如果是#div_test > span input[ch ...
- 函数响应式编程RxJava
RxJava 到底是什么 一个词:异步. RxJava 在 GitHub 主页上的自我介绍是 "a library for composing asynchronous and event- ...
- js固定表头的实现(转)
原文链接:http://www.th7.cn/web/js/201509/121055.shtml 参考链接:http://www.jb51.net/article/102568.htm 写两个表格, ...
- linux下部署tomcat服务器之安装jdk
如果一开始安装过jdk的可以卸载 rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.50.1.11.5.el6_3.x8 ...
- Linux:LNMP架构的搭建
LNMP架构的搭建 centos6.8-i686 MySQL PHP Nginx 搭建前先安装一些必要的rpm和php组件(全新系统) yum install -y wget gcc vim* lib ...
- [UOJ46][清华集训2014]玄学
uoj description 给出\(n\)个变换,第\(i\)个变换是将区间中\(l_i,r_i\)的数\(x\)变成\((a_ix+b_i)\mod m\). 每次会新增一个变换,或者查询询问如 ...
- [LOJ535]「LibreOJ Round #6」花火
loj description 给你一个排列\(h_i\),你需要交换任意两个位置上的数使得交换后排列的逆序对数最少. \(n \le 3\times 10^5\) sol 首先可以发现,如果交换两个 ...
- LG4777 【模板】扩展中国剩余定理(EXCRT)
题意 题目描述 给定\(n\)组非负整数\(a_i, b_i\),求解关于\(x\)的方程组 \[\begin{cases} x \equiv b_1\ ({\rm mod}\ a_1) \\ x\e ...
- bzoj2004公交线路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 好美妙的矩阵乘. 思考: 0.在一个序列上.所以考虑dp. 1.p<=10,k& ...
- bzoj4576 [Usaco2016 Open]262144
题目大意: 给出n个数a[1..n],n<=262144,a[i]<=40,相邻且相同的数可以合并成一个并将值加1,问能获得的最大数是多少 用一个双向链表维护原数列,每个节点记录此节点对应 ...