• JQuery中stop方法的使用


    转载来源:https://www.cnblogs.com/goodborder/p/5843050.html

    在前台页面开发中有时候我们会需要一些比较酷的效果,这个时候使用JQuery中的动画来实现便显得非常的简单。

    最近在工作中碰到了一个页面元素移动的效果,这是个简单的页面效果也非常容易实现。

    在使用中用到了一个停止动画的方法"stop()",以前只是用也没有过多的关注。

    这几天再次碰到,便翻开文档测试了一番,竟也有了一些新的认识。对这个方法有了全新的了解,在这里把我的测试记录下来。

    在JQuery文档中对这个方法的解释是这样的: 

    一、概述

    停止所有在指定元素上正在运行的动画。

    如果队列中有等待执行的动画(并且clearQueue没有设为true),他们将被马上执行。

    二、没有参数

    场景模拟

    假设有一个元素需要在背景中进行移动,然后回到起始位置。页面中有三个按钮,分别负责“开始移动”,“采用了stop方法的回归”,“没有采用stop方法的回归”。

    整体页面效果图如下:

    测试代码和效果

    <script type="text/javascript">
            
            $(function () {
                //向右移动600px
                $("#Button1").click(function () {
                    $("#move").stop().animate({ left: 610 }, 3000); 
                });
                //立即往回移动(有stop)
                $("#Button2").click(function () {
                    $("#move").stop().animate({ left: 10 }, 3000); 
                });
                //移动完600px,才会往回移动(没有stop)
                $("#Button3").click(function () {
                    $("#move").animate({ left: 10 }, 3000);
                });
    
            });
        </script>
    View Code

    通过测试我们不难发现

    有stop,当蓝色方块在向右侧移动的时候,点击按钮,方块会立即往回返(向左侧移动)。

    没有stop,当蓝色方块在向右侧移动的时候,点击按钮,方块会等到完全移动到指定位置后才往回返(向左侧移动)。

    测试总结

    stop()停止了当前正在执行的动画,并使后续的动画立即得到了执行。

     三、两个参数或者一个参数

    查看文档可以知道这个时候参数依次为:[clearQueue],[gotoEnd]  并且都为可选,类型都为Boolean。

    clearQueue:如果设置成true,则清空队列。可以立即结束动画。

    gotoEnd:让当前正在执行的动画立即完成,并且重设show和hide的原始样式,调用回调函数等。

    我们可以挨个进行测试。代码如下:

    <style type="text/css">
            table, tr, th, td {
                margin: 0px;
                padding: 5px;
                border-collapse: collapse;
                border: 1px solid black;
            }
    
            .bg {
                background-color: #8FBC8F;
                border: 1px solid black;
                width: 1000px;
                height: 200px;
                margin: 10px;
                position: relative;
            }
    
            .line {
                position: absolute;
                background-color: #3b9feb;
            }
    
            #linetop {
                top: 10px;
                left: 10px; /*980px;*/
                height: 5px;
            }
        </style>
        <script type="text/javascript">
    
            $(function () {
                // 
    
                var line;
    
                $("#start").click(function () {
                    line = $("#linetop").animate({  980 }, 3000)
                                        .animate({ height: 180 }, 3000);
                });
    
    
                $("#stop").click(function () {
                    $("#linetop").stop();
                });
    
                $("#stop_true").click(function () {
                    $("#linetop").stop(true);
                });
    
                $("#stop_false").click(function () {
                    $("#linetop").stop(false);
                });
    
    
                $("#stop_true_true").click(function () {
                    $("#linetop").stop(true, true);
                });
    
                $("#stop_true_false").click(function () {
                    $("#linetop").stop(true, false);
                });
    
                $("#stop_false_true").click(function () {
                    $("#linetop").stop(false, true);
                });
    
                $("#stop_false_false").click(function () {
                    $("#linetop").stop(false, false);
                });
    
            });
        </script>
    View Code
    <body><input type="button" id="start" value="动作开始" />
        <table>
            <tr>
                <th>方法</th>
                <th>参数</th>
                <th>说明</th>
                <th>方法</th>
                <th>参数</th>
                <th>说明</th>
            </tr>
            <tr>
                <td>
                    <input type="button" id="stop" value="stop()" /></td>
                <td></td>
                <td colspan="4">清空队列,当前执行动作<span style="color:#ff6a00; font-weight:bold;">立即停止</span>。后续动作会不再执行。
                    等同于:<span style="color:#ff6a00; font-weight:bold;">stop(false,false)</span>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" id="stop_true" value="stop(true)" /></td>
                <td>[clearQueue]</td>
                <td>清空队列,当前执行动作<span style="color:#ff6a00; font-weight:bold;">立即停止</span>。后续动作会不再执行。</td>
                <td>
                    <input type="button" id="stop_false" value="stop(false)" /></td>
                <td>[clearQueue]</td>
                <td>不清空队列,当前执行动作<span style="color:#ff6a00; font-weight:bold;">立即停止</span>。后续动作会立即执行。</td>
            </tr>
            <tr>
                <td>
                    <input type="button" id="stop_true_true" value="stop(true,true)" />
                </td>
                <td>[clearQueue],[gotoEnd]</td>
                <td>清空队列,当前执行动作<span style="color:#150ce6; font-weight:bold;">立即完成</span>。后续动作会不再执行。</td>
                <td>
                    <input type="button" id="stop_false_true" value="stop(false,true)" />
                </td>
                <td>[clearQueue],[gotoEnd]</td>
                <td>不清空队列,当前执行动作<span style="color:#150ce6; font-weight:bold;">立即完成</span>。后续动作会立即执行。</td>
            </tr>
            <tr>
                <td><input type="button" id="stop_true_false" value="stop(true,false)" /></td>
                <td>[clearQueue],[gotoEnd]</td>
                <td>清空队列,当前执行动作<span style="color:#ff6a00; font-weight:bold;">立即停止</span>。后续动作会不再执行。</td>
                <td><input type="button" id="stop_false_false" value="stop(false,false)" /></td>
                <td>[clearQueue],[gotoEnd]</td>
                <td>不清空队列,当前执行动作<span style="color:#ff6a00; font-weight:bold;">立即停止</span>。后续动作会立即执行。</td>
            </tr> 
    
        </table> 
        <div class="bg" id="Div1">
            <div class="line" id="linetop"></div>
        </div>
    </body>
    View Code

    我们可以看到如下整理结果

    方法参数说明方法参数说明
      清空队列,当前执行动作立即停止。后续动作会不再执行。 等同于:stop(false,false)
    [clearQueue] 清空队列,当前执行动作立即停止。后续动作会不再执行。 [clearQueue] 不清空队列,当前执行动作立即停止。后续动作会立即执行。
    [clearQueue],[gotoEnd] 清空队列,当前执行动作立即完成。后续动作会不再执行。 [clearQueue],[gotoEnd] 不清空队列,当前执行动作立即完成。后续动作会立即执行。
    [clearQueue],[gotoEnd] 清空队列,当前执行动作立即停止。后续动作会不再执行。 [clearQueue],[gotoEnd] 不清空队列,当前执行动作立即停止。后续动作会立即执行。

    四、笔记

    在jQuery的较高版本中stop还有一种用法,就是和队列(queue)配合使用。对于这种用法,我目前还不是还不是属性,这里无法给出一个好的解释。

    留待以后在慢慢研究了。

    目前stop的用法相信也足够我们只用了。

  • 相关阅读:
    “家亡血史,原应叹息”
    SQLite初体验
    两张表数据同步用触发器
    openstack 后期维护(四)--- 删除僵尸卷
    Python3 装逼神器---词云(wordcloud)
    (三)FastDFS 高可用集群架构学习---Client 接口开发
    (四)FastDFS 高可用集群架构学习---后期运维--基础知识及常用命令
    (二)FastDFS 高可用集群架构学习---搭建
    (一)FastDFS 高可用集群架构学习---简介
    Python3使用Print输出彩色字体
  • 原文地址:https://www.cnblogs.com/moppet/p/12468104.html
Copyright © 2020-2023  润新知