CSS3动画-transition、animation、transform(translate/scale/rotate)

目录

一、transition(过渡)

1、transition-duration 

2、transition-property

3、transition-timing-function

4、transition-delay

二、animation

1、@keyframes

2、animation-timing-function

3、animation-delay

4、animation-iteration-count:检索或设置对象动画的循环次数

5、animation-play-state:检索或设置对象动画的状态(running\paused)

6、animation-direction;是否反向运动

三、transform

rotate

rotateX

rotateY

translate

translateX

translateY

scale

scaleX

scaleY

 


一、transition(过渡)

  • transition-duration    transition效果需要指定多少秒或毫秒才能完成
  • transition-property    指定CSS属性的name,transition效果
  • transition-timing-function    指定transition效果的转速曲线
  • transition-delay    定义transition效果开始的时候

1、transition-duration 


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  transition-duration: 1s;

  &:hover {
    width: 4rem;
    height: 4rem;
  }
}
</style>

2、transition-property


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  transition-duration: 1s;
  transition-property: height;

  &:hover {
    width: 4rem;
    height: 4rem;
  }
}
</style>

3、transition-timing-function

  • linear(默认属性)    规定以相同速度开始至结束的过渡效果(等于 cubic-bezier(0,0,1,1))。    匀速
  • ease    规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1))。    快-慢-慢
  • ease-in    规定以慢速开始的过渡效果(等于 cubic-bezier(0.42,0,1,1))。    快-快
  • ease-out    规定以慢速结束的过渡效果(等于 cubic-bezier(0,0,0.58,1))。    慢-慢
  • ease-in-out    规定以慢速开始和结束的过渡效果(等于 cubic-bezier(0.42,0,0.58,1))。    慢-快-慢
  • cubic-bezier(n,n,n,n)    在 cubic-bezier 函数中定义自己的值。可能的值是 0 至 1 之间的数值。    自定义
     


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  transition-duration: 1s;
  transition-timing-function: ease-in-out;

  &:hover {
    width: 4rem;
    height: 4rem;
  }
}
</style>

4、transition-delay


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  transition-delay: 1s;

  &:hover {
    width: 4rem;
    height: 4rem;
  }
}
</style>

几个属性的简写,如下:


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  transition: all 1s ease-in-out 2s;//动画持续一秒,缓进缓出,并在两秒钟后开始执行

  &:hover {
    width: 4rem;
    height: 4rem;
  }
}
</style>

二、animation

  • @keyframes    定义一个动画,@keyframes定义的动画名称用来被animation-name所使用
  • animation-name    检索或设置对象所应用的动画名称 ,必须与规则@keyframes配合使用,因为动画名称由@keyframes定义
  • animation-duration    检索或设置对象动画的持续时间
  • animation-timing-function    检索或设置对象动画的过渡类型
  • animation-delay    检索或设置对象动画的延迟时间
  • animation-iteration-count    检索或设置对象动画的循环次数
  • animation-direction    检索或设置对象动画在循环中是否反向运动
  • animation-play-state    检索或设置对象动画的状态

1、@keyframes

 


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  &:hover {
    animation: mymove 2s;
  }
}
@keyframes mymove {
  0% {
    width: 2rem;
    height: 2rem;
  }
  50% {
    width: 4rem;
    height: 4rem;
  }
  100% {
    width: 2rem;
    height: 2rem;
    margin-left: 5rem;
  }
}
</style>

其实上面的animation: mymove 2s;就是animation-name:mymove  和 animation-duration:2s;  的简写形式

2、animation-timing-function

  • linear    动画从头到尾的速度是相同的。
  • ease    默认。动画以低速开始,然后加快,在结束前变慢。
  • ease-in    动画以低速开始。
  • ease-out    动画以低速结束。
  • ease-in-out    动画以低速开始和结束。
  • cubic-bezier(n,n,n,n)    在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。
     


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  &:hover {
    animation: mymove 2s;
    animation-timing-function: ease-in-out;
  }
}
@keyframes mymove {
  0% {
    width: 2rem;
    height: 2rem;
  }
  50% {
    width: 4rem;
    height: 4rem;
  }
  100% {
    width: 2rem;
    height: 2rem;
    margin-left: 5rem;
  }
}
</style>

