CSS3 过渡

2018-09-29  本文已影响0人  林键燃

CSS3 过渡 | transition 属性

属性

// 简写属性,用于在一个属性中设置四个过渡属性。
transition
// 规定应用过渡的 CSS 属性的名称。
transition-property
// 定义过渡效果花费的时间。默认是 0。
transition-duration
// 规定过渡效果的时间曲线。默认是 "ease"。
transition-timing-function
// 规定过渡效果何时开始。默认是 0。
transition-delay

如何工作

CSS3过渡是元素从一种样式逐渐改变为另一种的效果。
实现这一点需要满足两项要求:

实例
<style>
    img {
      width: 50px;
      height: 50px;
      border: 1px solid green;
      transition: width 1s;
    }
</style>
<body>
  <img src="https://cn.vuejs.org/images/menu.png">
</body>

注意: 如果未指定transition-duration属性,transition将没有效果,因为默认值为0。
指定的CSS属性的值更改时效果会发生变化。典型的实例是,CSS属性的变化是用户鼠标放在一个元素上时:

<style>
    img:hover {
      width: 100px;
      height: 100px;
      border: 1px solid greenYellow;
      content: url('https://cn.vuejs.org/images/logo.png')
    }
</style>
<body>
  <img src="https://cn.vuejs.org/images/menu.png">
</body>

注意:当鼠标的光标移动到该元素时,它逐渐改变它原有样式

多项改变

添加多个样式的变换效果,添加的属性有逗号分隔:

实例
<style>
    img {
      width: 50px;
      height: 50px;
      border: 1px solid green;
      transition: width 1s, height 1s;
    }
</style>
<body>
  <img src="https://cn.vuejs.org/images/menu.png">
</body>

过渡属性

实例

在一个例子中使用所有过渡属性:

<style>
    img {
      width: 50px;
      height: 50px;
      border: 1px solid green;
      transition-property: width, height;
      transition-duration: 1s, 1s;
      transition-timing-function: linear, linear;
      transition-delay: 1s, 1s;
    }
</style>
<body>
  <img src="https://cn.vuejs.org/images/menu.png">
</body>
实例

与上面的例子相同的过渡效果,但是使用了简写的 transition 属性:

<style>
    img {
      width: 50px;
      height: 50px;
      border: 1px solid green;
      transition: width 1s linear 1s, height 1s linear 1s;
    }
</style>
<body>
  <img src="https://cn.vuejs.org/images/menu.png">
</body>
上一篇 下一篇

猜你喜欢

热点阅读