Solved

JQuery timed image swap

Posted on 2010-09-12
6
1,162 Views
Last Modified: 2012-05-10
Hello experts!

Can someone tell me how I can swap an image with another for a set amount of time when a button is clicked using JQuery. I've managed to show an image for a set amount of time but swapping has deluded me? Thanks
<script type="text/javascript">
$(document).ready(function(){
	$('#click_to_load').click(function(){
		$('#loader').show(0).delay(500).hide(0);
		return false;
	});
});
</script>

Open in new window

0
Comment
Question by:allanch08
  • 3
  • 3
6 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 33657497
It would be easier if corresponding HTML was also posted. What is #loader? A way would be to have two of those and show one and hide another after the set delay.

<script type="text/javascript">
$(document).ready(function(){
      $('#click_to_load').click(function(){
            $('#loader1').hide(0).delay(500).show(0);
            $('#loader2').show(0).delay(500).hide(0);
            return false;
      });
});
</script>
0
 

Author Comment

by:allanch08
ID: 33657520
Hello,

#loader is the id of the div containing the image. so basically clicking the button with id of click_to_load shows the div with id ="loader".
<div id="loader">
    <img src="images/loading.gif" alt="Loading...">
</div><!-- end div -->

Open in new window

0
 
LVL 58

Expert Comment

by:amit_g
ID: 33657619
<div id="loader">
    <img id="img1" src="http://www.google.com/images/firefox/firefox1.png" alt="Loading...">
    <img id="img2" src="http://www.google.com/images/firefox/sync-addon.png" alt="Loading..." style="display:none">
</div><!-- end div -->


<script type="text/javascript">
$(document).ready(function(){
      $('#click_to_load').click(function(){
            $('#loader')..show(0);
            setTimeout("$('#loader img').toggle();", 3000);
            return false;
      });
});
</script>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:allanch08
ID: 33657821
thanks, can you tell me how to get it to revert back to the original image again after the second image is shown?
0
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 33658807
Toggle again...

<script type="text/javascript">
$(document).ready(function(){
      $('#click_to_load').click(function(){
            $('#loader')..show(0);
            setTimeout("$('#loader img').toggle();", 3000);
            setTimeout("$('#loader img').toggle();", 6000);
            return false;
      });
});
</script>
0
 

Author Comment

by:allanch08
ID: 33659834
cool, thanks for your help amit_g! much appreciated!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question