Skip to content

Instantly share code, notes, and snippets.

@davidpadbury
Created February 3, 2011 14:44
Show Gist options
  • Save davidpadbury/809551 to your computer and use it in GitHub Desktop.
Save davidpadbury/809551 to your computer and use it in GitHub Desktop.
jQuery Hover example
// $(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');
});
});
// $(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