使用传统的js和css怎样实现在一个Div的范围中拖拽另一个Div. 请高手指教。

 我来答
asd11000
2011-02-16 · TA获得超过341个赞
知道小有建树答主
回答量:117
采纳率:0%
帮助的人:128万
展开全部
以下直接可以运行。
如果DIV在ID为area的div中,即可拖拽。我在Drag函数里做了对父div的判断。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠标拖拽</title>
<script type="text/javascript">
function Drag(o, e){
if(o.parentElement!=document.getElementById('area')) return;
var e = window.event || e;
var _x = e.offsetX || e.layerX;
var _y = e.offsetY || e.layerY;
o.style.filter = 'Alpha(opacity=70)';
o.style.opacity = '0.7';
document.onmousemove = function(e){
var e = window.event || e;
o.style.left = e.clientX - _x + 'px';
o.style.top = e.clientY - _y + 'px';
o.style.cursor="move";
}
document.onmouseup = function(e){
document.onmousemove = null;
o.style.filter = o.style.opacity = '';
o.style.cursor="default";
}
}
</script>
</head>
<body>
<div id='area'>
<div onmousedown="Drag(this, event)" style="position:absolute;border:1px solid red;background:pink;width:400px;height:300px;"></div>
</div>
<div onmousedown="Drag(this, event)" style="position:absolute;left:500px;border:1px solid red;background:pink;width:400px;height:300px;"></div>
</body>
</html>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wvsju60
2011-02-16 · TA获得超过357个赞
知道答主
回答量:193
采纳率:0%
帮助的人:171万
展开全部
判断时改用 if($(objid).style.display="block")
{
不显示;
}
else
{
显示
}
你调用时实参始终是"show",怎么能实现转换呢?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2011-02-19
展开全部
这里有一个在div里面拖动另一个层的效果
可以参考一下

参考资料: http://www.blueidea.com/common/shoutbox/redir.asp?5=n&id=12284

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式