首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用velocity创建更智能的切换动画

如何使用velocity创建更智能的切换动画
EN

Stack Overflow用户
提问于 2015-08-25 19:25:09
回答 1查看 374关注 0票数 0

我正在使用velocity.js处理动画,它在很大程度上很棒。我在codepen上有一个基本的演示,它显示了一个简单的切换按钮,但是让它动画化的js似乎非常麻烦。

在我的例子中,处理这样的切换动画最好的方法是什么?

代码语言:javascript
复制
var open = false;

$('.filter').click(function(e){

    e.preventDefault();

    var el = $(this);

    if(!open){

        el.find('.filter__line:first').velocity({translateY: [0, -5]}, 200, function(){
            $(this).addClass('filter__line--thick').velocity({rotateZ: '45deg'}, 200);
        });

        el.find('.filter__line:last').velocity({translateY: [0, 5]}, 200, function(){
            $(this).addClass('filter__line--thick').velocity({rotateZ: '-45deg'}, 200);
        });

        open = true;

    } else {

        el.find('.filter__line:first').velocity({rotateZ: '0deg'}, 200, function(){
            $(this).removeClass('filter__line--thick').velocity({translateY: [-5, 0]}, 200);
        });

        el.find('.filter__line:last').velocity({rotateZ: '0deg'}, 200, function(){
            $(this).removeClass('filter__line--thick').velocity({translateY: [5, 0]}, 200);
        });

        open = false;
    }

});

http://codepen.io/matt3224/pen/zGgKwP?editors=011

谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-08-27 02:08:54

我使用Velocity很长一段时间了,遇到了很多这样的问题。如果你在做任何复杂的动画,我强烈建议你使用GSAP。GSAP时间线允许您轻松地播放、暂停和反转一系列动画,并且语法简洁。你可以在GSAP website上找到更多信息。

下面是脚本的样子:

代码语言:javascript
复制
$(document).ready(function(){

var top = $('.part-1');
var mid = $('.part-2');
var btm = $('.part-3');

var tl = new TimelineMax().pause();
  tl.to(mid, 0.3, {x:100, opacity:0})
  .to(top, 0.3, {rotation:18, transformOrigin:"left top"},"second")
  .to(btm, 0.3, {rotation:-18, transformOrigin:"left bottom"},"second");

var active = false;

  $('h1').click(function(){
    if(!active){
      tl.play();
      active = true;
    } else {
      tl.reverse();
      active = false;
    } 
  }); // end of click

}); // end of ready 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32202762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档