Solved

JQuery fade and change order

Posted on 2014-02-24
5
270 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 54

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 54

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 54

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

776 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