Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute jquery animate only one time

I've the following function:

$('.link1').click(function(){
    $("#div2").slideUp(function(){$("#div1").slideToggle();});
    $('html, body').animate({scrollTop: '600px'}, 800);
});

It toggles a div and scroll the page down. The problem is that everytime the user toggles the page scroll down again...how could I run this animate function only at first click?

like image 322
fackz Avatar asked Jul 27 '12 15:07

fackz


People also ask

Which jQuery method is used to stop an animation before it is finished?

The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations. Syntax: $(selector).

Is jQuery good for animation?

The jQuery library provides several techniques for adding animation to a web page. These include simple, standard animations that are frequently used, and the ability to craft sophisticated custom effects.

What is the use of the animate () method in jQuery?

The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").

What is the correct syntax to call the jQuery animate () function?

The jQuery animate() method is used to create custom animations. Syntax: $(selector).animate({params},speed,callback);


1 Answers

Use a flag or set a data attribute to make sure the scrolling animation only occurs on the first click.

var flag=true;

$('.link1').click(function(){
    $("#div2").slideUp(function(){$("#div1").slideToggle();});
    if (flag) {
        $('html, body').animate({scrollTop: '600px'}, 800);
        flag = false;
    }
});

I'm guessing #div2 should still toggle, but that it just should'nt scroll on every click?

like image 84
adeneo Avatar answered Sep 21 '22 03:09

adeneo