Skip to content

Instantly share code, notes, and snippets.

@ifthenelse
Created September 25, 2013 09:22
Show Gist options
  • Save ifthenelse/6697163 to your computer and use it in GitHub Desktop.
Save ifthenelse/6697163 to your computer and use it in GitHub Desktop.
jQuery script to manage rollover effect on img elements with a '.hoverable' class and two images (one with -normal and one with -hover suffix)
// init rollovers on images
imgRollover: function() {
var $app = this;
$.each($app.$hoverable, function(index, val) {
var $this = $(this);
// if the hoverable object is <img>
if (typeof $this === 'object' && $this.is("img")) {
var $obj = $this;
}
// if the hoverable object contains any <img>
if (typeof $obj === 'undefined' && !!$this.children("img").length) {
var $obj = $this.children("img");
}
if (typeof $obj !== 'undefined' && !!$obj.length) {
$.each($obj, function(index, val) {
var $img = $(this);
if (typeof $img === 'object') {
var normal_src = $img.attr("src"),
hover_src = $img.attr("src").replace("-normal.", "-hover.");
if (!!normal_src.length) {
$this.hover(function(evt) {
console.log("Over");
$img.attr("src", hover_src);
}, function(evt) {
console.log("Leave");
$img.attr("src", normal_src);
});
}
}
});
} else {
return false;
}
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment