

<!DOCTYPE">
<html">
<head>
<style>
#p1 {width:100px; height:100px; background:red; position:absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
var oDiv=null;
var disX=0;
var disY=0;
window.onload=function ()
{
oDiv=document.getElementById('p1');
oDiv.onmousedown=fnDown;
};
function fnDown(ev)
{
var oEvent=ev||event;
disX=oEvent.clientX-oDiv.offsetLeft;
disY=oEvent.clientY-oDiv.offsetTop;
document.onmousemove=fnMove;
document.onmouseup=fnUp;
}
function fnMove(ev)
{
var oEvent=ev||event;
oDiv.style.left=oEvent.clientX-disX+'px';
oDiv.style.top=oEvent.clientY-disY+'px';
}
function fnUp()
{
document.onmousemove=null;
document.onmouseup=null;
}
</script>
</head>
<body>
<p id="p1">
</p>
</body>
</html>相信看了这些案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
相关阅读:
js代码案列-根据日期计算星期几
JS引擎运行时是什么样的
H5中的弹窗无法用webview弹出怎么解决
