Solved

jQuery animation overlap

Posted on 2013-10-28
15
410 Views
Last Modified: 2013-10-29
Hi Guys,

I am not sure where to use/place the function stop();.

In short...

I have 4 content/text slides and I  simply show/hide each slide when the user click the "next" button. The "current" css is simply switching the slide from dispaly:none to dispaly:block.

When I keep clicking the button "next" fast the slides are overlapping.. meaning i see the last slide and the next slide text all together.

I know I should use the stop function yet i am not sure where to place it as everywhere i tried placing it.. it just got worse.

Thanks!

$("#next").click(function() {
		
		var theSlides = $(".lage-content-slide");
		$this = $("#lage-left .current");
		var lastSlide = $(".lage-content-slide").last();
		
		if ($this.is(lastSlide)) {
			theSlides.fadeOut().removeClass("current");			
			$(".lage-content-slide:eq(0)").fadeIn(600).addClass("current");
		} else {		
			$this.fadeOut(600, function() {			
			$this.toggleClass("current");			
		})
		$this.next(theSlides).fadeIn(600).toggleClass("current");		
		}	
	});

Open in new window

0
Comment
Question by:Refael
  • 7
  • 7
15 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39606520
Try
$this.finish().next(theSlides).fadeIn(600).toggleClass("current");
0
 

Author Comment

by:Refael
ID: 39606543
GaryC123 Hi,

That's fine but the fade effect is gone.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39606556
Ok move it to this
$this.finish().fadeOut(600, function() {
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Refael
ID: 39606577
Now it works fine until the first cycle meaning it goes back to the first slide.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39606606
Add here as well
theSlides.finish().fadeOut().removeClass("current");
0
 

Author Comment

by:Refael
ID: 39606641
the same issue. something is wrong when it rolls back to the first slide.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39606662
Got a link to the page
0
 

Author Comment

by:Refael
ID: 39606746
I am trying to add it to  jsfiddle but it does not work on my end.
This is offline i am working locally.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39606777
Can you post your function as you have it now again.
0
 

Author Comment

by:Refael
ID: 39606785
$("#lage-content-slide-nav").click(function() {
		
		var theSlides = $(".lage-content-slide");
		$this = $("#lage-left .current");
		var lastSlide = $(".lage-content-slide").last();
		
		if ($this.is(lastSlide)) {
			theSlides.finish().fadeOut(400).removeClass("current");					
			$(".lage-content-slide:eq(0)").fadeIn(600).addClass("current");
		} else {		
			$this.finish().fadeOut(400, function() {			
			$this.toggleClass("current");			
		})
		//$this.next(theSlides).fadeIn(600).toggleClass("current");
		$this.next(theSlides).fadeIn(600).toggleClass("current");			
		}	
	});
	
});

Open in new window

0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 39606806
There are a couple of ways to solve this.

1. Disable the next button until all animations have completed.
2. Add the stop function before starting an animation with clearQueue false and jumpToEnd true
$("#next").click(function(e) {
	e.preventDefault();
	$(".lage-content-slide").stop(false, true);
	var theSlides = $(".lage-content-slide");
	$this = $("#lage-left .current");
	var lastSlide = $(".lage-content-slide").last();
	
	if ($this.is(lastSlide)) {
		theSlides.fadeOut().removeClass("current");			
		$(".lage-content-slide:eq(0)").fadeIn(600).addClass("current");
	} 
	else {		
		$this.fadeOut(600, function() {			
			$this.toggleClass("current");			
		});
		$this.next(theSlides).fadeIn(600).toggleClass("current");		
	}	
});

Open in new window


The other alternative is to use a different approach - something like this
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
    $('#next').click(function(e) {
        e.preventDefault();
        $('.slide').stop(false, true);
        $('.slide:visible').fadeOut(2000, function() {
            if ($(this).is($('.slide').last())) {
                $('.slide').first().fadeIn(2000);
            }
            else {
                $(this).next().fadeIn(2000);
            }
        });
    });
});

</script>
<style type="text/css">
.slide {
    display: none;
    height: 400px;
    width: 200px;
}
</style>
</head>
<body>
    <div class="slide" style="display: block">
        Slide one
    </div>
    <div class="slide">
        Slide two
    </div>
    <div class="slide">
        Slide three
    </div>
    <div class="slide">
        Slide four
    </div>
    <a href="#" id="next">Next</a>
</body>
</html>

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39607204
Try this

if ($this.is(lastSlide)) {
$this.finish()
...
0
 

Author Comment

by:Refael
ID: 39607745
Hi GaryC123,

Thank you! your solution above seem to solve the issue. Here is the code below.
Also, your "different approach" solution is also working just fine yet i am happyi  could keep my code :-)

Again.... Thank you!!!

if ($this.is(lastSlide)) {
   $this.finish();
   theSlides.fadeOut(400).removeClass("current");
   ............

Open in new window

0
 

Author Closing Comment

by:Refael
ID: 39607748
Read my comment above. Thank you GaryC123!
0
 
LVL 58

Expert Comment

by:Gary
ID: 39608852
NP
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jquery progress bar 3 41
How do you setTimeout in JQuery? 1 22
How to show span when clicked on? 10 22
alert(innerHTML); 8 18
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…
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 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)

777 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