CCS过渡和动画

2019-10-13  本文已影响0人  手指乐

过渡

  1. transition-property:过渡属性,比如color, opacity, width等所有可以在css中使用的属性
  2. transition-duration:过渡持续时间
  3. transition-timing-function:过渡速度,比如linear是匀速
  4. transition-delay:过渡延迟,0表示立即开始
.tl:hover {
  color: red;
  font-size: 18px;
}
.tl {
  transition-property: color font-size;//可以定义多个属性同时过渡,或者使用all代表所有变化的属性
  transition-duration: 2s;
  transition-timing-function: linear;
  transition-delay: 0s;
}

也可以写到一起:

.tl:hover {
  color: red;
  font-size: 18px;
}
.tl {
  transition: font-size linear 2s 0s, color linear 4s 0s 
}

注意多个属性可以定义不同的过渡效果,用逗号隔开,时间一定要带单位,比如0s不能写成0

动画

@keyframes my-animation {
    0% {
        color: red;
    }
    50% {
        color: green;
    }
    100% {
        color: blue;
    }
}

也可以只设置首尾两个关键帧

@keyframes my-animation {
    from {
        color: green;
    }
    to {
        color: blue;
    }
}
p {
    animation: my-animation 3s linear infinite alternate;
}

infinite:动画播放次数为循环播放

alternate:动画播放顺序为先正向后反向
如果不定义infinite,p元素会在创建完毕后执行一次动画,如果使用js动态给p元素添加动画class,会在添加完成后执行动画,参见:https://www.jianshu.com/p/0569e2925d58

上一篇 下一篇

猜你喜欢

热点阅读