Solved

JQuery fade and change order

Posted on 2014-02-24
5
272 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 56

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 56

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 56

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

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

Suggested Solutions

Title # Comments Views Activity
Tools for analysis of code 4 44
How Close unsubmited attempts 10 42
How to get text of href without any ID using Javascript 9 23
modify change color of text 9 33
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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)

679 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