js简单实现多个div拖动效果
要在OA里做个便签模块,我想把它做成类似于许愿墙的效果,每条记录放在单独的层里,通过js让每个div可拖动
点这里查看例子
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
#main div{position:absolute;width:220px;height:150px;border:1px solid #999;}
</style>
<script type="text/javascript">
var a;
document.onmouseup=function(){
if(!a)return;
document.all?a.releaseCapture():window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
a="";
};
document.onmousemove=function (d){
if(!a) return;
if(!d) d=event;
a.style.left=(d.clientX-b)+"px";a.style.top=(d.clientY-c)+"px";
};
function move(o,e){
a=o;
document.all?a.setCapture():window.captureEvents(Event.MOUSEMOVE);
b=e.clientX-parseInt(a.style.left);
c=e.clientY-parseInt(a.style.top);
o.style.zIndex=getMaxIndex()+1;
}
function $(id){return document.getElementById(id);}
function getMaxIndex(){
var index=0;
var ds=$('main').getElementsByTagName('div');
var l=$('main').getElementsByTagName('div').length;
for (i=0;i<l;i++)
{
if (ds[i].style.zIndex>index) index=ds[i].style.zIndex;
}
return index;
}
</script>
</head>
<body>
<div id="main">
<div style="left:100px;top:100px;background:#fc9;" onmousedown="move(this,event)">1</div>
<div style="left:400px;top:100px;background:#9cf;" onmousedown="move(this,event)">2</div>
<div style="left:700px;top:100px;background:#f9c;" onmousedown="move(this,event)">3</div>
<div style="left:100px;top:300px;background:#9fc;" onmousedown="move(this,event)">4</div>
<div style="left:400px;top:300px;background:#c9f;" onmousedown="move(this,event)">5</div>
<div style="left:700px;top:300px;background:#cf9;" onmousedown="move(this,event)">6</div>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
#main div{position:absolute;width:220px;height:150px;border:1px solid #999;}
</style>
<script type="text/javascript">
var a;
document.onmouseup=function(){
if(!a)return;
document.all?a.releaseCapture():window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
a="";
};
document.onmousemove=function (d){
if(!a) return;
if(!d) d=event;
a.style.left=(d.clientX-b)+"px";a.style.top=(d.clientY-c)+"px";
};
function move(o,e){
a=o;
document.all?a.setCapture():window.captureEvents(Event.MOUSEMOVE);
b=e.clientX-parseInt(a.style.left);
c=e.clientY-parseInt(a.style.top);
o.style.zIndex=getMaxIndex()+1;
}
function $(id){return document.getElementById(id);}
function getMaxIndex(){
var index=0;
var ds=$('main').getElementsByTagName('div');
var l=$('main').getElementsByTagName('div').length;
for (i=0;i<l;i++)
{
if (ds[i].style.zIndex>index) index=ds[i].style.zIndex;
}
return index;
}
</script>
</head>
<body>
<div id="main">
<div style="left:100px;top:100px;background:#fc9;" onmousedown="move(this,event)">1</div>
<div style="left:400px;top:100px;background:#9cf;" onmousedown="move(this,event)">2</div>
<div style="left:700px;top:100px;background:#f9c;" onmousedown="move(this,event)">3</div>
<div style="left:100px;top:300px;background:#9fc;" onmousedown="move(this,event)">4</div>
<div style="left:400px;top:300px;background:#c9f;" onmousedown="move(this,event)">5</div>
<div style="left:700px;top:300px;background:#cf9;" onmousedown="move(this,event)">6</div>
</div>
</body>
</html>
Tag标签: js拖动层JavaScript
- 评论
-
- [使用Ctrl+Enter键可以直接提交]
表情图标
- 所属分类的其它文章:
- ★Javascript获取某一标签的座标并返回其座标对象
- ★javascript操作Select标记中options集合
- ★div中放flash运行30秒钟后自动隐藏效果
- ★setTimeout和setInterval的区别
- ★attachEvent传递参数的办法
- ★JavaScript 是什么?
- ★5个有趣的浏览器地址栏JavaScript代码
- ★iframe自适应高度
- ★在文本框中光标位置插入文本
- ★event.srcElement说明,方法,技巧
- ★时间和日期的各种格式的特效代码
- ★在JS和ASP.NET中操作COOKIE
- ★添加到收藏夹 设为首页的js
- ★验证是否跨站提交
- ★变量location在Firefox和IE中的区别
- 随便看看:
Advertise
Category
Time Counter
离十一还有
Recent Article
- 1.工作笔记:AS3加载外部图片,有加载百分比
- 2.document.documentElement和document.body的区别
- 3.100种增加网站流量的方法
- 4.用div+css模拟表格对角线
- 5.如何做一个好的技术型领导
- 6.苍井空是谁?
- 7."心态"新解
- 8.程序员特有的9个坏习惯
- 9.AS3入门之简单Loading效果
- 10.系统问题:浏览器无法打开png图片
- 11.AS3中以post和get方式提交数据
- 12.defaultTextFormat和setTextFormat()区别
- 13.AS3中超方便地遍历xml
- 14.pv3d中物体常用的移动属性
- 15.开源Flash 3D引擎Papervision3d
- 16.as3 判断鼠标滚轮前滚或后滚
- 17.js:行向上替换滚动效果
- 18.为什么要清净?(禅与佛)
- 19.扩展window.setTimeout方法
- 20.收集2010的搞笑短句
Statistics
Recent Comments
Archive
Links
Support
TOP



