如何使用jQuery制作变换比例动画? [英] How to do a transform scale animation with jQuery?

查看:79
本文介绍了如何使用jQuery制作变换比例动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现打开窗口"动画,我尝试了以下方法:

I'm trying achieve and "opening-window" animation, I tried the following:

import $ from 'jquery'

export const fade = {
  css: false,
  enter: function (el, done) {
    $(el)
      .css('opacity', 0)
      .css({ transform: 'scale(0.9,0.9)' })
      .animate({
        opacity: 1,
        transform: 'scale(1,1)'
      }, 1000, done)
  },
  enterCancelled: function (el) {
    $(el).stop()
  },
  leave: function (el, done) {
    $(el).animate({
      opacity: 0,
      transform: 'scale(0.9,0.9)'
    }, 1000, done)
  },
  leaveCancelled: function (el) {
    $(el).stop()
  }
}

但是只有opacity动画有效(从0到1).而且只有transform: 'scale(0.9,0.9)'被应用.

But only the opacity animation works (it goes from 0 to 1). And only transform: 'scale(0.9,0.9)' is being applied.

注意:由于我使用的是Chrome,因此我也尝试过使用'-webkit-transform',但是它也不起作用.

Note: I also tried with '-webkit-transform' since I'm using Chrome, but it didn't work either.

transform无法正常工作的原因是什么?

What's the reason transform isn't working?

推荐答案

当然,与jQuery动画相比,css过渡要好得多.

Of course that css transition is much better then jQuery animate.

这是使用jQuery动画transform的通用示例.

Here is a generic example of how animate transform with jQuery.

技巧是设置不受影响的CSS属性(如border-spacing)并对其进行动画处理".然后,您可以使用step回调创建自己的动画效果.

The trick is to set unaffected css property (like border-spacing) and "animate" it. Then, you can use the step callback to create your own animation effect.

$('button').click(function() {
  $('div').css('borderSpacing', 1).animate(
    {
      borderSpacing: 1.3
    },
    {
    step: function(now,fx) {
      $(this).css('transform','scale('+now+')');  
    },
    duration:'slow'
  });
});

div {
  width:150px;
  height:150px;
  background:green;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>
<button>Animate</button>

这篇关于如何使用jQuery制作变换比例动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