⤴Top⤴

CSS 动画

博客分类: 前端

CSS 动画

CSS 动画

transform 变换

transform

transform: none | <transform-function>+

transform-origin

transform-origin 用来设置或检索对象以某个原点进行转换,默认值:50% 50%,效果等同于 center center,取值为:

-webkit-transform-origin: top center;
-moz-transform-origin: top center;
-ms-transform-origin: top center;
-o-transform-origin: top center;
transform-origin: top center;

transform-style

transform-style 指定某元素的子元素是位于三维空间内,还是在该元素所在的平面内被扁平化,取值为:

perspective

perspective 指定观察者距离「z=0」平面的距离,为元素及其内容应用透视变换。不允许负值。不同视距的设置会导致成像不同

perspective:none | <length>

查看 JSFiddle 演示:

backface-visibility

backface-visibility 指定元素背面面向用户时是否可见,取值为:

查看 JSFiddle 演示:

transition 过渡

复合属性,检索或设置对象变换时的过渡,包括:

/* here’s a handy-dandy reference to the defaults and their matching cubic-bezier in case you want to start with one of them and customize from there:
*/
transition-timing-function: linear;
transition-timing-function: cubic-bezier(0, 0, 1, 1);

transition-timing-function: ease;
transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);

transition-timing-function: ease-in-out;
transition-timing-function: cubic-bezier(0.42, 0, 0.58, 1);

transition-timing-function: ease-in;
transition-timing-function: cubic-bezier(0.42, 0, 1, 1);

transition-timing-function: ease-out;
transition-timing-function: cubic-bezier(0, 0, 0.58, 1);

ease

查看 cubic-bezier 在线生成器

transition: background-color .5s ease-in .1s;

/* 等价于 */
transition-property: background-color;
transition-duration: .5s;
transition-timing-function: ease-in;
transition-delay: .1s;

animation 动画

复合属性,检索或设置对象所应用的动画特效,包括:

使用 @keyframe 定义动画时,简单的动画可以直接使用关键字 from 和 to,也可使用 <percentage> 去设置某个时间段内的任意时间点的样式:

/* animation: testanimations 3s linear */
@keyframes testanimations {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes testanimations{
  0% { transform: translate(0, 0); }
  40% { transform: translate(40px, 0); }
  80% { transform: translate(80px, 10px); }
  100% { transform: translate(100px, 20px); }
}

CSS 动画示例

alloyteam 简单动画演示

使用 perspective 和 transform 实现容器上的悬浮效果,

天王盖地虎 宝塔镇河妖

/* 宝塔镇河妖 样式参考 */
.under-line{
  position: relative;
  &::before {
    position: absolute;
    top: auto;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 1px;
    content: '';
    background-color: #ff4081;
    -webkit-transition: all .2s;
    transition: all .2s;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
  }
  &:hover:before, &:focus:before {
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
  }
}
/* 时钟 loading 动画 */
.timer {
  width: 24px;
  height: 24px;
  background-color: transparent;
  box-shadow: inset 0px 0px 0px 2px #fff;
 /*  border: 2px solid white;
  border-radius: 20px; */
  border-radius: 50%;
  position: relative;
  &:after,
  &:before {
    position: absolute;
    content: "";
    background-color: #fff;
    height: 2px;
    top: 11px;
    left: 11px;
    -webkit-transform-origin: 1px 1px;
    -moz-transform-origin: 1px 1px;
    transform-origin: 1px 1px;
    -webkit-animation: ticktock linear infinite;
    -moz-animation: ticktock linear infinite;
    animation: ticktock linear infinite;
  }
  &:after {
    width: 10px;
    -webkit-animation-duration: 2s;
    -moz-animation-duration: 2s;
    animation-duration: 2s;
  }
  &:before {
    width: 8px;
    -webkit-animation-duration: 8s;
    -moz-animation-duration: 8s;
    animation-duration: 8s;
  }
}
@keyframes ticktock {
  from { transform: rotate(0deg) }
  to { transform: rotate(360deg) }
}

参考链接

  1. CSS3 参考手册
  2. w3cplus - Transform-style 和 Perspective 属性 By Airen
  3. w3cplus - 使用 perspective 和 transform 实现容器上的悬浮效果 By Airen
  4. alloyteam 简单动画演示
  5. 前端观察 - HTML5+CSS3 loading 效果收集
  6. cubic-bezier 在线生成器
  7. Creative Link Effects