var Move_fn = {};
(function(Move_fn){
function Move_img() {
}
Move_img.prototype = {
constructor:Move_img,
pageInit: function(imgEle, imgContent) {
this.Ele = imgEle;
this.Box = imgContent;
imgEle.className = "_positon";//添加定位屬性便于計算拖拽位置
this._mw = imgContent.offsetWidth - imgEle.offsetWidth;
this._mh = imgContent.offsetHeight - imgEle.offsetHeight;
this.mouseDown();
this.closeEvt();
},
closeEvt:function() {
var that = this;
this.Box.onclick = function(e) {
e.preventDefault();
e.stopPropagation();
if(e.target.tagName == "DIV" || e.srcElement.tagName == "div") {
Elf.utils.remove(that.Box.parentNode, that.Box.parentNode.parentNode);
}
}
},
mouseDown: function() {
var that = this;
this.Ele.onmousedown = function(e) {
that.offX = e.offsetX;
that.offY = e.offsetY;
that.mouseMove();
}
},
mouseMove: function(){
var that = this;
document.onmousemove = function(e) {
var l = e.clientX - that.offX;
var t = e.clientY - that.offY;
//判斷邊界設置最大最小值
if(t <= 0) {
t = 0;
}
if(t >= that._mh) {
t = that._mh;
}
if(l <= 0) {
l = 0;
}
if(l >= that._mw) {
l = that._mw;
}
that.Ele.style.top = t + "px";
that.Ele.style.left = l + "px";
that.mouseUp();
}
},
mouseUp: function() {
var that = this;
document.onmouseup = function(e) {
document.onmousemove = null;
document.onmousedown = null;
}
}
}
Move_fn.move_img = new Move_img();
}(Move_fn));
Move_fn.move_img.pageInit(imgShow, imgContent);
初始化一下,要求imgContent全屏遮蓋
禁止掉圖片自己的拖拽事件,對要拖拽的圖片添加幾個屬性
- oncontextmenu:"false" 禁止圖片右鍵菜單彈出
- onselectstart:"false" 禁止圖片選中
- ondragstart:"false" 禁止圖片拖拽
- draggable:"false" 禁止圖片拖拽