在做R80web的时候出现一个奇怪的现象,chorme现在的版本还是存在,拖动事件有mousedown、mousemove、mouseup组成,但是首次click以及失去焦点再重新点击的时候同样会触发mousemove的事件

事件问题代码

$('#mydiv').on("mousedown",function(){
console.log("this is mousedown event");
$(document).on("mousemove",function(){
console.log("this is mousemove event");
});
$(document).off("mouseup").on("mouseup",function(){
console.log("this is mouseup event");
$(document).off("mousemove")
});
});

注:火狐版本的浏览器不会存在此问题

问题存在,但是也要解决,因为这样一来,如果页面元素上有其他的事件,就有可能会触发mousemove的事件

解决办法:

在mousedown下申明一个变量,记录鼠标的位置,然后再在mousemove里面同位置判断,如果相同说明是点击的事件,否则为move事件

代码:

$('#mydiv').on("mousedown",function(e){
var m=e.clientX,n=e.clientY;
console.log("this is mousedown event");
$(document).on("mousemove",function(e){
var mousemove = { x: e.clientX, y: e.clientY };
//如果前后位置相同说明是点击事件,否则移动事件
  if (m !== mousemove.x || n !== mousemove.y) {
console.log("this is mousemove event");
}
});
$(document).off("mouseup").on("mouseup",function(){
console.log("this is mouseup event");
$(document).off("mousemove")
});
});

测试代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试chorme浏览器版本拖动问题</title>
<script type="text/javascript" src="js/jquery-1.11.3.js"></script>
</head>
<body>
<div id="mydiv" style="width:200px;height:200px;background: red;">
<script>
$(function(){
//有问题的
$('#mydiv').on("mousedown",function(){
console.log("this is mousedown event");
$(document).on("mousemove",function(){
console.log("this is mousemove event");
});
$(document).off("mouseup").on("mouseup",function(){
console.log("this is mouseup event");
$(document).off("mousemove")
});
});
//ok的
$('#mydiv').on("mousedown",function(e){
var m=e.clientX,n=e.clientY;
console.log("this is mousedown event");
$(document).on("mousemove",function(e){
var mousemove = { x: e.clientX, y: e.clientY };
//如果前后位置相同说明是点击事件,否则移动事件
  if (m !== mousemove.x || n !== mousemove.y) {
console.log("this is mousemove event");
}
});
$(document).off("mouseup").on("mouseup",function(){
console.log("this is mouseup event");
$(document).off("mousemove")
});
});
});
</script>
</body>
</html>

问题延生:如何阻止冒泡,当我点击放大缩小图标的时候也触发拖动的事件

解决办法:

//窗口最大化的事件
$("#expander .ipn-status").on("mousedown",function(){return false}).on("click","ipn-max",function(){
//最大化的业务代码
...
})

注意:一定是要注销对应的事件才会有效果

比如这样是没有效果的:

//窗口最大化的事件
$("#expander .ipn-status").on("click","ipn-max",function(){
//最大化的业务代码
...
return false;
})

上图功能代码:

功能:

1、点击右边图标控制放大缩小,缩小后显示放大图标,放大后显示缩小图标

2、可以自由拖动事件主体,高度

$(function(){
var src_posi_Y = 0, dest_posi_Y = 0, move_Y = 0, is_mouse_down = true; //事件窗体拖拽
$("#expander").mousedown(function(e){
console.log("点击");
$("#mask").css('display','block');//开启临时遮罩层
src_posi_Y = e.pageY;
var m=e.clientX;
var n=e.clientY;
is_mouse_down = true;
$(document).on('mousemove',function(e){
var mousemove = { x: e.clientX, y: e.clientY };
if (m !== mousemove.x || n !== mousemove.y) {//如果mousemove鼠标的位置和mouseDown鼠标位置相同说明不是move事件
console.log("移动");
dest_posi_Y = e.pageY;//鼠标距离顶部的距离
var maxPosiY=$("#top").height()+$("#top_nav").height()+$("#expander").height();//间距
var mm=dest_posi_Y-maxPosiY;
if(mm<0){mm=0}
//根据距离页面顶端位置进行判断时候终止拖拽
if(dest_posi_Y<=maxPosiY){
dest_posi_Y=maxPosiY;
$("#expander").find(".ipn-max").hide()//隐藏最大化图标
$("#expander").find(".ipn-default").css("display","inline-block")//显示默认图标
}
src_posi_Y = dest_posi_Y;
$("#event").removeClass("half").removeClass("all");
$("#event").css("top",mm+"px" );
}
});
$(document).off("mouseup").on("mouseup",function(e){
console.log("弹起");
$("#mask").css('display','none');//关闭临时遮罩层
$(document).off("mousemove");
})
}); //窗口最大化的事件
$("#expander .ipn-status").on("mousedown",function(){return false}).on("click",".ipn-max",function(){//阻止冒泡
//var maxPosiY=parseFloat($("#main").height()-40)+"px";
$(this).hide();
$("#expander").find(".ipn-default").css("display","inline-block");
$("#event").removeClass("half").addClass("all");
return false;
})
//窗口还原的事件
$("#expander .ipn-status").on("mousedown",function(){return false}).on("click",".ipn-default",function(){//阻止冒泡
$(this).hide();
$("#expander").find(".ipn-max").css("display","inline-block")
$("#event").removeClass("all").addClass("half");
return false;
})
})

css

@charset "utf-8";
*{
margin:0px;
padding:0px;
}
html,body{
background-color: #f9fbf8;
height:100%
}
p{margin:;}
.lf {float:left;}
.rt {float:right;}
.clear {clear:both;}
ul,li,p,h1,h2,h3,h4,h5,h6,dl,dt,dd {
margin:0px;
padding:0px;
border:none;
list-style:none;
}
button{outline: none}
a{text-decoration: none} /*实时告警*/
.foot_nav{
height: 40px;
width: 100%;
text-align: center;
line-height: 40px;
color: #7d7a7a;
background-color:rgba(0, 0, 0, 0.05);
position: absolute;
bottom:;
z-index:;
font-size: 12px;
}
/*实时告警*/
.event{
position: absolute;
width: 100%;
color: #7d7a7a;
background-color: #ffffff;
bottom: 40px;
z-index:;
font-size: 12px;
display: none;
/* transition: top 0.3s; */
}
.event.half{
top:60% !important
}
.event.all{
top:0 !important
}
.event.default{
top:100%
}
.showEvent span{
display: block;
margin-left: 10px;
}
.showEvent{
height: calc(100% - 22px);
overflow: auto;
}
#expander{
width: 100%;
height: 25px;
background-color: #f3f3f3;
border-radius: 5px;
border: 1px solid #eee;
line-height: 25px;
}
#expander:hover{ cursor:n-resize;}
.ipn-tools>li{
display:inline-block;
vertical-align: top;
margin-left:20px;
}
#iframe{
height:100%;
} #expander .ipn-tools{
height:25px;
line-height:25px;
}
#expander .ipn-tools a{
display:inline-block;
height:100%;
text-align:center;
color: #7d7a7a;
width: 60px;
height: 24px;
border: 1px solid #8a8a8a;
background-color: #ddd;
}
#expander .ipn-tools li input{
vertical-align: middle;
margin:0px 3px 3px 0px;
} div.ipn-status{
margin-right:20px;
height: 25px;
line-height: 29px;
}
div.ipn-status>i{
display:inline-block;
padding:7px;
font-style: normal;
margin-right: 10px;
cursor: pointer;
}
div.ipn-status>i:hover{
transform: scale(1.1);
}
div.ipn-status>i.ipn-close:hover{
transform: scale(1.2);
}
div.ipn-status>i.ipn-max{
background-image: url(../image/max.png)
}
div.ipn-status>i.ipn-default{
display:none;
background-image: url(../image/min.png)
}
div.ipn-status>i.ipn-close{
transform: scale(1.1);
padding:6px;
background-image: url(../image/close.png);
margin: 0 0 1px 0px;
}

html

<div id="event" class="event half">
<div id="expander">
<h5 class="lf" style="margin-left:15px">实时告警</h5>
<ul class="ipn-tools lf">
<li>
<a href="javascript:;">清除告警</a>
</li>
<li>
<label>
<input type="checkbox" name="" value="">本次不再弹框
</label>
</li>
<li>
<label>
<input type="checkbox" name="" value="">不再接受任何告警信息
</label>
</li>
<li></li>
</ul>
<div class="ipn-status rt">
<i class="ipn-max" title="最大化"></i>
<i class="ipn-default" title="还原"></i>
<i class="ipn-close" title="关闭"></i>
<div class="clear"></div>
</div>
</div>
<div class="showEvent">
<span>事件1</span>
<span>事件2</span>
<span>事件3</span>
<span>事件4</span>
</div>
</div>

关于chrome浏览器事件拖动的bug(首次点击的时候也触发move的事件)的更多相关文章

  1. chrome浏览器模拟手机端:jquery click()点击无效解决方法

    $(".sku-wrap .ok").click(); chrome浏览器模拟手机端,在油猴插件中写JS代码,然后发现click()点击失效. 解决方法:jquery的click( ...

  2. 关于谷歌Chrome浏览器的两个Bug?

    1.网络资源下载莫名其妙的网络错误(也不支持续传?有时打开文件是损坏的?) 2.超级链接莫名的成了html下载?

  3. 赋予option元素点击事件后,点击select时却触发了option事件。如何解决?

    将select的优先级提到option之前就可以了. 方法:为select元素添加position:relative: <select class="adt" name=&q ...

  4. 父节点使用css的transform: translate(0, 0)时position:fixed在chrome浏览器中无效

    今天在做移动端的页面,无意间发现了一个Chrome浏览器下的一个bug,在使用CSS3的transform: translate(0, 0)属性对节点A进行位置转化,此时A节点下面有一个字节点B,节点 ...

  5. Chrome 浏览器垃圾回收机制与内存泄漏分析

    Chorme 浏览器中的垃圾回收和内存泄漏 垃圾回收 通常情况下,垃圾数据回收分为手动回收和自动回收两种策略. 手动回收策略,何时分配内存.何时销毁内存都是由代码控制的. 自动回收策略,产生的垃圾数据 ...

  6. Selenium +Chrome浏览器如何模拟手机操作

    Selenium +Chrome浏览器如何模拟手机操作 进入手机模式 打开谷歌浏览器,按F12,进入开发者模式,点击Toggle device toolbar,进入手机模式 设置Chrome的手机模式 ...

  7. 关闭chrome浏览器的input香蕉黄背景

    chrome浏览器input的自动完成,点击之后自动输入,input的背景会变成香蕉黄,用如下方法修复: /* Change the white to any color ;) 就是给input设置内 ...

  8. 基于 Chrome 浏览器的扩展插件来进行的安装Postman

    我会给你一个安装包,见附件.你应该下载下来,解压缩到你喜欢的位置. 打开 Chrome 浏览器的「扩展程序」 点击「加载已解压的扩展程序...」按钮,找到你刚刚下载的安装包的位置,点击确定. 你去看看 ...

  9. 关于 Chrome 浏览器中 onresize 事件的 Bug

    我在写插件时用到了 onresize 事件,在反复地测试后发现该事件在 Chrome 及 Opera(内核基本与 Chrome 相同,以下统称 Chrome)浏览器打开时就会执行,这种情况也许不能算作 ...

随机推荐

  1. android事件分发

    1). android对事件分发的顺序为:Activity--->PhoneWindow--->DecorView--->yourView; 2). android控件对事件处理的优 ...

  2. 编译语言 vs 解释语言

    编译语言 vs 解释语言 阅读:  评论:  作者:Rybby  日期:  来源:rybby.com 一直以为,编译语言的性能绝对比解释语言快,因为就理论而言,解释语言要一边解释(将脚本语言翻译成计算 ...

  3. 为方便二层升三层新增的远程方法QuerySql6()

    为了方便原来D6,D7开发的二层老程序升级为三层,新增了远程方法QuerySql6().充分地兼容原来二层SQL的写法. 1)公共方法ParamsToStr() function ParamsToSt ...

  4. Ubuntu下Jenkins(docker)配置Docker远程启动

    背景: 在做用Jenkins构建docker的操作,需要用Jenkins调用docker命令,需要先安装docker-build-step插件,然后开启docker远程访问. 默认情况下,Docker ...

  5. Xamarin.Android RelativeLayout

    初次接触Xamarin.Android. 由于国内Xamarin的资料少见,我大多参考JAVA原生代码,慢慢摸索过来. 我把摸索出来的结果广而告之,希望后来人能少走一点弯路,也希望你也能做出一份贡献. ...

  6. 如何关闭SQL进程

    --通过下面的查询得到trace ID select * from sys.traces --修改下面的@traceid参数,关闭,删除对应的trace exec sp_trace_setstatus ...

  7. JZOJ6096 森林

    题目传送门 Description ​我们定义对一棵树做一次变换的含义为:当以 1 号节点为根时,交换两个互相不为祖先的点的子树: ​一棵树的权值为对它进行至多一次变换能得到的最大直径长度: ​初始时 ...

  8. Java找那个io类-File获取功能

    package com.hxzy.IOSer;import java.io.*; /* * File 的获取功能 * */public class Demo04 { public static voi ...

  9. Substr函数助你免杀php脚本

    主要利用substr函数和url编解码 本文作者:i春秋签约作家Laimooc 1]安全狗: 新研究的php脚本木马:最新版安全狗扫描如下: 成功看到:扫描已完成,未发现网页木马以及其他威胁(开心吗, ...

  10. django基础2

    一. 使用原生sql,了解即可 使用原生sql的目的:解决一些复杂的sql不能用ORM方式写出的问题 有三种方式如下 1. extra: 结果集修改器,一种提供额外查询参数的机制 2. raw:执行原 ...