day50—JavaScript鼠标拖拽事件
转行学开发,代码100天——2018-05-05
今天通过鼠标拖拽事件复习巩固一下鼠标事件。
鼠标拖拽事件需要记住两点:
1.距离不变
2.鼠标事件(按下,移动,抬起)
<div id="div1"></div>
<style type="text/css">
#div1{
width: 100px;
height: 100px;
background: red;
position: absolute;
}
</style>
JavaScript事件:
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById("div1");
var disX = 0;
var disY = 0;
//鼠标按下
oDiv.onmousedown = function(ev){
var oEvent = ev||event;
disX = oEvent.clientX-oDiv.offsetLeft;
disY = oEvent.clientY-oDiv.offsetTop;
//鼠标移动
oDiv.onmousemove = function(ev){
var oEvent = ev||event;
oDiv.style.left = oEvent.clientX-disX+"px";
oDiv.style.top = oEvent.clientY-disY+"px";
};
oDiv.onmouseup = function(){
oDiv.onmousemove = null;
oDiv.onmouseup = null;
}
};
}
</script>
注意:这里的鼠标的移动事件是在鼠标按下之后发生;鼠标抬起时需要关闭鼠标移动事件和它本身。
这时可以实现一个基本的拖拽效果。但是,当鼠标快速移动时,仍然有个问题:当鼠标移到div盒子外面时盒子被鼠标甩掉了。。
究其原因在于鼠标事件的添加对象只是div盒子,盒子很小时会发生上述情况。因此可以将事件添加对象升级到document上。
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById("div1");
var disX = 0;
var disY = 0;
//鼠标按下
document.onmousedown = function(ev){
var oEvent = ev||event;
disX = oEvent.clientX-oDiv.offsetLeft;
disY = oEvent.clientY-oDiv.offsetTop;
//鼠标移动
document.onmousemove = function(ev){
var oEvent = ev||event;
oDiv.style.left = oEvent.clientX-disX+"px";
oDiv.style.top = oEvent.clientY-disY+"px";
};
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
};
}
</script>
尽管如此,在火狐浏览器下拖拽空DIV盒子会出现重影,因此需要阻止默认事件,在鼠标按下事件中添加
return false;
此外,通过学习事件捕获,IE转属的setCapture()方法,可以避免使用事件对象升级到document。同时需要在鼠标抬起事件中,取消事件捕获releaseCapture();
最后,为了提升用户体验,还可以对上述拖拽事件进行更进一步的优化,即对拖拽范围添加限制。
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById("div1");
var disX = 0;
var disY = 0;
//鼠标按下
document.onmousedown = function(ev){
var oEvent = ev||event;
disX = oEvent.clientX-oDiv.offsetLeft;
disY = oEvent.clientY-oDiv.offsetTop;
//鼠标移动
document.onmousemove = function(ev){
var oEvent = ev||event;
var l = oEvent.clientX-disX;
var t = oEvent.clientY-disY; if (l<0) {
l =0;
}else if(l>document.documentElement.clientWidth-oDiv.offsetWidth)
{
l = document.documentElement.clientWidth-oDiv.offsetWidth;
} if (t<0) {
t =0;
}else if(t>document.documentElement.clientHeight-oDiv.offsetHeight)
{
t = document.documentElement.clientHeight-oDiv.offsetHeight;
} oDiv.style.left = l+"px";
oDiv.style.top = t+"px";
};
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
};
return false;
};
}
</script>

添加事件捕获后优化代码如下:
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById("div1");
var disX =0;
var disY =0;
oDiv.onmousedown = function(ev){
var oEvent = ev||event;
disX = oEvent.clientX-oDiv.offsetLeft;
disY = oEvent.clientY-oDiv.offsetTop;
if (oDiv.setCapture) {
//鼠标移动事件
oDiv.onmousemove = mouseMove;
oDiv.onmouseup = mouseUp;
oDiv.setCapture();//ie专用
return false;
}else{
//鼠标移动
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
return false;
}
};
//鼠标抬起事件
function mouseUp(){
this.onmousemove = null;
this.onmouseup = null;
if (this.setCapture) {
this.releaseCapture();//ie专用
}
};
//鼠标移动事件
function mouseMove(ev){
var oEvent = ev||event;
var l = oEvent.clientX-disX;
var t = oEvent.clientY-disY;
if (l<0) {
l =0;
}else if(l>document.documentElement.clientWidth-oDiv.offsetWidth)
{
l = document.documentElement.clientWidth-oDiv.offsetWidth;
}
if (t<0) {
t =0;
}else if(t>document.documentElement.clientHeight-oDiv.offsetHeight)
{
t = document.documentElement.clientHeight-oDiv.offsetHeight;
}
oDiv.style.left = l+"px";
oDiv.style.top = t+"px";
};
}
</script>
day50—JavaScript鼠标拖拽事件的更多相关文章
- javascript鼠标拖拽的那些事情
<html> <head> <title>javascript鼠标拖拽的那些事情</title> <meta http-equiv="C ...
- 完美实现鼠标拖拽事件,解决各种小bug,基于jquery
鼠标拖拽事件是web中使用频率极高的事件,之前写过的代码包括网上的代码,总存在各种各样的问题,包括拖拽体验差,松开鼠标后拖拽效果仍存在以及代码冗余过大等 本次我才用jQuery实现一个尽可能高效的拖拽 ...
- JS Event 鼠标拖拽事件
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- JavaScript鼠标拖拽特效及相关问题总结
#div1{width:200px;height:200px;background:red;position:absolute;} #div2{width:200px;height:200px;bac ...
- HTML5深入学习之鼠标跟随,拖拽事件
知识点(鼠标跟随): mousedown: 当用户用鼠标点击在某一元素上就会触发该事件 mouseover: 当鼠标指针在某一元素上移动就会触发改事件 下面这个例子的效果就是鼠标点击元素后,元素跟着 ...
- day52—JavaScript拖拽事件的应用(自定义滚动条)
转行学开发,代码100天——2018-05-07 前面的记录里展示了JavaScript中鼠标拖拽功能,今天利用拖拽功能实现另一个应用场景——自定义滚动条(作为控制器)的用法. 常通过自定义滚动条控制 ...
- 鼠标拖拽定位和DOM各种尺寸详解
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- html5拖拽事件 xhr2 实现文件上传 含进度条
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- JavaScript实现拖拽元素对齐到网格(每次移动固定距离)
这几天在做一个拖拽元素的附加功能,就是对齐到网格,实际上就是确定好元素的初始位置,然后拖拽元素时,每次移动固定的距离.让元素都可以在网格内对齐.先上效果图,然后在详细说明一下细节问题 做了一个gif图 ...
随机推荐
- HNUSTOJ 1601:名字缩写
1601: 名字缩写 时间限制: 1 Sec 内存限制: 128 MB 提交: 288 解决: 80 [提交][状态][讨论版] 题目描述 Noname老师有一个班的学生名字要写,但是他太懒了,想 ...
- HNUSTOJ 1444:树的最长路径
1444: 树的最长路径 时间限制: 1 Sec 内存限制: 128 MB 提交: 18 解决: 7 [提交][状态][讨论版] 题目描述 定义:无向树中结点的路径为该结点所能到达的最远距离:无向 ...
- osi七层模型??
1.应用层:提供用户服务,例如处理应用程序,文件传输,数据管理 (HTTP.RTSP.FTP) 2.表示层:做数据的转换和压缩,加解密等 3.会话层:决定了进程间的连接建立,选择使用什么样的 ...
- Cocos2d-x-javaScript 的webSocket的代码
var WebSocket = WebSocket || window.WebSocket || window.MozWebSocket; var WebSocketManager = cc.Clas ...
- 根据日志来源的不同生成不同的index索引
使用filebeat收集系统日志,不同应用的日志,然后把这些日志传输给Logstash,再然后交由elasticsearch处理,那么如何区分不同的日志来源呢? filebeat.yml配置文件中不启 ...
- C#操作电脑多显示器设置
电脑多显示器设置 第一种方式 通过使用api函数SetDisplayConfig来设置.这种方式在某些电脑中设置有几率会导致电脑黑屏 使用代码如下: private const uint SDC_AP ...
- Android数据库使用指南(下)
前言 上面已经说了,对表进行修改,其实就是对数据库进行升级,删除表也算升级啊,反正就是发生变化,数据库就需要升级. 所以老实说其实有个地方决定了数据库的版本 public class DBHelper ...
- fastadmin 搭建到云虚拟主机
1.把public下的index.php.router.php.install.php.admin_*******.php..htaccess(伪静态文件),移到 文件夹:/htdocs 根目录 ...
- Effective C++条款05:了解C++默默编写并调用哪些函数
class Empty{}; class Empty{ Empty(){}; Empty(const Empty& rhs){}; ~Empty(){}; Empty& operato ...
- do{}while(0);里面有continue
do{}while(0);里面有continue,退出的只是do{}while(0);