<body>
<p></p></body>p{ position: absolute; width: 200px; height: 200px; background: #369
}window.onload = function(){
var op = document.getElementsByTagName("p")[0];
/*鼠标点击的位置距离p左边的距离 */
var disX = 0;
/*鼠标点击的位置距离p顶部的距离*/
var disY = 0;
op.onmousedown = function(){
var e = e || window.event;
disX = e.clientX - op.offsetLeft;
disY = e.clientY- op.offsetTop;
document.onmousemove = function(e){
var e = e || window.event;
// 横轴坐标
var leftX = e.clientX - disX;
// 纵轴坐标
var topY =e.clientY - disY;
if( leftX < 0 ){
leftX = 0;
} /* 获取浏览器视口大小 document.document.documentElement.clientWidth*/
else if( leftX > document.documentElement.clientWidth - op.offsetWidth ){
leftX = document.document.documentElement.clientWidth - op.offsetWidth;
}
if( topY < 0 ){
topY = 0;
}
else if( topY > document.documentElement.clientHeight -op.offsetHeight ){
topY = document.documentElement.clientHeight - op.offsetHeight;
}
op.style.left = leftX + "px";
op.style.top = topY+"px";
}
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
}
}相关推荐:
HTML5讲解之拖拽事件dragstart、drag和dragend
以上就是js实现拖拽事件的方法实例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号