Last active
December 16, 2015 09:19
-
-
Save ingowennemaring/5412106 to your computer and use it in GitHub Desktop.
Image rollover with preload and fade effect
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
<a href="#"> | |
<img src="http://dummyimage.com/254x403/ccc/000.jpg&text=254x403+-+Produkt" data-hover="http://dummyimage.com/254x403/999/000.jpg&text=254x403+-+Produkt" alt="" /> | |
</a> |
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
$('img[data-hover]').each( | |
function() { | |
var img = $(this); | |
// preload hover image | |
$('<img />').attr( 'src', img.attr('data-hover') ); | |
img.parent().hover( | |
function() { | |
$('<img />') | |
.attr( 'src', img.data('hover') ) | |
.css({ | |
position: 'absolute', | |
top: 0, | |
left: 0, | |
display: 'none' | |
}) | |
.appendTo( | |
$(this).css({ | |
position: 'relative', | |
display: 'inline-block' | |
}) | |
) | |
.fadeIn(200); | |
}, | |
function() { | |
$(this).children('img').not(':first-child').fadeOut(200, | |
function() { | |
//$(this).remove(); | |
// the remove-method works, but the more complicated solution works better with fast mouse movings | |
$(this).parent().children('img').not(':first-child').remove(); | |
} | |
); | |
} | |
); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment