Hello, quick question. I am trying to use either javascript, jquery, or php to make it so when I click a link, it replaces a static image on my page with another image, for 15 seconds, then reverts back to the original image. What would be the best way to go about this?
+6
A:
You could do a simple timeout for this:
$('#myLink').click(function() {
$('#myImg').attr('src', 'newImg.jpg');
setTimeout(function() { $('#myImg').attr('src', 'oldImg.jpg'); }, 15000);
});
Alternatively, if you wanted a fade, have the other image absolutely positioned, like this:
<div>
<img id="tempImg" src="tempImg.jpg" style="position:absolute; display:none; z-index: 2;" />
<img src="oldImg.jpg" />
</div>
Then jQuery like this:
$('#myLink').click(function() {
$('#tempImg').fadeIn().delay(15000).fadeOut();
});
Make sure the images have the same dimensions(for looks, this is optional), the temp image will fade in on top of the static image, wait 15 seconds, then fade out.
Nick Craver
2010-05-02 01:42:38
Vote +1. Pet peeve, however. Why the double quote on the jQuery selector, yet the single quote on the attribute parameters?
BradBrening
2010-05-02 01:46:46
@Brad - It's whatever mood my fingers were in when it was time to type that string. Fixed so it won't bug you :)
Nick Craver
2010-05-02 01:48:32
Gotcha. Good answer, nice alternative solution.
BradBrening
2010-05-02 01:54:11