美文网首页前端大联盟,与众不同Web前端之路
基于Jquery队列实现根据不同输入数量元素实现动画

基于Jquery队列实现根据不同输入数量元素实现动画

作者: MakingChoice | 来源:发表于2016-04-24 23:07 被阅读85次
animate.gif

如上面的gif图所示,可以在输入框中,输入要产生的动画的数量,然后点击click me按钮,就产生了效果。产生的效果是通过在数组中预设的几种。这里为了演示方便,没有设置具体的形状,比如可以更换为一些其它的iconfont来实现效果。<p>
具体思路:通过<code>$.queue</code>和<code>$.dequeue</code>来实现动画队列的存取与取出实现效果。首先通过按照<code>input</code>输入的数字来形成对应数量效果对象的数组。然后在把数组存放到<code>$.queue</code>中,最后通过click me按钮触发,一个一个取出动画序列,实现动画。<p>
注意:这里要注意的是,在一个一个取出动画的时候,用到了<code>setInterval</code>,不需要的时候一定要清除计时器,否则会影响序列,不停的取出。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../lib/jquery-2.1.4.min.js" type="text/javascript"></script>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 50px;
            height: 50px;
            border: 1px solid slateblue;
            position: relative;
            left: 750px;
            top: 500px;
        }
        .animate{
            width:  50px;
            height: 50px;
            border: 1px solid skyblue;
            background: slateblue;
            opacity: 0;
            position: absolute;
        }
        .up{
            z-index: 999;
            background: red;
            width:  50px;
            height: 50px;
            border: 1px solid skyblue;

        }
        #btn{
            background: slateblue;
            position: absolute;
            left: 0;
            top: 0;
        }
        .number{
            position: absolute;
            top: 600px;
            left: 740px;
            width: 100px;
            height: 30px;
        }
        #btnTrg{
            background: slateblue;
            width: 100px;
            height: 30px;
            border: 0;
            position: absolute;
            top: 600px;
            left: 600px;
        }
        .result{
            position: absolute;
            top: 600px;
            left: 900px;
            width: 100px;
            height: 30px;
            background: skyblue;
            text-align: center;
        }

    </style>
</head>
<body>
    <div id="content"></div>
    <div class="box">
        <span class="animate1 animate"></span>
        <span class="animate2 animate"></span>
        <span class="animate3 animate"></span>
        <span class="animate4 animate"></span>
        <span class="animate5 animate"></span>
        <span class="animate6 animate"></span>
        <div class="up"></div>
    </div>
    <div id="btn"></div>
    <button id="btnTrg">click me</button>
    <input type="text" class="number" id="num"/>
    <span class="result"></span>
