Solved

JQuery timed image swap

Posted on 2010-09-12
6
1,158 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now