网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
07月20日漏签0天
javascript吧 关注:269,219贴子:906,509
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 4回复贴,共1页
<<返回javascript吧
>0< 加载中...

闭包

  • 只看楼主
  • 收藏

  • 回复
  • 没有如果379
  • trycatch
    9
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
写了一个拖拽函数,想通过闭包的方式让index公有化,在这不同的obj设置拖拽的时候,他们共用一个zindex
代码如下
/*拖拽函数,限定范围为整个窗口内 */function drag(obj){ var _this = this; var zindex = 100; return function(){ obj.onmousedown = function(e){ var e = e || window.event; var disX = e.clientX - this.offsetLeft; var disY = e.clientY - this.offsetTop; console.log(zindex); //zindex++; obj.style.zIndex = zindex++; if(obj.setCapture){ obj.setCapture(); obj.onmousemove = mouseMove obj.onmouseup = mouseUp; }else{ document.onmousemove = mouseMove; document.onmouseup = mouseUp; } return false; function mouseMove(e){ var e = e || window.event; var l = e.clientX - disX; var t = e.clientY - disY; if(obj.setCapture){ obj.setCapture(); } //console.log(e.clientX); if(l<=-obj.parentNode.getBoundingClientRect().left){ l = -obj.parentNode.getBoundingClientRect().left; }else if(l>= viewW()- obj.offsetWidth- obj.parentNode.getBoundingClientRect().left){ l = viewW()- obj.offsetWidth - obj.parentNode.getBoundingClientRect().left; } if(t<=-obj.parentNode.getBoundingClientRect().top){ t = -obj.parentNode.getBoundingClientRect().top; }else if(t>= viewH()- obj.offsetHeight- obj.parentNode.getBoundingClientRect().top){ t = viewH()- obj.offsetHeight - obj.parentNode.getBoundingClientRect().top; } obj.style.left = l + 'px'; obj.style.top = t + 'px'; } function mouseUp(){ if(obj.releaseCapture){ obj.releaseCapture(); } this.onmousemove = null; this.onmouseup = null; } }; }}


  • 没有如果379
  • trycatch
    9
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
擦,手机发的,代码乱了。。。


2025-07-20 13:30:06
广告
  • 没有如果379
  • trycatch
    9
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
代码如下
/*
拖拽函数,限定范围为整个窗口内
*/
function drag(obj,map){
var _this = this;
var zindex = 100;
return function(){
obj.onmousedown = function(e){
var e = e || window.event;
var disX = e.clientX - this.offsetLeft;
var disY = e.clientY - this.offsetTop;
console.log(zindex);
var pos = [];
var collision = false;//是否碰撞到
if(map){
for(var i=0;i<map.length;i++){
pos.push({
'left':map[i].offsetLeft,
'top':map[i].offsetTop
});
}
}
obj.style.zIndex = zindex++;
if(obj.setCapture){
obj.setCapture();
obj.onmousemove = mouseMove
obj.onmouseup = mouseUp;
}else{
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
}
return false;
function mouseMove(e){
var e = e || window.event;
var l = e.clientX - disX;
var t = e.clientY - disY;
if(obj.setCapture){
obj.setCapture();
}
//console.log(e.clientX);
if(l<=-obj.parentNode.getBoundingClientRect().left){
l = -obj.parentNode.getBoundingClientRect().left;
}else if(l>= viewW()- obj.offsetWidth- obj.parentNode.getBoundingClientRect().left){
l = viewW()- obj.offsetWidth - obj.parentNode.getBoundingClientRect().left;
}
if(t<=-obj.parentNode.getBoundingClientRect().top){
t = -obj.parentNode.getBoundingClientRect().top;
}else if(t>= viewH()- obj.offsetHeight- obj.parentNode.getBoundingClientRect().top){
t = viewH()- obj.offsetHeight - obj.parentNode.getBoundingClientRect().top;
}
obj.style.left = l + 'px';
obj.style.top = t + 'px';
if(map){
for(var i=0;i<map.length;i++){
if(map[i] != obj){
var l = obj.offsetLeft;
var t = obj.offsetTop;
var w = obj.offsetWidth;
var h = obj.offsetHeight;
if(map[i].offsetLeft<l+w &&map[i].offsetLeft>l && map[i].offsetTop>t&&map[i].offsetTop<t+h || map[i].offsetLeft+map[i].offsetWidth>l&&map[i].offsetLeft+map[i].offsetWidth<l+w&&map[i].offsetTop<t+h&&map[i].offsetTop>t || map[i].offsetLeft+map[i].offsetWidth>l&&map[i].offsetLeft+map[i].offsetWidth<l+w&&map[i].offsetTop+map[i].offsetHeight<t+h&&map[i].offsetTop+map[i].offsetHeight>t || map[i].offsetLeft+map[i].offsetWidth>l&&map[i].offsetLeft+map[i].offsetWidth<l+w&&map[i].offsetTop+map[i].offsetHeight>t&&map[i].offsetTop+map[i].offsetHeight<t+h){
collision = true;
//break;
}else{
collision = false;
}
}
}
console.log(collision)
}
}
function mouseUp(){
if(obj.releaseCapture){
obj.releaseCapture();
}
this.onmousemove = null;
this.onmouseup = null;
}
};
}


  • 没有如果379
  • trycatch
    9
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
还是没人吗?


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 4回复贴,共1页
<<返回javascript吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示