Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery slideToggle() clicked multiple times - prevent animation

I've searched around for a solution for a while, but can't seem to find it. I thought maybe using JQuery's .clearQueue() as in my fiddle, but it doe'snt work.

Here's the fiddle.

To be clear: I want the sideToggle() to execute, but as soon as it gets clicked again & the first slideToggle() hasn't finished yet, it should stop the animation of the first and slide back up. Using the .clearQueue() method it does work for two clicks, but when clicked eg three times, the box dissapears and the heigt is somehow messed up.

like image 965
jdepypere Avatar asked Jan 26 '13 17:01

jdepypere


2 Answers

Using stop() during the animation can lead to bad situations where the original height cannot be reached anymore.

Another option is to force the animation to finish before the click can fire it again:

function clickFunc() {
    var $this=$(this);
    if($this.hasClass('toggling')){return;}
    $this.addClass('toggling')
    $this.stop().slideToggle(300,function(){
        $this.removeClass('toggling');
    });
}
like image 121
Hacktisch Avatar answered Oct 01 '22 21:10

Hacktisch


Using the jquery .stop() api does the trick.

Working example.

like image 20
jdepypere Avatar answered Oct 01 '22 22:10

jdepypere