转行学开发,代码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鼠标拖拽事件的更多相关文章

  1. javascript鼠标拖拽的那些事情

    <html> <head> <title>javascript鼠标拖拽的那些事情</title> <meta http-equiv="C ...

  2. 完美实现鼠标拖拽事件,解决各种小bug,基于jquery

    鼠标拖拽事件是web中使用频率极高的事件,之前写过的代码包括网上的代码,总存在各种各样的问题,包括拖拽体验差,松开鼠标后拖拽效果仍存在以及代码冗余过大等 本次我才用jQuery实现一个尽可能高效的拖拽 ...

  3. JS Event 鼠标拖拽事件

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  4. JavaScript鼠标拖拽特效及相关问题总结

    #div1{width:200px;height:200px;background:red;position:absolute;} #div2{width:200px;height:200px;bac ...

  5. HTML5深入学习之鼠标跟随,拖拽事件

    知识点(鼠标跟随): mousedown: 当用户用鼠标点击在某一元素上就会触发该事件 mouseover:  当鼠标指针在某一元素上移动就会触发改事件 下面这个例子的效果就是鼠标点击元素后,元素跟着 ...

  6. day52—JavaScript拖拽事件的应用(自定义滚动条)

    转行学开发,代码100天——2018-05-07 前面的记录里展示了JavaScript中鼠标拖拽功能,今天利用拖拽功能实现另一个应用场景——自定义滚动条(作为控制器)的用法. 常通过自定义滚动条控制 ...

  7. 鼠标拖拽定位和DOM各种尺寸详解

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. html5拖拽事件 xhr2 实现文件上传 含进度条

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. JavaScript实现拖拽元素对齐到网格(每次移动固定距离)

    这几天在做一个拖拽元素的附加功能,就是对齐到网格,实际上就是确定好元素的初始位置,然后拖拽元素时,每次移动固定的距离.让元素都可以在网格内对齐.先上效果图,然后在详细说明一下细节问题 做了一个gif图 ...

随机推荐

  1. C#索引器2 字符串作为索引号

    6.索引器   字符串作为索引号 public class Demo { private Hashtable name = new Hashtable(); public string this[st ...

  2. Linux为程序员添加了行为准则

    假如你是开发人员,如果您密切关注Linux开发,您就会知道Linux内核讨论会非常热烈.最近,LinusTorvalds承认Linux内核邮件列表(LKML)和其他Linux开发空间对许多人都是敌对的 ...

  3. AI人工智能对医疗行业有哪些巨大贡献?

    人工智能(AI)有可能显着改变医生的角色并彻底改变医学实践.这篇定性评价文章总结了过去12个月的人工智能健康研究,涉及不同的医学专业,并讨论了与这一新兴技术相关的当前优势和挑战. 医生,特别是担任领导 ...

  4. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  5. git 的add .

    git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容修改(modified)以及新文件(new),但不包括被删除的文件. git add -u :他仅监控 ...

  6. LeetCode--075--颜色分类(python)

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  7. Oracle12c RAC RMAN异机恢复

    ######################################################## #编辑pfile文件initspdb.ora vi /oracle/app/oracl ...

  8. sublime格式化

    https://nodejs.org/dist/v6.2.0/node-v6.2.0-x64.msi sublime格式化

  9. Ubuntu 16.04下使用docker部署ceph集群

    ceph集群docker部署 通过docker可以快速部署小规模Ceph集群的流程,可用于开发测试. 以下的安装流程是通过linux shell来执行的:假设你只有一台机器,装了linux(如Ubun ...

  10. 算法题常见的BUG错误(总结)

    1. 快排的partition if(l >= r) return ; int i = l, j = r; int tmp = v[i]; while(i < j) { while(i & ...