当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JQuery clearQueue()用法及代码示例


clearQueue()方法从队列中删除所有尚未运行的项目。请注意,一个函数开始运行后,它将一直运行到完成为止。
用法:

$(selector).clearQueue(name);

“selector”是选定的元素。
参数:它接受参数“name”,它是函数的名称。
返回值:它返回带有删除函数的所选元素。

jQuery代码显示clearQueue()方法的用法方式:

代码1:
在下面的代码中,清除了动画方法。
<html> 
  
<head> 
    <script src="https://code.jquery.com/jquery-1.10.2.js"> 
    </script> 
    <script> 
        <!-- jQuery code to demonstrate clearQueue method -->
        $(document).ready(function() { 
            <!-- click button to start animation -->
            $("#b1").click(function() { 
                $("div").animate({ 
                    height: 200 
                }, 2000); 
                $("div").animate({ 
                    width: 200 
                }, 2000); 
                $("div").animate({ 
                    height: 100 
                }, 2000); 
                $("div").animate({ 
                    width: 100 
                }, 2000); 
                $("div").animate({ 
                    height: 200 
                }, 2000); 
                $("div").animate({ 
                    width: 200 
                }, 2000); 
                $("div").animate({ 
                    height: 100 
                }, 2000); 
                $("div").animate({ 
                    width: 100 
                }, 2000); 
            }); 
            <!-- button to stop animation -->
            $("#b2").click(function() { 
                $("div").clearQueue(); 
            }); 
        }); 
    </script> 
    <style> 
        div { 
            background: green; 
            height: 100px; 
            width: 100px; 
        } 
          
        button { 
            margin-top: 10px; 
        } 
    </style> 
</head> 
  
<body> 
    <div></div> 
    <!-- click on this button to start the animation -->
    <button id="b1">Start !</button> 
    <!-- click on this button to stop the animation at  
             given situation -->
    <button id="b2">Stop !</button> 
</body> 
  
</html>

输出:
在单击任何按钮之前,

单击“开始!”后,再经过一段时间“停止!”按钮,


代码2:

<html> 
  
<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/ 
              jquery/3.3.1/jquery.min.js"></script> 
    <script> 
        <!-- jQuery code to demonstrate clearQueue method -->
        $(document).ready(function() { 
            $(".b1").click(function() { 
                $("p").animate({ 
                    borderRightWidth: "5px" 
                }); 
                $("p").animate({ 
                    borderTopWidth: "5px" 
                }); 
                $("p").animate({ 
                    borderLeftWidth: "5px" 
                }); 
                $("p").animate({ 
                    borderBottomWidth: "5px" 
                }); 
            }); 
            $(".b2").click(function() { 
                $("p").clearQueue(); 
            }); 
        }); 
    </script> 
    <style> 
        p { 
            display: block; 
            width: 150px; 
            border: 1px solid green; 
            padding: 10px; 
        } 
    </style> 
</head> 
  
<body> 
    <p>This is a paragraph.</p> 
    <!-- click on this button to start the animation -->
    <button class="b1">Start</button> 
    <!-- click on this button to stop the animation  
            at given situation -->
    <button class="b2">Stop</button> 
</body> 
  
</html>

输出:
在单击任何按钮之前,

单击“开始!”后,再经过一段时间“停止!”按钮,



相关用法


注:本文由纯净天空筛选整理自kundankumarjha大神的英文原创作品 jQuery | clearQueue() with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。