标签:str fse 定义 sem pre get mouse tor 函数
ECMA5:
function Drag(id){
this.ele = document.getElementById(id);
var that = this;
this.ele.onmousedown = function(evt){
that.fnDown(evt);
}
this.fnDown = function(evt){
var e = evt || window.event;
this.disX = e.offsetX;
this.disY = e.offsetY;
var that = this;
document.onmousemove = function(evt){
that.fnMove(evt);
}
document.onmouseup = this.fnUp;
}
this.fnMove = function(evt){
var e = evt || window.event;
this.ele.style.left = e.pageX - this.disX + ‘px‘;
this.ele.style.top = e.pageY - this.disY + ‘px‘;
}
this.fnUp = function(){
document.onmousemove = null;
}
}
ECMA6:
class Drag{
constructor(id){
//属性
this.ele = document.getElementById(id);
var that = this;
this.ele.onmousedown = function(evt){
that.fnDown(evt);
}
}
fnDown(evt){
var e = evt || window.event;
this.disX = e.offsetX;
this.disY = e.offsetY;
var that = this;
document.onmousemove = function(evt){
that.fnMove(evt);
}
document.onmouseup = this.fnUp;
}
fnMove(evt){
var e = evt || window.event;
this.ele.style.left = e.pageX - this.disX + ‘px‘;
this.ele.style.top = e.pageY - this.disY + ‘px‘;
}
fnUp(){
document.onmousemove = null;
}
}
标签:str fse 定义 sem pre get mouse tor 函数
原文地址:https://www.cnblogs.com/zhongchao666/p/9275566.html