3、animation-delay


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  &:hover {
    animation: mymove 2s;
    animation-delay: 1s;
  }
}
@keyframes mymove {
  0% {
    width: 2rem;
    height: 2rem;
  }
  50% {
    width: 4rem;
    height: 4rem;
  }
  100% {
    width: 2rem;
    height: 2rem;
    margin-left: 5rem;
  }
}
</style>

4、animation-iteration-count:检索或设置对象动画的循环次数

animation-iteration-count: infinite;表示无限循环


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  animation: mymove 2s;
  animation-iteration-count: infinite;
}
@keyframes mymove {
  0% {
    width: 2rem;
    height: 2rem;
  }
  50% {
    width: 4rem;
    height: 4rem;
  }
  100% {
    width: 2rem;
    height: 2rem;
    margin-left: 5rem;
  }
}
</style>

当然,还可以指定具体的循环次数,如下:


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  animation: mymove 2s;
  animation-iteration-count: 3;//只循环三次
}
@keyframes mymove {
  0% {
    width: 2rem;
    height: 2rem;
  }
  50% {
    width: 4rem;
    height: 4rem;
  }
  100% {
    width: 2rem;
    height: 2rem;
    margin-left: 5rem;
  }
}
</style>

5、animation-play-state:检索或设置对象动画的状态(running\paused)


<template>
  <div class="test"></div>
</template>
<script>
export default {
  name: "",
  components: {},
  data() {
    return {};
  },
  mounted() {
    setTimeout(() => {
      this.$refs.ref_test.style.animationPlayState = "paused";
    }, 3000);
    setTimeout(() => {
      this.$refs.ref_test.style.animationPlayState = "running";
    }, 5000);
  }
};
</script>
<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  animation: mymove 2s;
  animation-iteration-count: 3;//只循环三次
}
@keyframes mymove {
  0% {
    width: 2rem;
    height: 2rem;
  }
  50% {
    width: 4rem;
    height: 4rem;
  }
  100% {
    width: 2rem;
    height: 2rem;
    margin-left: 5rem;
  }
}
</style>

6、animation-direction;是否反向运动

  • normal    默认值。动画按正常播放。
  • reverse    动画反向播放。
  • alternate    动画在奇数次(1、3、5…)正向播放,在偶数次(2、4、6…)反向播放。
  • alternate-reverse    动画在奇数次(1、3、5…)反向播放,在偶数次(2、4、6…)正向播放。
  • initial    设置该属性为它的默认值。
  • inherit    从父元素继承该属性。
     


<template>
  <div class="test"></div>
</template>

<style lang="scss" scoped>
.test {
  background-color: orange;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;

  animation: mymove 2s;
  animation-iteration-count: infinite;
  animation-direction: alternate;

}
@keyframes mymove {
  0% {
    width: 2rem;
    height: 2rem;
  }
  50% {
    width: 4rem;
    height: 4rem;
  }
  100% {
    width: 2rem;
    height: 2rem;
    margin-left: 5rem;
  }
}
</style>

三、transform

rotate


<template>
  <img src="./二哈.jpg" />
</template>

<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;
  animation: myRotate 2s infinite linear;
}
@keyframes myRotate {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>

 

 

rotateX

 
<template>
  <img src="./二哈.jpg" />
</template>
 
<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;
  animation: myRotateX 2s infinite linear;
}
@keyframes myRotateX {
  0% {
    transform: rotateX(0);
  }
  100% {
    transform: rotateX(360deg);
  }
}
</style>

rotateY


<template>
  <img src="./二哈.jpg" />
</template>

<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;
  animation: myRotateY 2s infinite linear;
}
@keyframes myRotateY {
  0% {
    transform: rotateY(0);
  }
  100% {
    transform: rotateY(360deg);
  }
}
</style>

translate

<template>
  <div>
    <img src="./二哈.jpg" />
  </div>
