Skip to content

Instantly share code, notes, and snippets.

@sakunyo
Created August 2, 2011 22:10
Show Gist options
  • Save sakunyo/1121362 to your computer and use it in GitHub Desktop.
Save sakunyo/1121362 to your computer and use it in GitHub Desktop.
js image over
(function(){
var suffix = { normal : '_no.', over : '_on.'}
$('a.over, img.over, input.over').each(function(){
var a,
img,
el = $(this).get(0),
el_name = el.nodeName.toLowerCase();
src_no,
src_on,
cache;
if( el_name === 'a' ){
a = $(this);
img = $('img',this);
}else if( el_name === 'img' || el_name === 'input' ){
img = $(this);
}
src_no = img.attr('src');
src_on = src_no.replace(suffix.normal, suffix.over);
if( el_name === 'a' ){
a.bind("mouseover focus",function(){ img.attr('src',src_on); })
.bind("mouseout blur", function(){ img.attr('src',src_no); });
}else if( el_name === 'img' ){
img.bind("mouseover",function(){ img.attr('src',src_on); })
.bind("mouseout", function(){ img.attr('src',src_no); });
}else if( el_name === 'input' ){
img.bind("mouseover focus",function(){ img.attr('src',src_on); })
.bind("mouseout blur", function(){ img.attr('src',src_no); });
}
// cache img
cache = document.createElement('img');
cache.src = src_on;
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment