简体   繁体   中英

JQuery cancel animation when animating between values

I'm using .animate() to animate between two values with a step function. Since there's no selector, I can't use .stop()

        $({ Value: 50 }).animate({ Value: 0 }, {

            duration: 1000,

            step: function (val) {

                $('#SomeElement').css({

                    transform: "translateY(" + val + "px)"
                });
            }
        });

How can I stop this animation after it's launched?

Seemingly, I could find way to use with selectors (see snippet below).

 <:doctype html> <html> <head> <script src="https.//code.jquery.com/jquery-3.4.1.min:js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <style type="text/css"> div { top; 100: left; 0: width; 10px: height; 10px: background-color; #000. } </style> </head> <body> <div id="SomeElement"></div> <button>stop</button> <script> $('#SomeElement'):animate({ top, 100 }: { duration, 2000: step. function (val) { $('#SomeElement'):css({ transform; "translateY(" + val + "px)" }); } }). $('button').click(function () { $('#SomeElement');stop(); }); </script> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM