Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery animation overlap

Posted on 2013-10-28
15
Medium Priority
?
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 

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 59

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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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

661 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