js上传图片
程序员文章站
2022-03-12 16:13:20
鼠标悬浮时显示大图 ......
function dragimgupload(id,options) {
this.me = $(id);
var defaultopt = {
boxwidth:'135px',
boxheight:'135px'
}
this.preview = $('#preview');
this.reset=$('<div id="delete-img" style="">×</div>')
this.preview.append(this.reset);
this.opts=$.extend(true, defaultopt,{
}, options);
this.init();
this.callback = this.opts.callback;
}
//定义原型方法
dragimgupload.prototype = {
init:function () {
this.me.append(this.preview);
this.me.append(this.fileupload);
this.eventclickinit();
this.resetimage();
this.cssinit();
},
cssinit:function () {
this.me.css({
'width':this.opts.boxwidth,
'height':this.opts.boxheight,
'border':'1px solid #bbb',
'display': 'inline-block',
'cursor':'pointer',
'position': 'relative'
})
this.preview.css({
'height':'100%',
'overflow':'hidden',
})
this.reset.css({
'line-height': '20px',
'z-index': '999',
'background': 'rgba(0,0,0,.4)',
'color': '#fff',
'position': 'absolute',
'top': '0',
'right': '0',
'height': '25px',
'width': '30px',
'border-bottom-left-radius':'92%',
'text-align': 'center',
'font-size':'20px',
'display':'none'
})
},
ondragover:function (e) {
e.stoppropagation();
e.preventdefault();
e.datatransfer.dropeffect = 'copy';
},
ondrop:function (e) {
var self = this;
e.stoppropagation();
e.preventdefault();
var filelist = e.datatransfer.files;
if(filelist.length == 0){
return false;
}
//检测文件是不是图片
if(filelist[0].type.indexof('image') === -1){
alert("您拖的不是图片!");
return false;
}
//拖拉图片到浏览器,可以实现预览功能
var img = window.url.createobjecturl(filelist[0]);
var filename = filelist[0].name; //图片名称
var filesize = math.floor((filelist[0].size)/1024);
if(filesize>500){
alert("上传大小不能超过500k.");
return false;
}
self.me.find("img").attr("src",img);
self.me.find("img").attr("title",filename);
if(this.callback){
this.callback(filelist);
}
},
eventclickinit:function () {
var self = this;
self.reset.css({
'display':'block'
})
this.me.unbind().click(function () {
self.createimageuploaddialog();
})
var dp = this.me[0];
dp.addeventlistener('dragover', function(e) {
self.ondragover(e);
});
dp.addeventlistener("drop", function(e) {
self.ondrop(e);
});
},
onchangeuploadfile:function () {
var self = this;
var fileinput = this.fileinput;
var files = fileinput.files;
var file = files[0];
var img = window.url.createobjecturl(file);
var filename = file.name;
this.me.find("img").attr("src",img);
this.me.find("img").attr("title",filename);
self.reset.css({
'display':'block'
})
if(this.callback){
this.callback(files);
}
},
createimageuploaddialog:function () {
var self = this;
var fileinput = this.fileinput;
if (!fileinput) {
//创建临时input元素
fileinput = document.getelementbyid('file');
// fileinput = document.createelement('input');
//设置input type为文件类型
// fileinput.type = 'file';
//设置文件name
// fileinput.name = 'file';
//允许上传多个文件
fileinput.multiple = true;
fileinput.onchange = this.onchangeuploadfile.bind(this);
this.fileinput = fileinput;
self.reset.css({
'display':'block'
})
}
//触发点击input点击事件,弹出选择文件对话框
fileinput.click();
},
resetimage:function(){
var self = this;
this.reset.click(function(e){
e.stoppropagation();
var imgreset=self.me.find("img")[0];
console.log('图片',imgreset.src);
imgreset.src='/alink-hq/static/img/upload.png';
self.reset.css({
'display':'none'
})
})
}
}
鼠标悬浮时显示大图
$(function(){
var dragimgupload = new dragimgupload("#drop_area", {
callback: function (files) {
//回调函数,可以传递给后台等等
var file = files[0];
console.log(file.name);
}
});
$('#drop_area').on({
mouseover: function () {
var imgurl = $(this).find('img')[0].src;
$('#img-max').attr('src', imgurl);
},
mouseout: function () {
var imgurl = $(this).find('img')[0].src;
$('#img-max').attr('src', '');
}
});
dragimgupload.resetimage();
})