例: 限制范圍的拖拽
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
width: 800px;
height: 400px;
margin: 50px auto;
border: 1px solid #f00;
/*讓拖拽元素根據它進行定位*/
position: relative;
}
.move{
width: 200px;
height: 120px;
cursor: move;
background-color: orange;
/*定位屬性*/
position: absolute;
left: 100px;
top: 50px;
}
</style>
</head>
<body>
<div class="box">
<div class="move"></div>
</div>
<script type="text/javascript">
//獲取box盒子
var box = document.querySelector(".box");
//獲取拖拽的盒子
var move = document.querySelector(".move");
//求得box盒子距離body的凈位置
var boxLeft = box.getBoundingClientRect().left;
var boxTop = box.getBoundingClientRect().top;
//拖拽三大事件
move.onmousedown = function(e){
var ev = e || window.event;//事件對象兼容
//存儲鼠標按下時到事件源的位置
var startX = ev.offsetX;
var startY = ev.offsetY;
document.onmousemove = function(e){
var ev = e || window.event;//事件對象兼容
//真實的拖拽元素的left和top值
var left = ev.clientX -boxLeft - startX;
var top = ev.clientY - boxTop - startY;
//多拖拽盒子的left和top值進行約束
if(left<0){
left = 0;//left最小是0
}else if(left>(box.offsetWidth-move.offsetWidth)){
left = box.offsetWidth-move.offsetWidth;//left最大是大盒子寬度-小盒子寬度
}
if(top<0){
top = 0;//top最小是0
}else if(top>(box.offsetHeight-move.offsetHeight)){
top = box.offsetHeight-move.offsetHeight;//top最大是大盒子高度-小盒子高度
}
//設置拖拽元素的left和top屬性值
move.style.left = left + "px"
move.style.top = top + "px"
}
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
}
</script>
</body>
</html>
實例: 進一步優化(帶吸附拖拽)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
width: 800px;
height: 400px;
margin: 50px auto;
border: 1px solid #f00;
/*讓拖拽元素根據它進行定位*/
position: relative;
}
.move{
width: 200px;
height: 120px;
cursor: move;
background-color: orange;
/*定位屬性*/
position: absolute;
left: 100px;
top: 50px;
}
</style>
</head>
<body>
<div class="box">
<div class="move"></div>
</div>
<script type="text/javascript">
//獲取box盒子
var box = document.querySelector(".box");
//獲取拖拽的盒子
var move = document.querySelector(".move");
//求得box盒子距離body的凈位置
var boxLeft = box.getBoundingClientRect().left;
var boxTop = box.getBoundingClientRect().top;
//拖拽三大事件
move.onmousedown = function(e){
var ev = e || window.event;//事件對象兼容
//存儲鼠標按下時到事件源的位置
var startX = ev.offsetX;
var startY = ev.offsetY;
document.onmousemove = function(e){
var ev = e || window.event;//事件對象兼容
//真實的拖拽元素的left和top值
var left = ev.clientX -boxLeft - startX;
var top = ev.clientY - boxTop - startY;
//彈性吸附 就是讓他還差**px時我就讓他到邊邊上
if(left<20){
left = 0;//left最小是0
}else if(left>(box.offsetWidth-move.offsetWidth-20)){
left = box.offsetWidth-move.offsetWidth;//left最大是大盒子寬度-小盒子寬度
}
if(top<20){
top = 0;//top最小是0
}else if(top>(box.offsetHeight-move.offsetHeight-20)){
top = box.offsetHeight-move.offsetHeight;//top最大是大盒子高度-小盒子高度
}
//設置拖拽元素的left和top屬性值
move.style.left = left + "px"
move.style.top = top + "px"
}
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
}
</script>
</body>
</html>
實例: 進一步優化(帶影子拖拽)
圖1
圖2
圖3
圖1
圖2
圖3
*請認真填寫需求信息,我們會在24小時內與您取得聯系。