欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

jquery mouseleave

shiping1 的头像

.mouseleave( handler(eventObject) )返回: jQuery

描述: 为 mouse leaves(鼠标离开) 事件绑定一个处理函数,或者触发元素上的 mouse leaves(鼠标离开) 事件。

  • 添加的版本: 1.0.mouseleave( handler(eventObject) )

    • handler(eventObject)
      类型: Function()
      每次事件触发时会执行的函数。
  • 添加的版本: 1.4.3.mouseleave( [eventData ], handler(eventObject) )

    • eventData
      类型: PlainObject
      一个对象,它包含的数据键值对映射将被传递给事件处理程序。
    • handler(eventObject)
      类型: Function()
      每次事件触发时会执行的函数。
  • 添加的版本: 1.0.mouseleave()

    • 这个方法不接受任何参数。

这个方法的前两个用法是 .bind('mouseleave', handler) 的快捷方式,第3个不带参数的用法是.trigger('mouseleave') 的快捷方式。

mouseleaveJavaScript事件是Internet Explorer专有的。。由于该事件在平时很有用,jQuery的模拟这一事件,以便它可用于所有浏览器。该事件在鼠标离开元素上时被触发。任何HTML元素都可以接受此事件。

举例来说,请看下面的HTML:

1
2
3
4
5
6
7
8
9
10
<div id="outer">
Outer
<div id="inner">
Inner
</div>
</div>
<div id="other">
Trigger the handler
</div>
<div id="log"></div>

这个事件可以绑定到任何元素:

1
2
3
$('#outer').mouseleave(function() {
$('#log').append('<div>Handler for .mouseleave() called.</div>');
});

现在当指针在Outer <div>元素上移出时,Handler for .mousedown() called.将被添加到<div id="log">。我们也可以通过点击其它元素,手动触发另一个元素上的该事件:

1
2
3
$('#other').click(function() {
$('#outer').mouseleave();
});

这些代码执行后,点击Trigger the handler同样警报显示。

mouseleave事件和mouseover的不同之处是事件的冒泡的方式。如果mouseover在这个例子中使用了,然后当鼠标指针在Inner元素上移出,该处理程序将被触发。这通常是不受欢迎的行为。另一方面, mouseleave 事件只会在绑定它的元素上被调用,而不会在后代节点上被触发。因此,在这个例子中,当鼠标离开Outer元素,而不是Inner元素时,处理器才会被触发。

例子:

当触发 mouseout 和 mouseleave 事件时,显示鼠标移出对象的次数。当鼠标移出绑定 mouseout 事件元素的子元素时,mouseout 事件同样会被触发。但是,只有在绑定 mouseleave 事件的元素上,将鼠标移出时,才会触发该事件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html>
<head>
<style>
div.out {
width:40%;
height:120px;
margin:0 15px;
background-color:#D6EDFC;
float:left;
}
div.in {
width:60%;
height:60%;
background-color:#FFCC00;
margin:10px auto;
}
p {
line-height:1em;
margin:0;
padding:0;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
 
<div class="out overout"><p>move your mouse</p><div class="in overout"><p>move your mouse</p><p>0</p></div><p>0</p></div>
 
<div class="out enterleave"><p>move your mouse</p><div class="in enterleave"><p>move your mouse</p><p>0</p></div><p>0</p></div>
 
 
<script>
var i = 0;
$("div.overout").mouseover(function(){
$("p:first",this).text("mouse over");
}).mouseout(function(){
$("p:first",this).text("mouse out");
$("p:last",this).text(++i);
});
 
var n = 0;
$("div.enterleave").mouseenter(function(){
$("p:first",this).text("mouse enter");
}).mouseleave(function(){
$("p:first",this).text("mouse leave");
$("p:last",this).text(++n);
});
 
</script>
 
</body>
</html>

Demo:


来自 http://www.css88.com/jqapi-1.9/mouseleave/


.mouseleave()


.mouseleave( handler )Returns: jQuery

Description: Bind an event handler to be fired when the mouse leaves an element, or trigger that handler on an element.

This method is a shortcut for .on('mouseleave', handler) in the first two variations, and .trigger('mouseleave') in the third.

The mouseleave JavaScript event is proprietary to Internet Explorer. Because of the event's general utility, jQuery simulates this event so that it can be used regardless of browser. This event is sent to an element when the mouse pointer leaves the element. Any HTML element can receive this event.

For example, consider the HTML:

1
2
3
4
5
6
7
8
9
10
<div id="outer">
Outer
<div id="inner">
Inner
</div>
</div>
<div id="other">
Trigger the handler
</div>
<div id="log"></div>

The event handler can be bound to any element:

1
2
3
$( "#outer" ).mouseleave(function() {
$( "#log" ).append( "<div>Handler for .mouseleave() called.</div>" );
});

Now when the mouse pointer moves out of the Outer <div>, the message is appended to <div id="log">. You can also trigger the event when another element is clicked:

1
2
3
$( "#other" ).click(function() {
$( "#outer" ).mouseleave();
});

After this code executes, clicks on Trigger the handler will also append the message.

The mouseleave event differs from mouseout in the way it handles event bubbling. If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. This is usually undesirable behavior. The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. So in this example, the handler is triggered when the mouse leaves the Outer element, but not the Innerelement.

Example:

Show number of times mouseout and mouseleave events are triggered. mouseout fires when the pointer moves out of child element as well, while mouseleave fires only when the pointer moves out of the bound element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>mouseleave demo</title>
<style>
div.out {
width: 40%;
height: 120px;
margin: 0 15px;
background-color: #d6edfc;
float: left;
}
div.in {
width: 60%;
height: 60%;
background-color: #fc0;
margin: 10px auto;
}
p {
line-height: 1em;
margin: 0;
padding: 0;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<div class="out overout">
<p>move your mouse</p>
<div class="in overout"><p>move your mouse</p><p>0</p></div>
<p>0</p>
</div>
<div class="out enterleave">
<p>move your mouse</p>
<div class="in enterleave"><p>move your mouse</p><p>0</p></div>
<p>0</p>
</div>
 
<script>
var i = 0;
$( "div.overout" )
.mouseover(function() {
$( "p:first", this ).text( "mouse over" );
})
.mouseout(function() {
$( "p:first", this ).text( "mouse out" );
$( "p:last", this ).text( ++i );
});
 
var n = 0;
$( "div.enterleave" )
.mouseenter(function() {
$( "p:first", this ).text( "mouse enter" );
})
.mouseleave(function() {
$( "p:first", this ).text( "mouse leave" );
$( "p:last", this ).text( ++n );
});
</script>
 
</body>
</html>

Demo:


普通分类: