首页 技术 正文
技术 2022年11月12日
0 收藏 618 点赞 2,848 浏览 4743 个字

模拟拖拽的原理:

JavaScript动画-模拟拖拽

x1等于div.offsetLeft

y1等于div.offsetTop

x2等于ev.clientX(ev表示event事件)

y2等于ev.clientY

当我们在方块上按下鼠标的时候,x2-x1即可确定。移动鼠标之后,我们用鼠标当前的位置即x4、y4减去x2-x1、y2-y1就可以得到方块现在的位置。

效果图:点击查看

代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box{
width: 100px;
height: 100px;
background: red;
position: absolute;
}
</style>
</head>
<body> <div id="box"></div>
<script type="text/javascript">
var oBox = document.getElementById('box'); oBox.onmousedown = function(ev){
// 鼠标按下 var ev = ev || event; // 获取鼠标离div得距离
var mouseBoxleft = ev.clientX - this.offsetLeft;
var mouseBoxTop = ev.clientY - this.offsetTop; oBox.onmousemove = function(ev){
// 鼠标按下左键并移动 var ev = ev || event; // 设置div移动时,它的位置
oBox.style.left = ev.clientX - mouseBoxleft + 'px';
oBox.style.top = ev.clientY - mouseBoxleft + 'px'; } oBox.onmouseup = function(){
// 鼠标左键抬起 oBox.onmousemove = oBox.onmouseup = null;
}
}
</script>
</body>
</html>

优化代码:

  【1】鼠标移动快的时候,鼠标会移出方块,这时方块就不会再跟随鼠标动了。

      解决办法:就是将onmousemove和onmouseup加到document对象上

效果:点击查看

代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box{
width: 100px;
height: 100px;
background: red;
position: absolute;
}
</style>
</head>
<body> <div id="box"></div>
<script>
var oBox = document.getElementById('box'); oBox.onmousedown = function(ev){
// 鼠标按下 var ev = ev || event; // 获取鼠标离div得距离
var mouseBoxleft = ev.clientX - this.offsetLeft;
var mouseBoxTop = ev.clientY - this.offsetTop; document.onmousemove = function(ev){
// 鼠标按下左键并移动 var ev = ev || event; // 设置div移动时,它的位置
oBox.style.left = ev.clientX - mouseBoxleft + 'px';
oBox.style.top = ev.clientY - mouseBoxleft + 'px'; } document.onmouseup = function(){
// 鼠标左键抬起 document.onmousemove = document.onmouseup = null;
}
}
</script>
</body>
</html>

  【2】当要拖动的方块中有文字时会触发浏览器的默认行为

      解决办法:1、使用return false添加到onmousedown事件中阻止浏览器的默认行为(IE除外)

           2、使用全局捕获(IE)

1、使用return false添加到onmousedown事件中阻止浏览器的默认行为(IE除外)

效果:点击查看

代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box{
width: 100px;
height: 100px;
background: red;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body> <div id="box">模拟拖拽</div>
<script>
var oBox = document.getElementById('box'); oBox.onmousedown = function(ev){
// 鼠标按下 var ev = ev || event;
// 获取鼠标离div得距离
var mouseBoxleft = ev.clientX - this.offsetLeft;
var mouseBoxTop = ev.clientY - this.offsetTop; document.onmousemove = function(ev){
// 鼠标按下左键并移动 var ev = ev || event; // 设置div移动时,它的位置
oBox.style.left = ev.clientX - mouseBoxleft + 'px';
oBox.style.top = ev.clientY - mouseBoxleft + 'px'; } document.onmouseup = function(){
// 鼠标左键抬起 document.onmousemove = document.onmouseup = null;
} // 阻止默认行为
return false;
}
</script>
</body>
</html>

2、使用全局捕获(IE)

  全局捕获:当我们给一个元素这只全局捕获后,改元素会监听后续发生的所有事件,当有事件发生的时候就会触发改元素的事件

  举个栗子:点击查看

代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="button" id="button1" value="弹出1" />
<input type="button" id="button2" value="弹出2" />
<script type="text/javascript">
window.onload = function(){
var Btn1 = document.getElementById('button1');
var Btn2 = document.getElementById('button2'); Btn1.setCapture(); Btn1.onclick = function(){
alert(1);
}
Btn2.onclick = function(){
alert(2);
} }
</script>
</body>
</html>

给Btn1设置了全局捕获之后,即使我们点击了Btn2还是会触发Btn1的点击事件

在模拟拖拽中,给要拖拽的方块onmousedown添加全局捕获然后再onmouseup中取消全局捕获

效果:点击查看

代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box{
width: 100px;
height: 100px;
background: red;
position: absolute; }
</style>
</head>
<body> <div id="box">模拟拖拽</div>
<script>
var oBox = document.getElementById('box'); oBox.onmousedown = function(ev){
// 鼠标按下 var ev = ev || event; // 获取鼠标离div得距离
var mouseBoxleft = ev.clientX - this.offsetLeft;
var mouseBoxTop = ev.clientY - this.offsetTop; // IE浏览器,全局捕获
if(oBox.setCapture){
oBox.setCapture();
} document.onmousemove = function(ev){
// 鼠标按下左键并移动 var ev = ev || event; // 设置div移动时,它的位置
oBox.style.left = ev.clientX - mouseBoxleft + 'px';
oBox.style.top = ev.clientY - mouseBoxleft + 'px'; } document.onmouseup = function(){
// 鼠标左键抬起 document.onmousemove = document.onmouseup = null; //IE下,释放全局捕获 releaseCapture();
if ( oBox.releaseCapture ) {
oBox.releaseCapture();
}
} // 阻止默认行为
return false;
}
</script>
</body>
</html>

  

  【3】封装模拟拖拽函数

效果:点击查看

 代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box{
width: 100px;
height: 100px;
background: red;
position: absolute; }
</style>
</head>
<body> <div id="box">模拟拖拽</div>
<script>
var oBox = document.getElementById('box'); drag(oBox); function drag(obj){
obj.onmousedown = function(ev){
// 鼠标按下 var ev = ev || event; // 获取鼠标离div得距离
var mouseBoxleft = ev.clientX - this.offsetLeft;
var mouseBoxTop = ev.clientY - this.offsetTop; // IE浏览器,全局捕获
if(obj.setCapture){
obj.setCapture();
} document.onmousemove = function(ev){
// 鼠标按下左键并移动 var ev = ev || event; // 设置div移动时,它的位置
obj.style.left = ev.clientX - mouseBoxleft + 'px';
obj.style.top = ev.clientY - mouseBoxleft + 'px'; } document.onmouseup = function(){
// 鼠标左键抬起 document.onmousemove = document.onmouseup = null; //IE下,释放全局捕获 releaseCapture();
if ( obj.releaseCapture ) {
obj.releaseCapture();
}
} // 阻止默认行为
return false;
}
} </script>
</body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901