【问题标题】:How to animate stuff with animate.css and Jquery如何使用 animate.css 和 Jquery 制作动画
【发布时间】:2015-06-23 18:58:22
【问题描述】:

我有以下代码片段

HTML

<ul id="test">
    <li class="a">1</li>
    <li class="b">2</li>
    <li class="c">3</li>
</ul>
<ul id="test2">
    <li data-prod="a" class="animated">
        <div>1</div>
        <div>2</div>
        <div>3</div>        
    </li>
    <li data-prod="b" class="animated hide">
        <div>1</div>
        <div>2</div>
        <div>3</div> 
    </li>
    <li data-prod="c" class="animated hide">
        <div>1</div>
        <div>2</div>
        <div>3</div> 
    </li>
</ul>

CSS

ul#test > li {
    display: inline-block;
    padding: 10px;
    cursor: pointer;
}
#test {
    position: fixed;
    top: 0;
    left: 0;
}
#test2 {
    position: fixed;
    top: 0;
    right: 0;
    list-style: none;
}
#test2 > li {
    background: yellow;
    width: 350px;
    height: 600px;
    /* transition: height 0.3s ease, width 0.2s ease;*/
    overflow: hidden;
    margin-top: -30px;
    padding-top: 30px;
    margin-right: -50px;
}
.hide {
    display: none;
}

还有这个 jquery

$(document).ready(function () {

    $('#test li').on('click', function () {

        var className = $(this).attr('class');
        $('#test2 > li').each(function () {

            var prodName = $(this).data('prod');


            if (prodName == className) {

                $('#test2 > li').removeClass('bounceInRight').addClass('bounceOutRight');
                $this = $(this);
                setTimeout(function () {
                    console.log(this);
                    $('#test2 > li').addClass('hide')
                    $this.removeClass('hide bounceOutRight').addClass('bounceInRight');
                }, 400);


            }
        });
    })
});

这里是DEMO

我有几个问题

  1. 每次点击 liul,id 为 #test,它都会为 适当的liul 具有ID #test2。如何为lis 中的每个 div 设置动画为 fadeInTop(animate.css) 一个接一个没有 一旦li 完成动画,就使用类和ID(仅使用标签选择器)。
  2. 有没有更好的方法来做我用 jquery 所做的事情,并且性能更好。我在codereview 中添加了这部分问题

【问题讨论】:

  • 你看过内置的jquery函数吗,比如hidefadeIn and fadeOut
  • @Phoenix 是的,但我想用 animate.css 和 jquery 没有从顶级动画中淡入淡出
  • 如果你在有效果且没有css文件的动画之后试试这个 -- julian.com/research/velocity
  • @Tasos 谢谢你的链接:)

标签: javascript jquery css css-animations animate.css


【解决方案1】:

您可以为动画添加回调。如果不删除和添加类,您将无法重复动画。

               $(document).ready(function () {

        $('#test li').on('click', function () {

            var className = $(this).attr('class');
            $('#test2 > li').each(function () {

                var prodName = $(this).data('prod');


                if (prodName == className) {

                    $('#test2 > li').removeClass('bounceInRight').addClass('bounceOutRight');
                    $this = $(this);
                    $this.find('div').removeClass('fadeInDown');
                    setTimeout(function () {
                        console.log(this);
                        $('#test2 > li').addClass('hide')
                        $this.removeClass('hide bounceOutRight').addClass('bounceInRight');
                    }, 400);

                    
                }
            });
          
        })
        
        $('#test2 li').on('oanimationend animationend webkitAnimationEnd', function() { 
            $(this).find('div').each(function(i){
               var $this = $(this);
                setTimeout(function(){
                  $this.addClass('animated fadeInDown')
                },i*500);
                
            })
        });
    });
    ul#test > li {
        display: inline-block;
        padding: 10px;
        cursor: pointer;
    }
    #test {
        position: fixed;
        top: 0;
        left: 0;
    }
    #test2 {
        position: fixed;
        top: 0;
        right: 0;
        list-style: none;
    }
    #test2 > li {
        background: yellow;
        width: 350px;
        height: 600px;
        /* transition: height 0.3s ease, width 0.2s ease;*/
        overflow: hidden;
        margin-top: -30px;
        padding-top: 30px;
        margin-right: -50px;
    }
    .hide {
        display: none;
    }
    ul#test2 > li div {  padding: 5px; transform: translate3d(0,-2000px,0)}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.6/animate.min.css" rel="stylesheet" />

<ul id="test">
  <li class="a">1</li>
  <li class="b">2</li>
  <li class="c">3</li>
</ul>
<ul id="test2">
  <li data-prod="a" class="animated hide">
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </li>
  <li data-prod="b" class="animated hide">
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </li>
  <li data-prod="c" class="animated hide">
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </li>
</ul>

【讨论】:

  • 谢谢 :) 这对我帮助很大。我想按 1、2、3 的顺序一个接一个地为这些 div 设置动画。你能帮我吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 2015-04-19
  • 2013-10-17
  • 1970-01-01
  • 2015-12-07
  • 2013-03-20
相关资源
最近更新 更多