资讯专栏INFORMATION COLUMN

进入 CSS3 动画

lindroid / 2592人阅读

摘要:我最近有机会深入研究一些动画。下一步是添加动画效果并确定它们何时发生。创建关键帧后,您可以将动画称为属性。点击动画使用上面的关键帧和动画语法,这里是我用来在本页顶部的中制作动画的代码。

我最近有机会深入研究一些CSS3动画。 我使用了像animate.css这样的库,用javascript完成了动画,但从未做过任何自定义的CSS3工作 原文

任务

我们最近在SeatGeek更新了我们的“跟踪"图标,以匹配我们的新iPhone应用程序。 首席设计师在PSD中创建了具有不同状态的心脏图标,并在下面创建了动画:

什么是CSS3动画?

在CSS中,动画是一种让元素逐渐改变样式的效果。 您可以使用@keyframes关键字创建动画,后跟动画的名称。

@keyframes heartAnimation {
  /* Animation code goes here */
}

要使动画跨浏览器兼容,您需要使用供应商前缀:

@keyframes heartAnimation {
  /* IE 10+ */
}

@-webkit-keyframes heartAnimation {
  /* Safari 4+ */
}

@-moz-keyframes heartAnimation {
  /* Fx 5+ */
}

@-o-keyframes heartAnimation {
  /* Opera 12+ */
}

但是,对于本文的其余部分,我将为了空间而排除供应商前缀。

下一步是添加动画效果并确定它们何时发生。 您可以使用0%到100%的百分比或使用“from"和“to"关键字来执行此操作,只需使用起始和结束状态的简单动画。 下面是将背景颜色从黄色变为蓝色,然后从黄色变为绿色变为蓝色的示例。

@keyframes colorChange {
  from {background: yellow;}
  to {background: blue;}
}

@keyframes colorChange {
  0% {background: yellow;}
  50% {background: green;}
  100% {background: blue;}
}

创建关键帧后,您可以将动画称为CSS属性。 例如,下面的代码将运行colorChange动画2次以上,持续时间为2秒:

.color-animation {
  animation-name: changeColor;
  animation-iteration-count: 2;
  animation-duration: 2s;
}

/* Shorthand */
.color-animation {
  animation: changeColor 2 2s;
}

您可以查看所有CSS3动画属性here

计划动画

在看了几次gif之后,我意识到它是一个轻微的收缩,然后扩展到比原始尺寸略大的尺寸,然后回到原来的尺寸。

Heart点击动画

使用上面的CSS3关键帧和动画语法,这里是我用来在本页顶部的gif中制作动画的代码。 它使用css变换和属性来缩放图像。

@keyframes heartAnimation {
  0% {transform: scale(1,1)}
  20% {transform: scale(0.9,0.9)}
  50% {transform: scale(1.15,1.15)}
  80% {transform: scale(1,1)}
}

.toggle-animation {
  animation: heartAnimation 0.7s; // no iteration count is needed as the default is 1 time
}

对于图像,我使用的是精灵,所以我还需要更改图像的位置以获得红色背景:

.toggle-animation {
  background: url("../images/animation-example-sprite.png") no-repeat -320px 0;
  animation: heartAnimation 0.7s; // no iteration count is needed as the default is 1 times
}
Loading动画

对于一个加载状态,我让心脏发白并且无限地脉动in-and-out。 它还缩小并缩小到原始大小,而不是像上面的heartAnimation代码那样在进入原始状态之前略大于原始大小。 以下是加载状态的代码:

@keyframes loading {
  0% {transform: scale(1,1) }
  50% {transform: scale(0.8,0.8) }
  100% {transform: scale(1,1) }
}

/* Notice the added "infinite" to is used to make the animation-iteration-count */

.toggle-loading {
  background: url("../images/animation-example-sprite.png") no-repeat -160px 0; // make background white
  animation: loading 1s infinite;
  -webkit-animation: loading 1s infinite;
  -moz-animation: loading 1s infinite;
  -o-animation: loading 1s infinite;
}
查看动画的演示

这是我为演示动画而构建的网站:

http://heart-animation.herokuapp.com/

下面是我用来点击每个图标时动画的JS。 JS添加并删除了我添加动画属性的类。

$(document).ready(function(){

  $(".animation-1 .image").on("click", function(){
    $(this).toggleClass("toggle-animation");
  });

  $(".animation-2 .image").on("click", function(){
    $(this).toggleClass("toggle-animation-slow");
  });

  $(".animation-3 .image").on("click", function(){
    $(this).toggleClass("toggle-loading");
  });

});

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/116323.html

相关文章

  • CSS3 animation 基础

    摘要:最近项目有需求要做一个的页面,涉及到的使用。中间的动画过程由浏览器渲染引擎进行渲染。其中可以使用的时间段或者通过关键词和,等价于和。动画以低速开始,然后加快,在结束前变慢。动画延迟进入动画动画匀速播放持续,播放一次后将停止在最终的状态 最近项目有需求要做一个H5的页面,涉及到CSS3 animation的使用。这里做了一个项目中的总结。 animation 基本用法 animation...

    rose 评论0 收藏0
  • 谈谈常见H5制作方法——视频与CSS3

    摘要:但目前白名单申请途径已经关闭。目前在安卓有一种同层播放器的解决方案。可解决安卓机自动全屏以及视频播放完毕会跳出广告的问题,并且可以实现交互。 本文在H5动效的常见制作手法的基础上,对相印的H5动效制作手法进行了扩展和整理,并结合案例谈谈怎么将其做得生动。 视频类 1、体验案例 视频类h5可以带给用户动效逼真,流畅的体验。虽然说制作视频的难度较大,但是瑕不掩瑜,一支视频可以尽可能地创造出...

    tracy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<