What is the error of this code? the animation should only work once

Asked

Viewed 18 times

0

I have several elements with the class. track, when anyone is clicked the div #Controls appears with this animation, but I need it to occur only once. I tried several ways from the internet but could not, one that came closer did not repeat when clicking on the same element with the class, but clicking on another element that repeats. Here:

       $(document).ready(
function(){
    $(".track").one("click", function () {
        $("#controls").fadeIn()
        .css({bottom:-50,position:'absolute'})
        .animate({bottom:0}, 500, function() {
        });
    });
});

1 answer

0


The easiest way to solve this problem is by using a global variable, indicating whether you have already performed the animation or not. In this example:

var animado = false;
$(document).ready(
function(){
    $(".track").one("click", function () {
        if (animado) {
            return
        }
        animado = true;        
        $("#controls").fadeIn()
        .css({bottom:-50,position:'absolute'})
        .animate({bottom:0}, 500, function() {});
    });
});

So it will only run the animation the first time.

  • Friend, thank you so much for the answer! but I tested it here and it didn’t work (the script in general)... it worked there for you?

  • Sorry, I just forgot to add a parenthesis. Try again :p

  • Perfect friend!!! muuuuuito thanks xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.