Solved

jquery delay and cycle

Posted on 2012-03-27
10
713 Views
Last Modified: 2012-03-28
Hello Experts,

On a page i have a slideshow. I want to add text to serve as a caption of the image.
In total there are only two photo so this should not be a long script.....

What i did using my very basic knowledge in jquery is simply use show/hide with the delay.

Like so

        $('#caption1').fadeIn(5000);
	$('#caption1').delay(12000).fadeOut(500);

        $('#caption2').fadeIn(5000);
	$('#caption2').delay(12000).fadeOut(500);	

Open in new window


You see in need to follow the time of the photo as they appear and cycle.
- when the first photo appear show caption1
- when the second photo appears hide caption1 and show caption2
- cycle the same timing... first photo appears again.....

Please if someone can help?
0
Comment
Question by:Refael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37772677
something like

var total = $("[id^='caption']").size();
function showcaption(id){
$("#"+id).fadeIn(5000, function(){
   setTimeout("getNextId(" + id + ")", 12000);
});
}

function getNextId(id)
{
  $("#"+id).fadeOut(2000, function(){
   if ( $("#"+id).index() == size()-1 )
   {
        id = $( "[id^='caption']").first().attr("id");
   }
   else
   {
        id = $("#"+id).next( "[id^='caption']").attr("id");
   }
   setTimeout("showcaption(" + id + ")", 12000);
});
}
0
 
LVL 7

Expert Comment

by:Lalit Chandra
ID: 37772768
you can do something like following


<html>
<head>
<script script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(function() {
     $("#divImage-Slide div:first").hide();
     setInterval("SwapImageWithCaption()", 1000);
});
function SwapImageWithCaption()
{
    $("#divImage-Slide>div").toggle()
}
</script>      
</head>
<body>
<div id="divImage-Slide">
   <div id="divImage1">
       <img src="Desert.jpg" height="308px" width="308px" >     
       <div id="divCaption1">Caption 1 - Desert</div>
   </div>
   <div id="divImage2">
        <img src="Chrysanthemum.jpg" height="308px" width="308px">      
        <div id="divCaption1">Caption 2- Chrysanthemum</div>
   </div>
</div>

</body>
</html>

hope this will solve your problem.
0
 

Author Comment

by:Refael
ID: 37772868
Hi again,

gurvinder372, I tried your solution. nothing happens.

Lalit-Chandra, I am only looking for the solution on the caption not the slideshow.

I will set the time corresponding the two photo.

All i am after is the option to set a time for the "caption" 1 to appear then fade... the the time for "caption2" then hide... then to cycle the same function (show/hide) in the same timing.

thanks again guys!
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 7

Expert Comment

by:Lalit Chandra
ID: 37772922
Ok. you want the show/hide effect only on the caption not with the image.So, in that scene you can simple remove the image from the sample i have presented. It will start the animation with caption only.
0
 

Author Comment

by:Refael
ID: 37773896
Hi Lalit-Chandra,

Thank you! for some reason the 2nd caption is displaying first. I can set the time, wonderful.
Can i add fadeIn, fadeOut or slideLeft SlideRight?
0
 

Author Comment

by:Refael
ID: 37773959
Hi Lalit-Chandra,

as in my previous reply, again, thank you!

here is the code below, as i set it.

the second caption gets to show first, do you know why?

Can i add fadeIn, fadeOut or slideLeft SlideRight?

$(function() {
     $("#captions div:first").hide();
     setInterval("SwapImageWithCaption()", 5000);
});
function SwapImageWithCaption()
{
    $("#captions>div").toggle()
}

Open in new window



<div id="captions">
   <div id="caption-1">Caption 1 - Desert</div>
   <div id="caption-2">Caption 2- Chrysanthemum</div>
</div>

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37774984
check this simple example code too

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<script src="jquery-1.4.2.min.js"></script>
	<script>
		$(document).ready(function(){
			$("#captions div").hide();

			var id = $("#captions div").first().attr("id");

			showcaption(id);
		});

		function showcaption(id){
			$("#"+id).fadeIn(1000, function(){
			   setTimeout("getNextId('" + id + "')", 120);
			});
		}

		function getNextId(id)
		{
			$("#"+id).fadeOut(2000, function(){

				id = $("#"+id).next( "[id^='caption']").attr("id");
				if ( id == undefined || id == null )
				{
					id = $("#captions div").first().attr("id");
				}
				setTimeout("showcaption('" + id + "')", 1000);
			});
		}
	</script>
</HEAD>

<BODY>
	<div id="captions">
	   <div id="caption-1">Caption 1 - Desert</div>
	   <div id="caption-2">Caption 2- Chrysanthemum</div>
	   <div id="caption-3">Caption 3 - Desert 2</div>
	   <div id="caption-4">Caption 4- Chrysanthemum 2</div>
	</div>
</BODY>
</HTML>

Open in new window

0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 500 total points
ID: 37775344
hello

the second caption is showing first as because i have make the first caption hidden in my code at the first time.
To make 1 caption visible , do some script change

        $("#captions div:first").hide();
To

        $("#divImage-Slide>div:last").hide();

The above will make frist caption visible first.

As far as Fade In/Out effect is concerned,you have to do some more changing's to achieve that.you take a review of the below code...


<html>
<head>
<script script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(function() {
$("#divImage-Slide>div:last").hide();
setInterval("SwapImageWithCaption()", 1000);
     
});
function SwapImageWithCaption()
{
$("#divImage-Slide>div").fadeToggle()
}

</script>      

</head>

<body>

<div id="divImage-Slide" style=" width: 100px;height: 100px;position: relative;">
   <div id="divImage1" style=" width: 100%;height: 100%;position: absolute;top: 0;left: 0;">
       <img src="Desert.jpg" height="308px" width="308px" >     
       <div id="divCaption1">Caption 1 - Desert</div>
   </div>
   <div id="divImage2" style=" width: 100%;height: 100%;position: absolute;top: 0;left: 0;">
        <img src="Chrysanthemum.jpg" height="308px" width="308px">      
        <div id="divCaption1">Caption 2- Chrysanthemum</div>
   </div>
</div>

</body>
</html>
         


Hope this will do your required needs.
0
 

Author Closing Comment

by:Refael
ID: 37775625
Thank you so much! I have learned!
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37775823
did you tried my solution?
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
This article discusses how to implement server side field validation and display customized error messages to the client.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

705 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