Solved

JQuery fade and change order

Posted on 2014-02-24
5
267 Views
Last Modified: 2014-02-25
I have a series of absolute positioned images and the code is supposed to fade out the top image and then move it to the back, it's only running once (well running continuously but I must have something screwed up with the logic as the first image only shows on the initial load.)

	setInterval(function() { 
		$(".cycle").each(function(){
			$("img:last-child",this).fadeOut('slow', function() {
			$("img:last-child",this).insertBefore($("img:first-child",this))
			$("img:last-child",this).show()})
		})}, 3000); 

Open in new window

0
Comment
Question by:Gary
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39885046
Without seeing your markup you could try this
$(function() {
	setInterval(function() { 
		var indx = $('.cycle img:visible').index();
		$('.cycle img:eq(' + (indx++) + ')').fadeOut();
		$('.cycle img:eq(' + (indx%3) + ')').fadeIn();
	}, 3000); 
});

Open in new window

Working sample here
0
 
LVL 58

Author Comment

by:Gary
ID: 39886319
I can't use an index reference, there are multiple elements on the page that this function is working on and they all have differing number of images.

The markup is basically what you have in your sample, just absolute positioned images in a container.
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39887258
Cool - enjoying the challange.
Try this one then
$(function() {
  setInterval(function() { 
    $('.cycle').each(function() {
      $(this).find('img:first').fadeOut(function() {
        $(this).appendTo($(this).parent());
      }).next().fadeIn();
    });
  }, 3000); 
});

Open in new window

Working example here. This time with two different image boxes.
0
 
LVL 58

Author Closing Comment

by:Gary
ID: 39887451
Had to change it to this coz something was still going screwy with it and I can't be bothered to look it through, maybe something funny in my code.

  setInterval(function() { 
    $('.cycle').each(function() {
      $('img:last',this).fadeOut(function() {
        $(this).prependTo($(this).parent()).fadeIn();
      });
    });
  }, 3000); 

Open in new window

0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39887975
Interesting - tried it in all browsers including IE8 in IE7 standards mode - and seems to work - as a matter of interest what was the rest of your code so I can test here.

Thanks for the points though.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

920 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

11 Experts available now in Live!

Get 1:1 Help Now