Solved

jQuery animation overlap

Posted on 2013-10-28
15
413 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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 56

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

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

733 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