js拖动层

CiCi_Victory 2011-09-26 03:18:18
下面的代码为什么布恩那个实现层的拖动

<script type="text/javascript">
function divdown(){
flag=true;
//o=document.getElementById("move");
//o.setCapture();
//alert(flag);
}

document.onmousemove=function(){
d=event;
window.status="X:"+event.clientX+" Y:"+event.clientY;
//if(!o)return;
if(flag){
a=document.getElementById("move");
b=event.clientX-parseInt(a.style.left);
c=event.clientY-parseInt(a.style.top);
a.style.left=(d.clientX-b)+"px";
a.style.top=(d.clientY-c)+"px";
//a.style.left=(event.clientX)+"px";
//a.style.top=(event.clientY)+"px";
}
}

function divup(){
flag=false;
//alert(flag);
}

function showdiv(){
var div=document.createElement("div");
div.id="move";
div.style.position="absolute";
div.style.backgroundColor="#f0f";
div.style.width=100+"px";
div.style.height=100+"px";
div.style.top=100+"px";
div.style.left=100+"px";
document.body.appendChild(div);
div.style.display="block";
div.onmousedown=divdown;
div.onmouseup=divup;
}
var a,b,c,flag=false,o;


</script>
</head>

<body>
<input id="btn" type="button" value="显示DIV" onclick="showdiv()"/>
</body>
...全文
78 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
CiCi_Victory 2011-09-26
  • 打赏
  • 举报
回复
不过你让我明白一件事,就是我什么地方写多了 写了 分给你了
CiCi_Victory 2011-09-26
  • 打赏
  • 举报
回复
这样其实可以,但是不跟着鼠标走啊,兄弟
001007009 2011-09-26
  • 打赏
  • 举报
回复

<!doctype html>
<script type="text/javascript">
function divdown(){
flag=true;
//o=document.getElementById("move");
//o.setCapture();
//alert(flag);
}

document.onmousemove=function(event){
event = event || window.event;
window.status="X:"+event.clientX+" Y:"+event.clientY;
//if(!o)return;
if(flag){
a=document.getElementById("move");
b=parseInt(a.style.width)/2
c=parseInt(a.style.top)/2
a.style.left=(event.clientX-b)+"px";
a.style.top=(event.clientY-c)+"px";
// a.style.left=(event.clientX)+"px";
// a.style.top=(event.clientY)+"px";
}
}

function divup(){
flag=false;
//alert(flag);
}

function showdiv(){
var div=document.createElement("div");
div.id="move";
div.style.position="absolute";
div.style.backgroundColor="#f0f";
div.style.width=100+"px";
div.style.height=100+"px";
div.style.top=100+"px";
div.style.left=100+"px";
document.body.appendChild(div);
div.style.display="block";
div.onmousedown=divdown;
div.onmouseup=divup;
}
var a,b,c,flag=false,o;


</script>
</head>

<body>
<input id="btn" type="button" value="显示DIV" onclick="showdiv()"/>
<div id="test"></div>
</body>



楼主 试试

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