计算从el元素左上角至鼠标单击元素的坐标的距离,x偏移,y偏移
初步封装
// var dragNclick = function (a, b) {
// console.log(a, b);
// }; //不能写()
// dragNclick(1, 2); //这是一个模块
//拖拽 封装好 以后直接调用就行了
//初步封装 把上节课的封装好
var dragNclick = (function (elem) {
drag();
function drag() {
var x, y;
addEvent(elem, "mousedown", function (e) {
// console.log('1');
var e = e || window.event;
// console.log(elem);
x = pagePos(e).X - getStyles(elem, "left");
y = pagePos(e).Y - getStyles(elem, "top");
addEvent(document, "mousemove", mouseMove);
addEvent(document, "mouseup", mouseUp);
cancelBubble(e); //保险起见 把阻止事件放进去
preventDefaultEvent(e);
});
function mouseMove(e) {
console.log(elem);
var e = e || window.event;
elem.style.left = pagePos(e).X - x + "px";
elem.style.top = pagePos(e).Y - y + "px";
}
function mouseUp(e) {
var e = e || window.event;
removeEvent(document, "mousemove", mouseMove);
removeEvent(document, "mouseup", mouseUp);
}
}
});
调用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<style>
body{
margin: 0;
}
a{
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
background-color: green;
}
</style>
<body>
<!-- <a href="http://www.baidu.com">百度</a> -->
<a href="javascript:;">百度</a>
<!-- <a href="javascript:void(0)"></a> -->
<!-- <button>点击</button> -->
<script type="text/javascript" src="./utils.js"></script>
<script type="text/javascript" src="./index.js"></script>
<script>
// var oBtn=document.getElementsByTagName('button')[0];
var oLink=document.getElementsByTagName('a')[0];
// oMenu=document.getElementsByTagName('div')[0];
dragNclick(oLink)
oBtn.onclick = function() {
console.log('click');
}
oBtn.onmousedown=function() {
console.log('mousedown');
}
oBtn.onmouseup = function() {
console.log('mouseup');
}
// mousedown+mouseup=click
//https://aoxvpn.cc/?ch=0
</script>
</body>
</html>
点击与拖拽1
有不好的地方,当符合条件时既有点击,又有拖拽
var dragNclick = (function (elem,elemClick) {
var bTime=0,
eTime = 0;
drag();
function drag() {
var x, y;
addEvent(elem, "mousedown", function (e) {
// console.log('1');
var e = e || window.event;
bTime=new Date().getTime();
console.log('bTime',bTime);
// console.log(elem);
x = pagePos(e).X - getStyles(elem, "left");
y = pagePos(e).Y - getStyles(elem, "top");
addEvent(document, "mousemove", mouseMove);
addEvent(document, "mouseup", mouseUp);
cancelBubble(e); //保险起见 把阻止事件放进去
preventDefaultEvent(e);
});
function mouseMove(e) {
// console.log(elem);
var e = e || window.event;
elem.style.left = pagePos(e).X - x + "px";
elem.style.top = pagePos(e).Y - y + "px";
}
function mouseUp(e) {
var e = e || window.event;
eTime=new Date().getTime();
console.log('减法结果',eTime-bTime)
console.log('eTime',eTime)
if(eTime-bTime<100){//如果小于100就是点击事件,而不是拖动事件
// console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
elemClick();
}
removeEvent(document, "mousemove", mouseMove);
removeEvent(document, "mouseup", mouseUp);
}
}
});
点击与拖拽2
var dragNclick = (function (elem, elemClick) {
var bTime = 0,
eTime = 0,
oPos=[];
drag();
function drag() {
var x, y;
addEvent(elem, "mousedown", function (e) {
// console.log('1');
var e = e || window.event;
bTime = new Date().getTime();
// console.log("bTime", bTime);
// console.log(elem);
x = pagePos(e).X - getStyles(elem, "left");
y = pagePos(e).Y - getStyles(elem, "top");
addEvent(document, "mousemove", mouseMove);
addEvent(document, "mouseup", mouseUp);
cancelBubble(e); //保险起见 把阻止事件放进去
preventDefaultEvent(e);
});
function mouseMove(e) {
// console.log(elem);
var e = e || window.event;
elem.style.left = pagePos(e).X - x + "px";
elem.style.top = pagePos(e).Y - y + "px";
}
function mouseUp(e) {
var e = e || window.event;
eTime = new Date().getTime();
oPos=[getStyles(elem,'left'),getStyles(elem,'top')];
// console.log("减法结果", eTime - bTime);
// console.log("eTime", eTime);
if (eTime - bTime < 100) {
//如果小于100就是点击事件,而不是拖动事件
// console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
elem.style.left=oPos[0]+'px';
elem.style.top=oPos[1]+'px';
elemClick();
}
removeEvent(document, "mousemove", mouseMove);
removeEvent(document, "mouseup", mouseUp);
}
}
});
元素原型封装
Element.prototype.dragNclick = (function (_self,elemClick) {
var bTime = 0,
eTime = 0,
oPos=[];
drag.call(this);//改变this指向,变成调用的this
function drag() {
var x, y,
_self=this;
// console.log('this',this)
addEvent(this, "mousedown", function (e) {
// console.log('1');
var e = e || window.event;
bTime = new Date().getTime();
// console.log("bTime", bTime);
// console.log(elem);
x = pagePos(e).X - getStyles(_self, "left");
y = pagePos(e).Y - getStyles(_self, "top");
addEvent(document, "mousemove", mouseMove);
addEvent(document, "mouseup", mouseUp);
cancelBubble(e); //保险起见 把阻止事件放进去
preventDefaultEvent(e);
});
function mouseMove(e) {
// console.log(elem);
var e = e || window.event;
_self.style.left = pagePos(e).X - x + "px";
_self.style.top = pagePos(e).Y - y + "px";
}
function mouseUp(e) {
var e = e || window.event;
eTime = new Date().getTime();
oPos=[getStyles(_self,'left'),getStyles(_self,'top')];
// console.log("减法结果", eTime - bTime);
// console.log("eTime", eTime);
if (eTime - bTime < 100) {
//如果小于100就是点击事件,而不是拖动事件
// console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
_self.style.left=oPos[0]+'px';
_self.style.top=oPos[1]+'px';
elemClick();
}
removeEvent(document, "mousemove", mouseMove);
removeEvent(document, "mouseup", mouseUp);
}
}
});
解决拖拽超出滚动条的问题
Element.prototype.dragNclick = (function (_self,elemClick) {
var bTime = 0,
eTime = 0,
oPos=[],
wWidth=getViewportSize().width,
wHeight=getViewportSize().height,
eleWidth=getStyles(this,'width'),
eleHeight=getStyles(this,'height');
drag.call(this);//改变this指向,变成调用的this
function drag() {
var x, y,
_self=this;
// console.log('this',this)
addEvent(this, "mousedown", function (e) {
// console.log('1');
var e = e || window.event;
bTime = new Date().getTime();
// console.log("bTime", bTime);
// console.log(elem);
x = pagePos(e).X - getStyles(_self, "left");
y = pagePos(e).Y - getStyles(_self, "top");
addEvent(document, "mousemove", mouseMove);
addEvent(document, "mouseup", mouseUp);
cancelBubble(e); //保险起见 把阻止事件放进去
preventDefaultEvent(e);
});
function mouseMove(e) {
// console.log(elem);
var e = e || window.event,
eleLeft=pagePos(e).X-x,//
eleTop=pagePos(e).Y-y;
if(eleLeft<=0){
eleLeft=0;
}else if(eleLeft>=wWidth-eleWidth){//
eleLeft=wWidth-eleWidth-1;
}
if(eleTop<=0){
eleTop=0;
}else if(eleTop>=wHeight-eleHeight){
eleTop=wHeight-eleHeight-1;
}
// _self.style.left = pagePos(e).X - x + "px";
// _self.style.top = pagePos(e).Y - y + "px";
_self.style.left = eleLeft+ "px";
_self.style.top = eleTop + "px";
}
function mouseUp(e) {
var e = e || window.event;
eTime = new Date().getTime();
oPos=[getStyles(_self,'left'),getStyles(_self,'top')];
// console.log("减法结果", eTime - bTime);
// console.log("eTime", eTime);
if (eTime - bTime < 100) {
//如果小于100就是点击事件,而不是拖动事件
// console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
_self.style.left=oPos[0]+'px';
_self.style.top=oPos[1]+'px';
elemClick();
}
removeEvent(document, "mousemove", mouseMove);
removeEvent(document, "mouseup", mouseUp);
}
}
});
封装右键菜单
Element.prototype.dragNclick = function ( menu,elemClick) {
var bTime = 0,
eTime = 0,
oPos = [],
wWidth = getViewportSize().width,
wHeight = getViewportSize().height,
eleWidth = getStyles(this, "width"),
eleHeight = getStyles(this, "height"),
mWidth=getStyles(menu,'width'),
mHeight=getStyles(menu,'height'),
cbTime=0,
ceTime=0,
counter=0;
drag.call(this); //改变this指向,变成调用的this
function drag() {
var x,
y,
_self = this;
// console.log('this',this)
addEvent(this, "mousedown", function (e) {
// console.log('1');
var e = e || window.event,
btnCode=e.button;//e.button消耗效率,缓存一下比较好
if(btnCode===2){
var mLeft=pagePos(e).X,
mTop=pagePos(e).Y;
if(mLeft<=0){
mLeft=0
}else if(mLeft>=wWidth-mWidth){
mLeft=pagePos(e).X-mWidth;
}
if(mTop<=0){
mTop=0;
}else if(mTop>=wHeight-mHeight){
mTop=pagePos(e).Y-mHeight
}
menu.style.left=mLeft+'px';
menu.style.top=mTop+'px';
menu.style.display='block';
}else if(btnCode===0){
bTime = new Date().getTime();
oPos=[getStyles(_self,'left'),getStyles(_self,'top')];
menu.style.display='none';
// console.log("bTime", bTime);
// console.log(elem);
x = pagePos(e).X - getStyles(_self, "left");
y = pagePos(e).Y - getStyles(_self, "top");
addEvent(document, "mousemove", mouseMove);
addEvent(document, "mouseup", mouseUp);
cancelBubble(e); //保险起见 把阻止事件放进去
preventDefaultEvent(e);
}
});
//阻止默认右键点击事件,右键点击不会触发默认的右键菜单
addEvent(document,'contextmenu',function(e){
var e=e||window.event;
preventDefaultEvent(e);
})
//点击文档区域,让自定义的菜单消失
addEvent(document,'click',function(e){
menu.style.display='none';
})
//阻止冒泡
addEvent(menu,'click',function(e){
var e=e||window.event;
cancelBubble(e);
})
function mouseMove(e) {
// console.log(elem);
var e = e || window.event,
eleLeft = pagePos(e).X - x, //
eleTop = pagePos(e).Y - y;
if (eleLeft <= 0) {
eleLeft = 0;
} else if (eleLeft >= wWidth - eleWidth) {
//
eleLeft = wWidth - eleWidth - 1;
}
if (eleTop <= 0) {
eleTop = 0;
} else if (eleTop >= wHeight - eleHeight) {
eleTop = wHeight - eleHeight - 1;
}
// _self.style.left = pagePos(e).X - x + "px";
// _self.style.top = pagePos(e).Y - y + "px";
_self.style.left = eleLeft + "px";
_self.style.top = eleTop + "px";
}
function mouseUp(e) {
var e = e || window.event;
eTime = new Date().getTime();
oPos = [getStyles(_self, "left"), getStyles(_self, "top")];
// console.log("减法结果", eTime - bTime);
// console.log("eTime", eTime);
if (eTime - bTime < 100) {
//如果小于100就是点击事件,而不是拖动事件
// console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
_self.style.left = oPos[0] + "px";
_self.style.top = oPos[1] + "px";
counter++;
if(counter===1){
cbTime=new Date().getTime();
}
if(counter===2){
ceTime=new Date().getTime();
}
if(cbTime&&ceTime&&(ceTime-cbTime<200)){
elemClick();
// cbTime=0;
// ceTime=0;
// counter=0;
}
t=setTimeout(function(){//如果点一次,隔2秒清零,要不然无限叠加次数
cbTime=0;
ceTime=0;
counter=0;
clearTimeout(t);
},200);
}
removeEvent(document, "mousemove", mouseMove);
removeEvent(document, "mouseup", mouseUp);
}
}
};
多人协作封装
;(function(){
var Test=function(opt){
}
Test.prototype={
}
window.Test=Test;
})();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<input type="text" id="num1">
<input type="text" id="num2">
<div class="btn-group">
<button data-sign="plus">+</button>
<button data-sign="minus">-</button>
<button data-sign="mul">*</button>
<button data-sign="div">/</button>
</div>
<script type="text/javascript" src="./utils.js"></script>
<script>
// console.log(document.getElementsByClassName('btn-group')[0])
; (function () {
var _self;//保存this
var Test = function (opt) {
this.num1 = opt.num1;
this.num2 = opt.num2;
this.btnGroup = opt.btnGroup;
_self = this;
}
Test.prototype = {
init: function () {//
this.bindEvent();
},
bindEvent: function () {
var btns = this.btnGroup;
// _self = this;//不能成功 不能影响compute
// addEvent(btns,'click',function(e){
// });
// addEvent(btns,'click',this.compute.call(this));//执行了 不行
// addEvent(btns,'click',this.compute);
addEvent(btns, 'click', function (e) {
// this.compute.call(_self);//不行
_self.compute.call(_self, e);
})
},
compute: function (e) {
// console.log(e)
// console.log('this',this)//this是btns对象,相当于btns调用
// var val1 = _self.num1.value,
// val2 = _self.num2.value,
// btns = _self.btnGroup;
var val1 = this.num1.value,
tar = e.target || e.srcElement,
val1 =Number(this.num1.value) ,
val2 =Number( this.num2.value),
btns = this.btnGroup,
sign;
sign = tar.getAttribute('data-sign');
switch (sign) {
case 'plus':
console.log(val1+val2)
break;
case 'minus':
console.log(val1-val2)
break;
case 'mul':
console.log(val1*val2)
break;
case 'div':
console.log(val1/val2)
break;
default:
console.log('出错了')
break;
}
}
}
window.Test = Test;
})();
new Test({
num1: document.getElementById('num1'),
num2: document.getElementById('num2'),
btnGroup: document.getElementsByClassName('btn-group')[0]
}).init();
// function addEvent(el, type, fn) {
// // console.log(el);
// if (el.addEventListener) {
// //方法存在
// el.addEventListener(type, fn, false);
// } else if (el.attachEvent) {
// el.attachEvent("on" + type, function () {
// fn.call(el); //将this指向改为调用的本身
// });
// } else {
// el["on" + type] = fn; //相当于el.on,例子el.onclick=func
// }
// }
</script>
</body>
</html>