Created
February 3, 2011 14:44
-
-
Save davidpadbury/809551 to your computer and use it in GitHub Desktop.
jQuery Hover example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $(fn) shorthand for $(document).ready(fn) | |
$(function() { | |
// Hover is a handy $ function that takes two functions, the first for when the mouse goes in and the other for when it goes out | |
$('.navcontainer li').hover(function() { | |
// this in a $ event handler will refer to the element raising the event | |
$('.' + this.id).addClass('thecolor'); | |
}, function() { | |
$('.' + this.id).removeClass('thecolor'); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $(fn) shorthand for $(document).ready(fn) | |
$(function() { | |
// Generates a function to class {x}Class methods on elements with a class matching the id | |
var colorRelated = function(n) { | |
// Note how we're returning a function here, which will in turn be executed by $ | |
return function() { $('.' + this.id)[n + 'Class']('thecolor') } | |
}; | |
// Hover is a handy $ function that takes two functions, the first for when the mouse goes in and the other for when it goes out | |
$('.navcontainer li').hover(colorRelated('add'), colorRelated('remove')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment