整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

JavaScript 如何拖拽元素?

例: 限制范围的拖拽

<!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>

实例: 进一步优化(带影子拖拽)

家好,今天给大家介绍一款,css3实现的鼠标悬停特效,鼠标悬停给图片加边框html页面前端源码(图1)。送给大家哦,获取方式在本文末尾。

图1

鼠标放在图像上,图像后面的边框就会上浮包住图像(图2)

图2

源码完整,需要的朋友可以下载学习(图3)

图3

本源码编码:10191,需要的朋友,访问下面链接后,搜索10191,即可获取。

「链接」

家好,今天给大家介绍一款,css实现的悬停菜单鼠标跟随图片显示交互特效html页面前端源码(图1),布局合理。送给大家哦,获取方式在本文末尾。

图1

菜单切换的时候,鼠标会跟随显示不同的图片,非常炫酷(图2)

图2

源码完整,需要的朋友可以下载学习(图3)

图3

本源码编码:10206,需要的朋友,访问下面链接后,搜索10206,即可获取。

「链接」