Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery animation overlap

Posted on 2013-10-28
15
Medium Priority
?
419 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 60

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

963 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