CSS3中,我们为了添加某种效果可以从一种样式转变到另一个的时候,无需使用Flash动画或JavaScript。
通过过渡transition,可以让web前端开发人员不需要javascript就可以实现简单的动画交互效果。
它的工作原理就是元素从一种样式逐渐改变为另一种的效果
要实现这一点,必须规定两项内容:指定要添加效果的CSS属性
指定效果的持续时间。
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
div:hover {
width: 300px;
}
注意:如果未指定的时间,transition将没有任何效果,因为默认值是0。
transition支持多项改变
要添加多个样式的变换效果,添加的属性由逗号分隔:
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s, height 2s
}
div:hover {
width: 300px;
height: 200px;
}
transition 有四个子属性:
transition-property: 规定应用过渡的 CSS 属性的名称
transition-duration: 定义过渡效果花费的时间。默认是 0。
transition-delay: 规定过渡效果何时开始。默认是 0。
transition-timing-function: 规定过渡效果的时间曲线。默认是 "ease"。
transition-property指定需要过渡的CSS属性。
并不是所有属性都能过渡的,只有能数字量化的CSS属性才能过渡。
如 width,height,top,right,bottom,left,opacity 等等。
像 displat:block这些显示隐藏就没有过度效果
transition-property有三个属性
none 没有属性会获得过渡效果。
all 所有属性都将获得过渡效果。
property 定义应用过渡效果的 CSS 属性名称列表,列表以逗号分隔。
demo:
div {
width: 100px;
height: 100px;
background: red;
transition: all 2s
}
div:hover {
width: 300px;
height: 200px;
}
transition-duration 过渡需要的时间
单位可指定s秒,也可指定ms毫秒。默认值是0,表示立刻变化
transition-timing-function 属性指定切换效果的速度。
div {
width: 100px;
height: 50px;
background: red;
margin-bottom: 5px;
transition: all 2s
}
#div1 {transition-timing-function: linear;}
#div2 {transition-timing-function: ease;}
#div3 {transition-timing-function: ease-in;}
#div4 {transition-timing-function: ease-out;}
#div5 {transition-timing-function: ease-in-out;}
div:hover {
width: 300px;
}
transition-delay 属性指定何时将开始切换效果。
transition-delay值是指以秒为单位(S)或毫秒(ms)。
默认值为 0 表示不延迟,立即开始过渡动作
div {
width: 100px;
height: 100px;
background: red;
text-align: center;
margin-bottom: 5px;
transition-property:width;
transition-duration:3s;
transition-delay:1s;
}
div:hover {
width: 300px;
}
这里可以看到,过度效果会停顿 1 秒后产生效果。知乎视频www.zhihu.comhttps://www.bilibili.com/video/BV1Xz411e7Uy/?p=104www.bilibili.com
记得关注收藏哦!!!