CSS3 过渡


CSS3 过渡是元素从一种样式逐渐改变为另一种的效果。
要实现这一点,必须规定两项内容:指定要添加效果的CSS属性;指定效果的持续时间。

一、过渡属性
属性 描述 CSS
transition 简写属性,用于在一个属性中设置四个过渡属性。 3
transition-property 规定应用过渡的 CSS 属性的名称。 3
transition-duration 定义过渡效果花费的时间。默认是 0。 3
transition-timing-function 规定过渡效果的时间曲线。默认是 "ease"。 3
transition-delay 规定过渡效果何时开始。默认是 0。 3
二、实例
span{
    display:block;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    width: 100px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 50px;
}
span.transition{
    display:block;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    width: 100px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    transition: padding-left 0.25s, border-left 0.5s;
    -webkit-transition: padding-left 0.25s, border-left 0.5s;
    -moz-transition: padding-left 0.25s, border-left 0.5s;
}
span:hover{
    padding-left: 5px;
    border-left: 3px solid rgba(0, 155,0, 1);
}

第一个标签无过渡效果,hover动画比较生硬;第二个标签有过渡效果,hover动画比较润滑,用户体验比较好。效果如下:
三、相关属性。
  1. transition-property
    transition-property属性指定CSS属性的nametransition效果(transition效果时将会启动指定的CSS属性的变化)。默认值:all 。
    注意:始终指定transition-duration属性,否则持续时间为0,transition不会有任何效果。
span.width{
    display:block;
    width: 100px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 20px;
    transition-property: width;
    transition-duration: 0.5s;
}
span.width:hover{
    width: 200px;
}
span.height{
    display:block;
    width: 100px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    transition-property: height;
    transition-duration: 0.5s;
}
span.height:hover{
    height: 100px;
}

效果图:
transition-property.gif
  1. transition-duration
    transition-duration属性规定完成过渡效果需要花费的时间(以秒或毫秒计)。默认值:0 。
span{
    display:block;
    width: 100px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 20px;
    transition-property: width;
}
span.one-s{
    transition-duration: 1s;
}
span.two-s{
    transition-duration: 2s;
}
span.three-s{
    transition-duration: 3s;
}
span:hover{
    width: 200px;
}

效果图:
transition-duration.gif
  1. transition-timing-function
    transition-timing-function属性指定切换效果的速度。它可以取一下几个值。默认值:ease 。
描述
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 之间的数值。
span{
    display:block;
    width: 100px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 20px;
    transition-property: width;
    transition-duration: 3s;
}
span.linear{
    transition-timing-function: linear;
}
span.ease{
    transition-timing-function: ease;
}
span.ease-in{
    transition-timing-function: ease-in;
}
span.ease-out{
    transition-timing-function: ease-out;
}
span.ease-in-out{
    transition-timing-function: ease-in-out;
}
span.cubic-bezier{
    transition-timing-function: cubic-bezier(0.2,1,0.2,1);
}
span:hover{
    width: 200px;
}

效果图:
transition-timing-function.gif
  1. transition-delay
    transition-delay属性指定何时将开始切换效果,值是指以秒为单位(S)或毫秒(ms)。默认值:0 。
span{
    display:block;
    width: 200px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 20px;
    transition-property: width;
    transition-duration: 2s;
    transition-timing-function: ease;
}
span.transition-delay-1s{
    transition-delay: 1s;
}
span.transition-delay-2s{
    transition-delay: 2s;
}
span.transition-delay-3s{
    transition-delay: 3s;
}
span:hover{
    width: 400px;
}

效果图:
transition-delay.gif
四、应用实例
  1. 简写
    语法:transition: property duration timing-function delay;,durationdelay可以省略会自动配置为默认值。
span{
    display:block;
    width: 200px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 20px;
    transition-property: width;
    transition-duration: 2s;
    transition-timing-function: ease;
    transition-delay: 1s;
}
//相当于
span{
    display:block;
    width: 200px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 20px;
    transition: width 2s ease 1s;
}
//相当于
span{
    display:block;
    width: 200px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 20px;
    transition: width 2s;
}
  1. 多项简写
    语法:transition: property duration timing-function delay , property1 duration1 timing-function1 delay1 , ...propertyx durationx timing-functionx delayx;;同上,durationdelay可以省略会自动配置为默认值。
span{
    display:block;
    width: 200px;
    height: 30px;
    line-height: 30px;
    padding-left: 2px;
    background: rgba(0, 155,0, 0.25);
    margin-bottom: 20px;
    transition: width 2s , height 2s;
}

做一个简单的人,踏实而务实。不沉溺幻想。不庸人自扰。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 通过 CSS3,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种...
    LorenSLJ阅读 480评论 0 0
  • transition语法简写 在CSS3中,我们可以使用transition属性来将元素的某一个属性从“一个属性值...
    挥剑斩浮云阅读 391评论 0 0
  • 一、过渡(Transition) −允许css的属性值在一定的时间区间内平滑地过渡(一个元素从一种表现形态到另一种...
    越IT阅读 1,088评论 0 0
  • 《红语堂十言集》第八十五章 1、一切与目标与目的为无关的事,都是无意义的,都在浪费时间与精力。 2、人没有敬畏之心...
    红林主人阅读 102评论 0 1
  • 相遇也许是冥冥中的缘分,相离也是为了下次更好的遇见。 回首过往的种种,是那么的美好,那么的科幻,在这三年中我们有过...
    朦胧的夏天阅读 313评论 0 2