Last active
August 29, 2015 14:12
-
-
Save psi-4ward/4d88fb265b33415f863f to your computer and use it in GitHub Desktop.
Contao jQuery Template to change the a <img src="img.jpg"> to <img src="img_hover.jpg"> on mouseover
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
<script> | |
(function($) { | |
$(document).ready(function() { | |
var fadeDuration = 500; | |
$('.hover-img').each(function(i, el) { | |
el = $(el); | |
if(el.css('position') != 'absolute') | |
el.css('position', 'relative'); | |
var org = el.find('img').first(); | |
var hover = $(org.clone()); | |
hover.attr('src', hover.attr('src').replace(/(.*)(\.[a-z]+)$/, "$1_hover$2")); | |
hover.css({ | |
"position": "absolute", | |
"display": "block", | |
"opacity": 0 | |
}); | |
hover.insertAfter(org); | |
el.mouseenter(function() { | |
hover.css({ | |
top: org.position().top, | |
left: org.position().left | |
}); | |
hover.animate({'opacity': 1}, {queue: false, duration: fadeDuration}); | |
org.animate({'opacity': 0}, {queue: false, duration: fadeDuration}); | |
}); | |
el.mouseleave(function() { | |
hover.animate({'opacity': 0}, {queue: false, duration: fadeDuration}); | |
org.animate({'opacity': 1}, {queue: false, duration: fadeDuration}); | |
}); | |
}); | |
}); | |
})(jQuery); | |
</script> |
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 class="hover-img" src="mypic.png"> | |
<div class="hover-img"><img src="anotherpic.jpg"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment