在做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. SSO单点登录三种情况的实现方式详解(转载)

    单点登录(SSO——Single Sign On)对于我们来说已经不陌生了.对于大型系统来说使用单点登录可以减少用户很多的麻烦.就拿百度来说吧,百度下面有很多的子系统——百度经验.百度知道.百度文库等 ...

  2. SqlServer获取字符串中数字,中文及字符部分数据

    --获取英文字符数据 Create function [dbo].[Fun_GetChar] ( ) ) ) AS BEGIN BEGIN ,'') --删掉一个非数字的字符,循环结束,剩余的为数字部 ...

  3. 【node错误】/usr/bin/env: node: No such file or directory

    背景 安装了node后,执行npm run xxx的命令的时候,报错,提示如下: /usr/bin/env: node: No such file or directory 步骤 1. 什么玩意,执行 ...

  4. JAVA特性一:封装

    封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 封装概念详解:封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口. 面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完 ...

  5. 重新使用Eclipse建立安卓工程遇到的问题

    很早之前用过Eclipse建立安卓工程,很久没用了,最近打算用Eclipse开发安卓程序,我是用谷歌提供的Eclipse集成环境建立的安卓工程,发现有了一些变化,而且遇到一点问题,这几天不断学习,终于 ...

  6. JavaScript多个音频audio标签,点击其中一个播放时,其他的停止播放

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 使用java中for循环,循环打印出五角星--

    //5.0 输出五角星        int touHigh = 6;        int jianHigh = 25 ;        int kuang =50;        for (int ...

  8. kali linux之Audacity

    常用音频隐写工具 安装: sudo apt install audacity 初次打开的界面 看波的宽度分辨长短音 比较细的就是短音,代表".",比较粗的就是长音,代表" ...

  9. SVN教程 -- 基于自己学习记录

    SVN教程 -- 基于自己学习记录 1. 概述 a. 什么是SVN? Apache Subversion 通常被缩写成 SVN,是一个开放源代码的版本控制系统.相较于 git ,svn 是集中式版本控 ...

  10. Pangolin库的使用

    使用Pangolin画出相机的轨迹(包括朝向). 数据集结构data.csv: #timestamp, p_RS_R_x [m], p_RS_R_y [m], p_RS_R_z [m], q_RS_w ...