JS怎样将拖拉事件与点击事件分离?
帖子:http://bbs.csdn.net/topics/390785395?page=1#post-397369340
怎样将拖拉事件跟点击事件分离?
须要做到:拖拉时不触动点击事件
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
js拖拽组件1
</title>
<script type="text/javascript" src="Drag.js"></script>
<script type="text/javascript">
window.onload = function(){
Drag.init(document.getElementById("handle1"),document.getElementById("handle1"));//handle和dragBody都是一样的 这是就相当于是拖动handle本身
}
</script>
<style type="text/css"> </style>
</head>
<body>
<div id="handle1" style="border:1px solid red;width:150px;height:30px;position:absolute;left:100px;top:100px;" onclick="alert(2);">
拖拉我、点击我
</div>
</body>
</html>
Drag.js
var Drag={
"obj":null,
"init":function(handle, dragBody, e){
if (e == null) {
handle.onmousedown=Drag.start;
}
handle.root = dragBody;
if(isNaN(parseInt(handle.root.style.left)))handle.root.style.left="0px";
if(isNaN(parseInt(handle.root.style.top)))handle.root.style.top="0px";//确保后来可以取得top值
handle.root.onDragStart=new Function();
handle.root.onDragEnd=new Function();
handle.root.onDrag=new Function();
if (e !=null) {
var handle=Drag.obj=handle;
e=Drag.fixe(e);
var top=parseInt(handle.root.style.top);
var left=parseInt(handle.root.style.left);
handle.root.onDragStart(left,top,e.pageX,e.pageY);
handle.lastMouseX=e.pageX;
handle.lastMouseY=e.pageY;
document.onmousemove=Drag.drag;
document.onmouseup=Drag.end;
}
},
"start":function(e){
var handle=Drag.obj=this;
e=Drag.fixEvent(e);
var top=parseInt(handle.root.style.top);
var left=parseInt(handle.root.style.left);
//alert(left)
//普通情况下 left top 在初始的时候都为0
handle.root.onDragStart(left,top,e.pageX,e.pageY);
handle.lastMouseX=e.pageX;
handle.lastMouseY=e.pageY;
document.onmousemove=Drag.drag;
document.onmouseup=Drag.end;
return false;
},
"drag":function(e){//这里的this为document 所以拖动对象仅仅能保存在Drag.obj里
e=Drag.fixEvent(e);
var handle=Drag.obj;
var mouseY=e.pageY;
var mouseX=e.pageX;
var top=parseInt(handle.root.style.top);
var left=parseInt(handle.root.style.left);//这里的top和left是handle.root距离浏览器边框的上边距和左边距
var currentLeft,currentTop;
currentLeft=left+mouseX-handle.lastMouseX;
currentTop=top+(mouseY-handle.lastMouseY);
//上一瞬间的上边距加上鼠标在两个瞬间移动的距离 得到如今的上边距
handle.root.style.left=currentLeft +"px";
handle.root.style.top=currentTop+"px";
//更新当前的位置
handle.lastMouseX=mouseX;
handle.lastMouseY=mouseY;
//保存这一瞬间的鼠标值 用于下一次计算位移
handle.root.onDrag(currentLeft,currentTop,e.pageX,e.pageY);//调用外面相应的函数
return false;
},
"end":function(){
document.onmousemove=null;
document.onmouseup=null;
Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style.left),parseInt(Drag.obj.root.style.top));
Drag.obj=null;
},
"fixEvent":function(e){//格式化事件參数对象
if(typeof e=="undefined")e=window.event;
if(typeof e.layerX=="undefined")e.layerX=e.offsetX;
if(typeof e.layerY=="undefined")e.layerY=e.offsetY;
if(typeof e.pageX == "undefined")e.pageX = e.clientX + document.body.scrollLeft - document.body.clientLeft;
if(typeof e.pageY == "undefined")e.pageY = e.clientY + document.body.scrollTop - document.body.clientTop;
return e;
}
};
问题应该出在onmouseup时也调用了onclick方法。网上找了方法,当中,http://www.cnblogs.com/A_ming/archive/2013/03/08/2950346.html
不知怎样应用进来。
后来想了还有一个方法,就是加入一个公共变量,在onmousedown、onmouseup、onclick分别获取鼠标的坐标,并记录在公共变量里,做了个小样例区分他们运行的顺序,例如以下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="x" style="border:1px solid #ddd;">xxxxxxxxxxxxxxxxxxxxxxxx</div>
</body>
</html>
<script type="text/javascript">
var d = document.getElementById("x");
var shubiaoX = 0;
d.onmousedown=function(){
console.log("mousedown");
shubiaoX = event.screenX;
console.log("1:"+shubiaoX);
}
d.onmouseup=function(){
console.log("mouseup");
shubiaoX = event.screenX;
console.log("2:"+shubiaoX);
}
d.onclick=function(){
console.log("click");
shubiaoX = event.screenX;
console.log("3:"+shubiaoX);
}
</script>
发现运行的顺序为onmousedown、onmouseup、onclick
原位置点击:
mouse.html:20
mouse.html:22
mouse.html:25
mouse.html:27
mouse.html:15
拖动点击:
mouse.html:20
mouse.html:22
mouse.html:25
mouse.html:27
mouse.html:15
上面能够发现,拖动点击的mousedown后移动,mouseup与click事件鼠标坐标发生变化,且一样。
故而,能够推断鼠标的坐标来区分是拖动点击还是原地点击~
当然这个是个土的办法,假设有更好的请回复~
JS怎样将拖拉事件与点击事件分离?的更多相关文章
- (网页)HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath(转)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [前端][自定义DOM事件]不使用setTimeout实现双击事件或n击事件
使用setTimeout实现双击事件 例如,这样: let div = document.getElementById("div"); doubleClick(div, funct ...
- WebView 实现JS效果和a标签的点击事件
目前很多android app都可以显示web页面的界面,嵌入式开发,这个界面一般都是WebView这个控件加载出来的,学习该控件可以为你的app开发提升扩展性. 先说下WebView的一些优点: 可 ...
- 防止多次引入js文件导致的重复注册点击事件
前端代码中的js文件如果是动态引入的或者是某个事件操作进行注册的,那么重复的引入js文件或者多次触发注册事件会导致事件多次进行注册,造成不必要的麻烦,所以需要在每次注册之前将先前的事件进行取消,下面以 ...
- Android长按事件和点击事件问题处理,OnItemLongClickListener和OnItemClickListener冲突问题
今天在做demo时,须要设置ListView的item的长按和点击事件.OnItemLongClickListener和OnItemClickListener,然而点击事件能够实现,可是在长按操作时会 ...
- Android长按事件和点击事件 冲突问题
长按点击的时候默认会触发点击事件,android系统是优先点击的,并且没有返回值:而长按事件是有返回值的,如果返回false,两个事件都会有响应,如果返回true则只响应长按事件.
- threejs Object的点击(鼠标)事件(获取点击事件的object)
objects=[]; raycaster = new THREE.Raycaster(); mouse = new THREE.Vector2(); //监听全局点击事件,通过ray检测选中哪一个o ...
- input中blur失去焦点事件与点击事件冲突时如何解决
方法一 使用setTimeout $(function(){ $(".cy-name-input input").on({ focus:function() { $(". ...
随机推荐
- Android client和服务器JSP互传中国
出于兼容性简化.传统中国等多国语言.推荐使用UTF-8编码. 首选.我们期待Android到底应该怎么办: 在发送前,应该对參数值要进行UTF-8编码,我写了一个static的 转换函数.在做发送动作 ...
- 不一样的味道--Html和Xml解析、格式、遍历
很多其它内容查看官网:http://www.tinygroup.org TinyXmlParser一切以简单.有用.高速为主. 演示样例1:Xml字符串解析 比方,我们要解析一段Xml字符串,简单例如 ...
- 玩转Web之JavaScript(二)-----javaScript语法总结(二) 涉及Date与数组的语法
Date: document.write(document.lastModified) 网页最后一次更新时间 a=new Date(); //创建 a 为一个新的时期象 y=a.getY ear( ...
- c++日历v1.12版
////////////////////////////新增信息修改功能,未完善. #include<iostream> #include <string> #include& ...
- Cocos2d-x 3.0final 终结者系列教程04-引擎架构分析
从前,有一个跟我来Android学生,总是问我: 沉老师,为什么Android的形式被称为Activity,为什么要onCreate方法写setContentView(R.layout.main)? ...
- Appium之java API
AppiumDriver getAppStrings() 默认系统语言相应的Strings.xml文件内的数据. driver.getAppStrings(String language) 查找某一个 ...
- 腾讯2014在广州站实习生offer经验(TEG-开发背景)
研究在过去的一年是linux 什么系统编程和网络编程.比较熟悉的语言c/c++,python只写一些测试client.是后台开发类,比方前面笔面的网易CC(面完hr后挂).大概3月15号就在腾讯 jo ...
- 【iOS】Swift扩展extension和协议protocol
加上几个关节前Playground摘要码进入github在,凝视写了非常多,主要是为了方便自己的未来可以Fanfankan. Swift语法的主要部分几乎相同的. 当然也有通用的.运算符重载.ARC. ...
- target-action传值
Target-Action传值 实质就是:A页面要给B页面传值,A就提供接口出去,抓A到B内部来,A间接调用自己内部方法(相当于,A把自己内部需 要操作的方法, ...
- JSP_include指令和<jsp:include>
包括三个文件:jsp_include.jsp, static.html, two.jsp 周边环境:tomcat7.0. myeclipse10 1.jsp_include.jsp <%@ pa ...