码迷,mamicode.com
首页 > 其他好文 > 详细

小盒子拖拽

时间:2018-10-22 23:20:40      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:turn   html   charset   处理   clientx   color   htm   cti   space   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box {
width: 300px;
height: 300px;
background: red;
position: absolute;
}
</style>
</head>
<body>
<div id="box">
快来托我吧
</div>
</body>
</html>
<script>
 //引入
var box = document.getElementById("box");
//鼠标按下
box.onmousedown = function (e) {
var e = e || event;
 
var x = e.offsetX;
var y = e.offsetY;
//在文档页面上移动
document.onmousemove = function (e) {
var e = e || event;
 
var l = e.clientX - x;
var t = e.clientY - y;
//边界处理
  var left = document.documentElement.clientWidth - 300;
var top = document.documentElement.clientHeight - 300;
l = l < 0 ? 0 : (l > left ? left : l);
t = t < 0 ? 0 : (t > top ? top : t);
box.style.left = l + "px"
box.style.top = t + "px";
 
//阻止默认行
return false;
}
//鼠标离开
document.onmouseup = function () {
document.onmousemove = null;
}
}
</script>

小盒子拖拽

标签:turn   html   charset   处理   clientx   color   htm   cti   space   

原文地址:https://www.cnblogs.com/fengjuedingyaozou/p/9833672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!