当前位置:编程学习 > JS >>

最短的网页拖动代码,兼容性好

代码最少的拖动代码,几乎兼容所有的浏览器,无调用外部JS插件,完全JavaScript代码实现,兼容IE/Firefox/Chrome等内核的浏览器,分享给前端开发者参考。
答案:<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>兼容浏览器最多代码最少的拖动代码</title>
</head>
<body>
<div id="demo" style="width:100px;height:100px;position:absolute;background:red;cursor:move;"></div>
<script type="text/javascript">
function drag(o){var a="onmousemove",b="setCapture",c="releaseCapture",f="clientY",g="clientX",d=document,z=d.documentElement,x,y,t,l,w,h;o=d.getElementById(o);o.onmousedown=function(e){e=e||event;x=e[g]-o.offsetLeft;y=e[f]-o.offsetTop;d[a]=function(e){e=e||event;t=e[f]-y;l=e[g]-x;w=z.clientWidth-o.offsetWidth;h=z.clientHeight-o.offsetHeight;l<0&&(l=0);t<0&&(t=0);l>w&&(l=w);t>h&&(t=h);with(o.style){top=t+"px";left=l+"px"}};d.onmouseup=function(){d[a]=null;o[c]&&o[c]()};o[b]&&o[b]();return false}};
drag("demo");
</script>
</body>
</html>

上一个:LIGHTBOX示例,弹出层
下一个:点击展开的JS代码示例

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,