CSS3 过渡

作者: hunter97 | 来源:发表于2018-11-23 17:14 被阅读0次

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;
}

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

相关文章

  • 05_css中的过渡和动画效果了解吗

    一、CSS3 过渡 1、CSS3 过渡简介 CSS3过渡是元素从一种样式逐渐改变为另一种的效果。 (1)实现过渡效...

  • 08_dayCSS动画

    CSS3新增的功能有:过渡和动画,阴影和圆角 css3过渡动画: css3都有哪些新增的东西 : 过度,动画,阴影...

  • CSS3动画

    css3动画包括过渡,形变,动画 过渡transition: 指定过渡样式:transition-property...

  • CSS3 过渡

    CSS3 过渡 | transition 属性 属性 如何工作 CSS3过渡是元素从一种样式逐渐改变为另一种的效果...

  • 《响应式Web设计:HTML5和CSS3实战(第2版)》08章

    响应式Web设计:HTML5和CSS3实战(第2版) 第八章 CSS3过渡、变形和动画 过渡——transitio...

  • CSS3-目录

    1 CSS3 兼容性 2 CSS3 选择器 3 CSS3 新加属性 4 CSS3 过渡动画 5 CSS3 变型 6...

  • 九、CSS新特性

    CSS3过渡动画 1、transition-property 设置过渡的属性,比如:width height ba...

  • CSS3之过渡

    一、CSS3过渡简介 CSS3的transition允许CSS的属性值在一定的时间区间内平滑地过渡。这种效果可以在...

  • CSS学习笔记——一些属性

    CSS3 transition 规定应用过渡效果(当指定的 CSS 属性改变时,过渡效果将开始)。 过渡效果通常在...

  • web前端入门到实战:CSS3中的变形(transform)、过

    css3中制作动画的几个属性:css3中的变形(transform)、过渡(transition)、动画(anim...

网友评论

    本文标题:CSS3 过渡

    本文链接:https://www.haomeiwen.com/subject/olyjqqtx.html