0

I use the following script to move elements in a canvas randomly:

function makeNewPosition(){
    
    var h = $(window).height() - 50;
    var w = $(window).width() - 50;
    
    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);
    
    return [nh,nw];    
    
}

function animateDiv(element){
    
    var newq = makeNewPosition();
    $(element).animate({ top: newq[0], left: newq[1] }, 5000, function(){
        animateDiv(element);        
    });
    
};

Now i need to immediately stop the animation when clicking a button. How is this possible?

public9nf
  • 1,311
  • 3
  • 18
  • 48
  • 1
    https://api.jquery.com/stop/ – Calvin Nunes Jun 17 '21 at 17:48
  • $(".buttonClassName").click( function(event) { event.preventDefault(); console.log('button was clicked'); //add code here to stop animation } ); Here's a hint how to stop the animation with .stop https://stackoverflow.com/a/2858013/3825777 Here's a stack snippet showing how buttons work https://stackoverflow.com/a/5470762/3825777 – react_or_angluar Jun 17 '21 at 18:11

0 Answers0