| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 651 人关注过本帖
标题:[求助]帮看看这个拖动层的js代码怎么改
只看楼主 加入收藏
rossi789
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2005-8-13
收藏
 问题点数:0 回复次数:2 
[求助]帮看看这个拖动层的js代码怎么改

为什么第一次拖动的时候它老是会移动到顶端?后面几次就好了.
还有,我只想让它横向的移动,并且只能在它所在表格里横向地移动,改怎么弄?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
<script language="javascript">
var move_obj_x0,move_obj_y0,move_obj_x1,move_obj_y1;
var obj_moveable=false;
function startDrag(obj,id)
{
if(event.button==1)
{
obj.setCapture();
win=document.getElementById(id)
move_obj_x0 = event.clientX;
move_obj_y0 = event.clientY;
move_obj_x1 = parseInt(win.style.left);
move_obj_y1 = parseInt(win.style.top);
obj_moveable = true;
}
}
function drag(id)
{
if(obj_moveable)
{
win=document.getElementById(id)
win.style.left = (move_obj_x1 + event.clientX - move_obj_x0>0)?move_obj_x1 + event.clientX - move_obj_x0:0;
win.style.top = (move_obj_y1 + event.clientY - move_obj_y0>0)?move_obj_y1 + event.clientY - move_obj_y0:0;
}
}
function stopDrag(obj,id)
{
if(obj_moveable)
{
obj.releaseCapture();
obj_moveable = false;
}
}
</script>
</head>
<body>
<table width="300" height="45" border="1" cellspacing="0" cellpadding="0" onmousedown="startDrag(this,'move_test')" onmouseup="stopDrag(this,'move_test')" onmousemove="drag('move_test')">
<tr>
<td><div id="move_test" style="position:absolute;border:1px solid #FF0000">
kkkkkkkkkkkkkkkkkkkkkkk
</div></td>
</tr>
</table>
</body>
</html>

[此贴子已经被作者于2007-3-18 23:08:40编辑过]

搜索更多相关主题的帖子: 拖动 代码 
2007-03-18 23:07
hugeannex
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:483
专家分:911
注 册:2005-3-20
收藏
得分:0 

[CODE]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
<script language="javascript">
var mouse_x,obj_x
var obj_moveable=false;
function startDrag(obj){
mouse_x = event.clientX;
obj_x=parseInt(obj.style.left);
obj.setCapture();
obj_moveable=true;
}
function drag(obj) {
if(obj_moveable && event.button==1) {
obj.style.pixelLeft =obj_x+(event.clientX-mouse_x) ;
if(obj.style.pixelLeft<0){
obj.style.pixelLeft=0
stopDrag(obj);
}
if(obj.style.pixelLeft>document.getElementById("tt").style.pixelWidth-obj.offsetWidth-1){
obj.style.pixelLeft=document.getElementById("tt").style.pixelWidth-obj.offsetWidth-1
stopDrag(obj);
}
}
}
function stopDrag(obj)
{
obj.releaseCapture();
obj_moveable=false;
}
</script>
</head>
<body>
<table id="tt" border="1" cellspacing="0" cellpadding="0" style="position:absolute;left:5;top:5;width:300; height:45">
<tr>
<td>
<div id="move_test" style="position:absolute;left:8;top:8;width:150;border:1px solid #FF0000" onmousedown="startDrag(this)" onmouseup="stopDrag(this)" onmousemove="drag(this)">
kkkkkkkkkkkkkkkkkkkkkkk
</div></td>
</tr>
</table>
</body>
</html>[/CODE]


世事如潮我如水,只叹江湖几人回。
2007-03-19 16:10
rossi789
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2005-8-13
收藏
得分:0 
谢谢了
2007-03-19 20:50
快速回复:[求助]帮看看这个拖动层的js代码怎么改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.059531 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved