Created
September 15, 2010 11:02
-
-
Save DanielOberg/580564 to your computer and use it in GitHub Desktop.
(X)HTML 5 hover effect with prefetch.
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
// (X)HTML 5 hover effect with prefetch. | |
// By DanielO | |
// | |
// Usage: <img src="pic.png" alt="Pic" data-img-hover="hover.png" /> | |
// | |
// Just add the data-img-hover attribute to your image and your good to go. | |
// Note that data-* attributes are new to HTML 5. | |
$(function () { | |
// Hover effect | |
$('img[data-img-hover]').hover(function () { | |
var img = $(this).attr('src'); | |
$(this).attr('src', $(this).attr('data-img-hover')); | |
$(this).attr('data-img-hover', img); | |
}, function () { | |
var img = $(this).attr('src'); | |
$(this).attr('src', $(this).attr('data-img-hover')); | |
$(this).attr('data-img-hover', img); | |
}); | |
// Prefetch | |
$('img[data-img-hover]').each( | |
function () { | |
var img = new Image(); | |
$(img).attr('src', $(this).attr('data-img-hover')); | |
} | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment