jQuery中mouseleave和mouseout的区别详解
很多人在使用jQuery实现鼠标悬停效果时,一般都会用到mouseover和mouseout这对事件。而在实现过程中,可能会出现一些不理想的状况。
先看下使用mouseout的效果:
<p>先看下使用mouseout的效果:</p>
<div id="container" style="width: 300px;">
<div id="title" style="cursor: pointer; background: #ccc;">使用了mouseout事件↓</div>
<div id="list" style="display: none; position: absolute; background:#fff; border: 1px solid #ccc; width: 298px;">
<div>第一行</div>
<div>第二行</div>
<div>第三行</div>
</div>
</div>
<p><script type='text/javascript'>
jQuery(document).ready(function($) {
$("#title").mouseover(function() {
var offset = $(this).offset();
$("#list").css({left: offset.left, top: offset.top+19}).show();
});
$("#container").mouseout(function() {
$("#list").hide();
});
});
</script>
第一行第二行第三行我们发现使用mouseout事件时,鼠标只要在下拉容器#list里一移动,就触发了hide(),其实是因为mouseout事件是会冒泡的,也就是事件可能被同时绑定到了该容器的子元素上,所以鼠标移出每个子元素时也都会触发我们的hide()。
从jQuery 1.3开始新增了2个mouse事件,mouseenter和mouseleave。与mouseout事件不同,只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。
我们来看一下mouseleave事件的效果:
<div id="container2" style="width: 300px;">
<div id="title2" style="cursor: pointer; background: #ccc;">使用了mouseleave事件↓</div>
<div id="list2" style="display: none; position: absolute; background:#fff; border: 1px solid #ccc; width: 298px;">
<div>第一行</div>
<div>第二行</div>
<div>第三行</div>
</div>
</div>
<script type='text/javascript'>
jQuery(document).ready(function($) {
$("#title2").mouseover(function() {
var offset = $(this).offset();
$("#list2").css({left: offset.left, top: offset.top+19}).show();
});
// 绑定mouseleave事件,效果很好
$("#container2").mouseleave(function() {
$("#list2").hide();
});
});
</script>
<p>
第一行第二行第三行mouseleave和mouseout事件各有用途,因为事件冒泡在某些时候是非常有用的
解决div mouseout事件冒泡的问题
解决的办法是,使用jquery的bind方法
如:现在有一个div对象需要监听他的鼠标事件
<div class="dpx2"><div class="dpx2_px" style="cursor:pointer;" id="searchSort">请选择排序方式↓</div>
<div class="dpx2_px_xl" id="sortList" style="display:none;position:absolute;z-index:5;">
<div><a class="sortA">按时间升序↑</a></div>
<div><a class="sortA">按时间降序↓</a></div>
<div><a class="sortA">按评论数量升序↑</a></div>
<div><a class="sortA">按评论数量降序↓</a></div>
<div><a class="sortA">按点击数升序↑</a></div>
<div><a class="sortA">按点击数降序↓</a></div>
</div>
</div>
当鼠标移动到ID为searchSort的Div上时,显示下面的div。当鼠标移出下面的div时,隐藏div
JS为:
$(function(){
var sortList = $("#sortList");
$("#searchSort").mouseover(function() {
var offset = $(this).offset();
sortList.css("left", offset.left);
sortList.css("top", offset.top+20);
sortList.show();
});
//关键的一句,绑定Div对象的mouseleave事件
sortList.bind("mouseleave", function() {
$(this).hide();
});
});
本文来源:http://www.php100.com/html/program/jquery/2013/0905/5891.html
jQuery中mouseleave和mouseout的区别详解的更多相关文章
- Jquery中attr与prop的区别详解
prop()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性,返回值是空字符串. attr()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性, ...
- 基于python中staticmethod和classmethod的区别(详解)
例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object): def foo(self,x): print "executing foo ...
- DOS批处理中%cd%与%~dp0的区别详解
转载:https://www.jb51.net/article/105325.htm DOS批处理中%cd%与%~dp0的区别详解 Windows下批处理中%cd%和%~dp0都能用来表示当前 ...
- jquery 之height(),innerHeight(),outerHeight()方法区别详解
在jQuery中,获取元素高度的函数有3个,它们分别是height(). innerHeight().outerHeight(). 与此相对应的是,获取元素宽度的函数也有3个,它们分别是width() ...
- Jquery中find与each方法使用详解
本文实例讲述了jQuery中find与each方法用法.分享给大家供大家参考.具体如下: 一.find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 图解 ...
- JavaScript 中 Property 和 Attribute 的区别详解
property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...
- jquery 中 (function( window, undefined ) {})(window)写法详解(转)
最常见的闭包 (Closure) 范式大家都很熟悉了: 123 (function() {// ...})(); 很简单,大家都在用.但是,我们需要了解更多.首先,闭包是一个匿名函数 (Anonymo ...
- C#中struct和class的区别详解
本文详细分析了C#中struct和class的区别,对于C#初学者来说是有必要加以了解并掌握的. 简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上.class是引用类型,创建 ...
- Java中的==和equals的区别详解
1.基础知识 (1)String x = "hello"; (2)String x = new String ("hello"); 第1种方式的工作机制是,首先 ...
随机推荐
- CSS 背景图片 添加 重复和定位。
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- border-color的深入理解
.className{ width:100px;height:100px; border:100px solid; border-color: red green blue orange; } 最终的 ...
- Data Science With R In Visual Studio
R Projects Similar to Python, when we installed the data science tools we get an “R” section in our ...
- moogodb 安装及简单介绍
1,安装Moogodb 因为是windows 64位操作系统,直接到官网上下载.msi文件,下载完成后点击安装,点击同意协议之后,出现下面的对话框, Choose Setup Type, 就是选择安装 ...
- 牛客网-2018年湘潭大学程序设计竞赛-F
题目链接:https://www.nowcoder.com/acm/contest/105/F 解题思路:这道题第一眼直接思路就是搜索,但想了半天没想到有什么好办法搜,然后就转成最短路写了, 因为多入 ...
- 进程间通讯:有名管道FIFO
接收端: #include <sys/stat.h> #include <sys/types.h> #include <stdio.h> #include < ...
- Eclipse环境配置与快捷命令
1.VS.Chrome.Eclipse调试命令对比: VS: F5: 继续运行 F10: 单步执行 F11: 进入函数内部 Shift + F11: 由函数内部返回调用处 Chrome: F8: 继续 ...
- SpringBoot2.0.3 + SpringSecurity5.0.6 + vue 前后端分离认证授权
新项目引入安全控制 项目中新近添加了Spring Security安全组件,前期没怎么用过,加之新版本少有参考,踩坑四天,终完成初步解决方案.其实很简单,Spring Security5相比之前版本少 ...
- HDU3966-Aragorn's Story-树链剖分-点权
很模板的树链剖分题 注意什么时候用线段树上的标号,什么时候用点的标号. #pragma comment(linker, "/STACK:102400000,102400000") ...
- 戴尔服务器H330阵列卡取消磁盘阵列教程
一:服务器开机看到ctrl+R提示,按ctrl+r进入阵列卡配置界面 二:按ctrl+N 转到PD Mgmt查看硬盘信息,确认硬盘状态:Ready 三:光标移到需配置硬盘上,按F2,选择 conver ...