jQuery-stop方法停止所有在指定元素上正在运行的动画

1.stop()方法解析

  • 停止所有在指定元素上正在运行的动画
  • stop(clearQueue,gotoEnd)
  • 这个两个参数可选值是布尔值
  • stop(flase,flase):不请空动画队列,不立即跳到动画最后
  • stop(true,true):请空动画队列,立即跳到动画最后
  • stop(flase,true):不请空动画队列,立即跳到动画最后
  • stop(true,flase):请空动画队列,不立即跳到动画最后

2.代码如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>stop用法</title>
    <style>
        #box {
             100px;
            height: 100px;
            position: absolute;
            top: 50px;
            left: 50px;
            border: 1px solid #000;
        }
    </style>
</head>


<body>
    <button id="btn1">stop(false,false)</button>
    <button id="btn2">stop(true,true)</button>
    <button id="btn3">stop(false,true)</button>
    <button id="btn4">stop(true,false)</button>
    <div id="box"></div>
    <script src=" https://cdn.bootcss.com/jquery/3.5.0/jquery.min.js"></script>
    <script>
        var box = $("#box")
        box.animate({
            left: 300
        }, 1000)
        box.animate({
            top: 300
        }, 1000)
        box.animate({
            left: 50
        }, 1000)
        box.animate({
                top: 50
            }, 1000)
            //stop(flase,flase):不请空动画队列,不立即跳到动画最后
        $("#btn1").click(function() {
                box.stop(false, false)
            })
            // stop(true,true):请空动画队列,立即跳到动画最后
        $("#btn2").click(function() {
                box.stop(true, true)
            })
            // stop(flase,true):不请空动画队列,立即跳到动画最后
        $("#btn3").click(function() {
                box.stop(false, true)
            })
            // stop(true,flase):请空动画队列,不立即跳到动画最后
        $("#btn4").click(function() {
            box.stop(true, false)
        })
    </script>
</body>

</html>
原文地址:https://www.cnblogs.com/dongxuelove/p/12933765.html