</body>
<script>
    var span1=$(".animate1");
    var span2=$(".animate2");
    var span3=$(".animate3");
    var span4=$(".animate4");
    var span5=$(".animate5");
    var span6=$(".animate6");
    var box=$("#content");
    var btn=$("#btn");
    var btnTrg=$("#btnTrg");
    var input=$("#num");
    var result=$(".result");
    var resultNum=1;
    var ani=[
        function () {
            span1.css({
                background:"red",
                opacity:1
            }).animate({
                left:-300,
                top:-100
            }, function () {
                result.html(resultNum++)
            }).animate({
                left:-50,
                top:-450,
                opacity:0.2
            }, function () {
                $(this).css({
                    left:0,
                    top:0,
                    opacity:0
                })
            })
        },
        function () {
            span2.css({
                background:"blue",
                opacity:1
            }).animate({
                left:-200,
                top:-200
            }, function () {
                result.html(resultNum++)
            }).animate({
                left:-50,
                top:-450,
                opacity:0.2
            }, function () {
                $(this).css({
                    left:0,
                    top:0,
                    opacity:0
                })
            })
        },
        function () {
            span3.css({
                background:"pink",
                opacity:1
            }).animate({
                left:-100,
                top:-300
            }, function () {
                result.html(resultNum++)
            }).animate({
                left:-50,
                top:-450,
                opacity:0.2
            }, function () {
                $(this).css({
                    left:0,
                    top:0,
                    opacity:0
                })
            })
        },
        function () {
            span4.css({
                background:"green",
                opacity:1
            }).animate({
                left:100,
                top:-300
            }, function () {
                result.html(resultNum++)
            }).animate({
                left:50,
                top:-450,
                opacity:0.2
            }, function () {
                $(this).css({
                    left:0,
                    top:0,
                    opacity:0
                })
            })
        },
        function () {
            span5.css({
                background:"orange",
                opacity:1
            }).animate({
                left:200,
                top:-200
            }, function () {
                result.html(resultNum++)
            }).animate({
                left:50,
                top:-450,
                opacity:0.2
            }, function () {
                $(this).css({
                    left:0,
                    top:0,
                    opacity:0
                })
            })
        },
        function () {
            span6.css({
                background:"black",
                opacity:1
            }).animate({
                left:300,
                top:-150
            }, function () {
                result.html(resultNum++)
            }).animate({
                left:50,
                top:-450,
                opacity:0.2
            }, function () {
                $(this).css({
                    left:0,
                    top:0,
                    opacity:0
                })
            })
        }
    ];
    var queue;
    var len=0;
    var flag=0;
    var timer;
    input.on("keyup", function () {
        var result=[];
        var value=input.val();
        len=value;
        if(flag>=len){
            clearInterval(timer);
        }
        if(value<6){
            result =ani.slice(0,value);
            queue=$.queue(box,"animate",result);
        }else if(value>6){
            var num1=Math.floor(value/6);
            var num2=value%6;
            for(var i=0;i<num1;i++){
                result=result.concat(ani);
            }
            result=result.concat(ani.slice(0,num2));
            console.log(result);
            $.queue(box,"animate",result);
        }
    });
    btnTrg.on("click", function () {
        resultNum=0;
        flag=0;
        timer=setInterval(function () {
            flag++;
            console.log(flag);
            $.dequeue(box,"animate");

        },500);
    })
</script>
</html>

相关文章

  • 基于Jquery队列实现根据不同输入数量元素实现动画

    如上面的gif图所示,可以在输入框中,输入要产生的动画的数量,然后点击click me按钮,就产生了效果。产生的效...

  • jQuery动画队列

    队列实现是jQuery非常棒的一个拓展,使用动画队列可以使动画更容易实现。 .animate( propertie...

  • 队列

    基于数组的循环队列 Java实现 基于链表的队列实现 Java实现

  • 手敲数据结构——基于最大堆实现优先队列

    这里实现的优先队列是基于最大堆实现的,java系统是基于最小堆实现的。 队列接口 优先队列实现 LeetCode上...

  • 队列 - Queue

    基本概念 队列和栈类似,不同的是,先进队列的元素,最先从队列出去。 实现 通过链表实现队列 Java中,队列是一个...

  • 实现一个优先队列

    实现一个优先队列 实现一个优先队列,队列需要添加元素,删除元素,获得最大(小)元素等方法。要实现这样的一个队列,至...

  • js与jquery方法对比

    一.文档就绪 jquery实现 简写形式为: js实现 二、元素选择 三、点击事件 jquery实现 js实现 四...

  • jQuery - js 与 jQuery 的相互转换

    $() 与 jQuery() jQuery中$函数,根据传入参数的不同,进行不同的调用,实现不同的功能。返回的是j...

  • 基于Rabbitmq实现延迟队列

    转自 基于Rabbitmq实现延迟队列 基于Rabbitmq实现延迟队列 延迟队列的使用场景 淘宝订单业务:下单后...

  • jQuery 动画队列

    jQuery 动画队列 动画队列的方法: 我们知道jQuery提供了以下几种方法来操作动画队列: stop([cl...

网友评论

    本文标题:基于Jquery队列实现根据不同输入数量元素实现动画

    本文链接:https://www.haomeiwen.com/subject/rmxqhttx.html