2个事件都要定义很多很多相同的变量,但是每次都要重新赋值,每个事件对应的这些变量都是要在触发后重新计算后再使用的,所有要用到的变量都是一样的,想简化代码封装成一个函数,但是运行失败说找不到变量,请问是不是这么做是行不通的呢?
function mouseMove(){
document.onmousemove=function(ev){
var ev=ev||event;
var L1=op.offsetLeft-5;
var L2=op.offsetLeft+5;
var R1=L1+op.offsetWidth-5;
var R2=L1+op.offsetWidth+5;
var T1=op.offsetTop;
var B1=op.offsetTop+op.offsetHeight;
if(ev.clientX>L1&&ev.clientX<L2&&ev.clientY>T1&&ev.clientY<B1)
document.documentElement.style.cursor='w-resize';
else if(ev.clientX>R1&&ev.clientX<R2&&ev.clientY>T1&&ev.clientY<B1)
document.documentElement.style.cursor='e-resize';
else
document.documentElement.style.cursor='auto';
};
}
document.onmousedown=function(ev){
var ev=ev||event;
var L1=op.offsetLeft-5;
var L2=op.offsetLeft+5;
var R1=L1+op.offsetWidth-5;
var R2=L1+op.offsetWidth+5;
var L=op.offsetLeft;
var R=op.offsetLeft+op.offsetWidth;
var T1=op.offsetTop;
var B1=op.offsetTop+op.offsetHeight;
if(ev.clientX>L1&&ev.clientX<L2&&ev.clientY>T1&&ev.clientY<B1){
var difX=ev.clientX-L;
document.onmousemove=function(ev){
var ev=ev||event;
op.style.left=ev.clientX-difX+'px';
op.style.width=R-ev.clientX+difX+'px';
};
document.onmouseup=function(){
document.onmousemove=null;
mouseMove();
};
}
高洛峰2017-04-11 09:21:36
before
var a = 1;
var b = 2;
var c = 3;
var d = 4;
var e = 5;
//xx(a,c,d,e,b)...
after
var ctx = newContext();
//xx(ctx.a, ctx.c, ctx.d, ctx.e,ctx.b)...
function newContext() {
return {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
}
}