.hover()
Syntax
.hover(handlerIn, handlerOut)
Parameters
handlerIn
- A function to execute when the mouse pointer enters the element
handlerOut
- A function to execute when the mouse pointer leaves the element
Return value
The jQuery object, for chaining purposes.
Description
Bind two handlers, to be executed when the mouse pointer enters and leaves the elements.
$obj.hover(handlerIn, handlerOut) is shorthand for:
$obj.mouseenter(handlerIn);
$obj.mouseleave(handlerOut);
Hover action
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("b").hover(function () {
$(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
}, function () {
var cssObj = {
'background-color' : '#ddd',
'font-weight' : '',
'color' : 'red'
}
$(this).css(cssObj);
});
});
</script>
</head>
<body>
<body>
<b>Hello</b>
</body>
</html>
Hover to add and remove class
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").hover(
function () {
$(this).addClass("my");
},
function () {
$(this).removeClass("my");
}
);
});
</script>
<style>
.my { color:blue; }
</style>
</head>
<body>
<body>
<div>header 1</div>
</body>
</html>
Hover to hide tag
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").hover(
function () {
$(this).append($("<span> ***</span>"));
},
function () {
$(this).find("span:last").remove();
}
);
});
</script>
</head>
<body>
<body>
<div><h1>header 1</h1></div>
</body>
</html>
Hover to fade in and out
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
});
</script>
</head>
<body>
<body>
<div><h1>header 1</h1></div>
</body>
</html>
Whenever mouse cursor is moved over a matched element, the first specified function is fired.
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("li").hover(
function () {
$(this).append($("<span>*</span>"));
},
function () {
$(this).find("span:last").remove();
}
);
});
</script>
</head>
<body>
<body>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
</body>
</html>
Home
JavaScript Book
jQuery
JavaScript Book
jQuery
Event:
- jQuery Event
- jQuery's methods for event
- Event object
- event.keycode
- event.clientX/clientY
- event.pageX/pageY: click event coordinates
- event.preventDefault()
- event.stopPropagation(): Stop only an event from bubbling by using the stopPropagation method.
- event.target.tagName
- event.which:check key code
- return false to Cancel a default action and prevent it from bubbling up
- bind
- .blur()
- .change()
- .click()
- .error()
- .dblclick()
- .delegate()
- die:Removes a bound live event
- .focus()
- .hover()
- keydown() event
- .keypress()
- keyup event and check the key code
- .live()
- .load()
- mousedown() event
- mouseenter() event
- mouseleave
- mousemove()
- mouseover() event
- mouseout
- mouseup() event
- .off() removes events
- .on() replaces the functionality of all the event methods.
- .one() method executes handler only once.
- .ready()
- .resize()
- .scroll()
- .select()
- .submit()
- .toggle()
- .trigger()
- .triggerHandler()
- .unbind() accepts a string describing the event type to unbind.
- .undelegate() removes the binding
- .unload()
- use bind/trigger to create custom event