JS 拖动效果实现代码 比较简单
作者:bea
<!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> <title>JS拖动效果</title> <script typ
<!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>
<title>JS拖动效果</title>
<script type="text/javascript"><!--
function DragPanel(Panel,event)
{
var delayX=event.clientX-parseInt(Panel.style.left);
var delayY=event.clientY-parseInt(Panel.style.top);
document.attachEvent("onmousemove",HandleMove);
document.attachEvent("onmouseup",HandleUp);
event.cancelBubble=true;
event.returnValue=false;
function HandleMove(e)
{
e=window.event;
Panel.style.left=event.clientX-delayX+"px";
Panel.style.top=event.clientY-delayY+"px";
event.cancelBubble=true;
}
function HandleUp()
{
document.detachEvent("onmousemove",HandleMove);
document.detachEvent("onmouseup",HandleUp);
}
event.cancelBubble=true;
}
// --></script>
</head>
<body>
<div style="position:absolute; width:200px; height:150px; left:100px; top:300px; background-color:Maroon;">
<div style="background-color:Red;" style="background-color:Red;" onmousedown="DragPanel(this.parentNode,event)">Drag Me</div>
</div>
</body>
</html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- js鼠标移动在title中显示图片的效果代码
- [原创]javascript代码在ie8里报错 document.getElementById(...) 为空或不是对象的解决方法
- javascript &&和||运算法的另类使用技巧
- JavaScript 字符串操作的几种常见方法
- javascript showModalDialog 多层模态窗口实现页面提交及刷新的代码
- JavaScript 闭包在封装函数时的简单分析
- extJs 文本框后面加上说明文字+下拉列表选中值后触发事件
- Javascript 复制数组实现代码
- js setattribute批量设置css样式
- Javascript 二维数组
- isArray()函数(JavaScript中对象类型判断的几种方法)
- javascript 加入收藏、设为首页(IE,firefox兼容)
- Exitjs获取DataView中图片文件名
- 用javascript实现jquery的document.ready功能的实现代码
- 网页中的图片的处理方法与代码
- js 动态选中下拉框
- js 对联广告、漂浮广告封装类(IE,FF,Opera,Safari,Chrome
- JS在IE和FF下attachEvent,addEventListener学习笔记
- JS小框架 fly javascript framework