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

原位置点击:

mousedown
mouse.html:20

1:169
mouse.html:22

mouseup
mouse.html:25

2:169
mouse.html:27

click
mouse.html:15

3:169

拖动点击:

mousedown
mouse.html:20

1:360
mouse.html:22

mouseup
mouse.html:25

2:473
mouse.html:27

click
mouse.html:15

3:473

上面可以发现,拖动点击的mousedown后移动,mouseup与click事件鼠标坐标发生变化,且一样。

故而,可以判断鼠标的坐标来区分是拖动点击还是原地点击~

当然这个是个土的办法,如果有更好的请回复~

JS如何将拖拉事件与点击事件分离?,布布扣,bubuko.com

时间: 2024-08-05 06:41:58

JS如何将拖拉事件与点击事件分离?的相关文章

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=&qu

HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath(转)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

[前端][自定义DOM事件]不使用setTimeout实现双击事件或n击事件

使用setTimeout实现双击事件 例如,这样: let div = document.getElementById("div"); doubleClick(div, function (event) { console.log('双击') }) function doubleClick(ele, fn) { // 省略参数合法性的判断 let event = new Event("doubleClick"); // 自定义双击事件(可以使用CustomEvent

WebView 实现JS效果和a标签的点击事件

目前很多android app都可以显示web页面的界面,嵌入式开发,这个界面一般都是WebView这个控件加载出来的,学习该控件可以为你的app开发提升扩展性. 先说下WebView的一些优点: 可以直接显示和渲染web页面,直接显示网页 webview可以直接用html文件(网络上或本地assets中)作布局 和JavaScript交互调用 网页标签的点击事件 效果:(网页顶部是JS效果滚动,4个模块可以实现点击事件,可看到信息提示) activity_main.xml <RelativeL

防止多次引入js文件导致的重复注册点击事件

前端代码中的js文件如果是动态引入的或者是某个事件操作进行注册的,那么重复的引入js文件或者多次触发注册事件会导致事件多次进行注册,造成不必要的麻烦,所以需要在每次注册之前将先前的事件进行取消,下面以按钮的注册点击事件为例进行说明: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title>

android实现双击事件暨多击事件详解

最近在多击事件上稍微研究了下,在此做简要分析,供大家吐槽参考 方法一: 将两次点击的时间间隔小于0.5s的默认为双击事件 <span style="font-family:SimSun;font-size:14px;">mBtn1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getA

【Cocos2dx】使用CCControlButton创建按钮、按钮点击事件,点击事件中的组件获取,setPosition的坐标问题

按钮不仅在游戏,在任何地方都是不可或缺却又是最基本的东西.在游戏引擎Cocos2dx中也不例外. 下面用一个例子说明Cocos2dx中如何使用按钮,同时,如果在Cocos2dx中获取层,也就是场景.舞台中的组件. 如下图,有一个按钮Clickme,被点击时候与不被点击的时间,其背景图片是不同的.其实就是资源文件夹Resource中早就被玩坏的两个图片,一张CloseNormal.png一张CloseSelected.png被拉伸后的惨状. Cocos2dx的资源文件夹在<[Cocos2dx]资源

ListActivity的Item长按事件与点击事件

一.设置长按响应事件 在onCreate方法中添加下面代码: this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.mak