</template>

<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;

  animation: myTranslate 2s infinite linear;
}
@keyframes myTranslate {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(8rem, 8rem);
  }
}
</style>

translateX

<template>
  <div>
    <img src="./二哈.jpg" />
  </div>
</template>

<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;

  animation: myTranslateX 2s infinite linear;
}
@keyframes myTranslateX {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(8rem);
  }
}
</style>

translateY

<template>
  <div>
    <img src="./二哈.jpg" />
  </div>
</template>

<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;

  animation: myTranslateY 2s infinite linear;
}
@keyframes myTranslateY {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(8rem);
  }
}
</style>

scale

<template>
  <div>
    <img src="./二哈.jpg" />
  </div>
</template>

<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;

  animation: myScale 2s infinite linear;
}
@keyframes myScale {
  0% {
    transform: scale(0, 0);
  }
  100% {
    transform: scale(1, 1);
  }
}
</style>

scaleX

<template>
  <div>
    <img src="./二哈.jpg" />
  </div>
</template>

<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;

  animation: myScaleX 2s infinite linear;
}
@keyframes myScaleX {
  0% {
    transform: scaleX(0);
  }
  100% {
    transform: scaleX(1);
  }
}
</style>

scaleY

<template>
  <div>
    <img src="./二哈.jpg" />
  </div>
</template>

<style lang="scss" scoped>
img {
  width: 3rem;
  height: 4rem;
  border-radius: 50%;

  animation: myScaleY 2s infinite linear;
}
@keyframes myScaleY {
  0% {
    transform: scaleY(0);
  }
  100% {
    transform: scaleY(1);
  }
}
</style>

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS3的变形(transform)、过渡(transition)、动画(animation)是CSS3中非常重要的特性,可以为网页设计带来更加丰富的交互效果和视觉体验。 1. 变形(transform) 变形是指通过CSS3中的transform属性对元素进行平移、旋转、缩放、倾斜等操作,从而改变元素的形状和位置。具体的变形方式包括: 平移(translate):移动元素的位置。 旋转(rotate):以元素中心点为轴心进行旋转。 缩放(scale):缩放元素的大小。 倾斜(skew):倾斜元素。 矩阵变形(matrix):通过矩阵变换实现复杂的变形效果。 示例代码: ``` div { transform: translate(50px, 50px); } ``` 2. 过渡(transition) 过渡是指在元素属性改变时,通过CSS3中的transition属性设置过渡时间和过渡效果,从而实现平滑的转换效果。具体的过渡方式包括: 过渡时间(transition-duration):设置过渡动画的时间,单位可以是秒(s)或毫秒(ms)。 过渡效果(transition-timing-function):设置过渡效果,常用的有linear、ease-in、ease-out、ease-in-out等。 过渡属性(transition-property):设置需要过渡的属性,可以是单个属性或多个属性。 过渡延迟(transition-delay):设置过渡动画的延迟时间。 示例代码: ``` div { transition: all 1s ease-in-out; } ``` 3. 动画(animation) 动画是指通过CSS3中的animation属性对元素进行动画效果的设置。具体的动画方式包括: 关键帧动画(@keyframes):定义一组动画序列,可以设置元素在不同时间点上的样式。 动画时间(animation-duration):设置动画持续时间,单位可以是秒(s)或毫秒(ms)。 动画速度(animation-timing-function):设置动画速度,常用的有linear、ease-in、ease-out、ease-in-out等。 动画延迟(animation-delay):设置动画延迟时间。 动画方向(animation-direction):设置动画播放方向,可以是正方向(normal)、反方向(reverse)、交替播放(alternate)等。 动画次数(animation-iteration-count):设置动画播放次数,可以是无限次(infinite)。 示例代码: ``` div { animation: myanimation 2s ease-in-out infinite; } @keyframes myanimation { 0% { transform: scale(1); } 50% { transform: scale(1.5); } 100% { transform: scale(1); } } ``` 以上就是CSS3中变形、过渡、动画的基本介绍和示例代码,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值