百科狗-知识改变命运!
--

transition-timing-function - 过度效果加速度曲线 - css 过度

梵高1年前 (2023-11-21)阅读数 48#技术干货
文章标签属性

transition-timing-function

版本:CSS3

CSS属性受到transition effect的影响,会产生不断变化的中间值,而CSStransition-timing-function属性用来描述这个中间值是怎样计算的。实质上,通过这个函数会建立一条加速度曲线,因此在整个transition变化过程中,变化速度可以不断改变。

这条加速度曲线被所定义,之后作用到每个CSS属性的过渡.

你可以规定多个timing function,通过使用transition-property属性,可以根据主列表(transition property的列表)给每个CSS属性应用相应的timing function.如果timing function的个数比主列表中数量少,缺少的值被设置为初始值(ease)。如果timing function比主列表要多,timing function函数列表会被截断至合适的大小。这两种情况下声明的CSS属性都是有效的。

示例

transition-timing-function: ease
transition-timing-function: ease-in
transition-timing-function: ease-out
transition-timing-function: ease-in-out
transition-timing-function: linear
transition-timing-function: cubic-bezier(0.1, 0.7, 1.0, 0.1)
transition-timing-function: step-start
transition-timing-function: step-end
transition-timing-function: steps(4, end)

transition-timing-function: ease, step-start, cubic-bezier(0.1, 0.7, 1.0, 0.1)

transition-timing-function: inherit

浏览器支持

IE10以上浏览器都支持transition-timing-function

语法

transition-timing-function:

取值

  • :通过transition-property中定义被过渡属性,每个的值代表与这个属性相对应的timing function.
  • linear:线性过渡。等同于 cubic-bezier(0.0, 0.0, 1.0, 1.0)
  • ease:平滑过渡。等同于 cubic-bezier(0.25, 0.1, 0.25, 1.0)
  • ease-in:由慢到快。等同于 cubic-bezier(0.42, 0, 1.0, 1.0)
  • ease-out:由快到慢。等同于 cubic-bezier(0, 0, 0.58, 1.0)
  • ease-in-out:由慢到快再到慢。等同于 cubic-bezier(0.42, 0, 0.58, 1.0)
  • step-start:等同于 steps(1, start)
  • step-end:等同于 steps(1, end)。
  • cubic-bezier(,,,):特定的贝塞尔曲线类型,4个数值需在[0, 1]区间内

说明:

检索或设置对象中过渡的动画类型。

  • 如果提供多个属性值,以逗号进行分隔。
  • 对应的脚本特性为transitionTimingFunction

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

默认值ease
适用于所有元素,包含伪对象::beforeand::after
继承性
动画性
计算值指定值
媒体交互

例子

transition-timing-function: ease

 
lorem
//CSS .parent { width: 250px; height:125px;} .box { width: 100px; height: 100px; font-size: 20px; left: 0px; top: 0px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: ease; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: ease; } .box1{ width: 50px; height: 50px; color: yellow; font-size: 18px; left: 150px; top:25px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: ease; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: ease; } //JS function updatetransition() { var el = document.queryselector("div.box"); if (el) { el.classname = "box1"; } else { el = document.queryselector("div.box1"); el.classname = "box"; } return el; } var intervalid = window.setinterval(updatetransition, 7000);

transition-timing-function: ease-in

 
lorem
//CSS .parent { width: 250px; height:125px;} .box { width: 100px; height: 100px; font-size: 20px; left: 0px; top: 0px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: ease-in; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: ease-in; } .box1{ width: 50px; height: 50px; color: yellow; font-size: 18px; left: 150px; top:25px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: ease-in; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: ease-in; } //JS function updatetransition() { var el = document.queryselector("div.box"); if (el) { el.classname = "box1"; } else { el = document.queryselector("div.box1"); el.classname = "box"; } return el; } var intervalid = window.setinterval(updatetransition, 7000);

transition-timing-function: ease-out

lorem
//CSS .parent { width: 250px; height:125px;} .box { width: 100px; height: 100px; font-size: 20px; left: 0px; top: 0px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: ease-out; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: ease-out; } .box1{ width: 50px; height: 50px; color: yellow; font-size: 18px; left: 150px; top:25px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: ease-out; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: ease-out; } //CSS function updatetransition() { var el = document.queryselector("div.box"); if (el) { el.classname = "box1"; } else { el = document.queryselector("div.box1"); el.classname = "box"; } return el; } var intervalid = window.setinterval(updatetransition, 7000);

transition-timing-function: ease-in-out

 
lorem
.parent { width: 250px; height:125px;}
.box {
    width: 100px;
    height: 100px;
    font-size: 20px;
    left: 0px;
    top: 0px;
    position:absolute;
    -webkit-transition-property: width height background-color font-size left top color;
    -webkit-transition-duration:2s;
    -webkit-transition-timing-function: ease-in-out;
    transition-property: width height background-color font-size left top color;
    transition-duration:2s;
    transition-timing-function: ease-in-out;
}
.box1{
    width: 50px;
    height: 50px;
    color: yellow;
    font-size: 18px;
    left: 150px;
    top:25px;
    position:absolute;
    -webkit-transition-property: width height background-color font-size left top color;
    -webkit-transition-duration:2s;
    -webkit-transition-timing-function: ease-in-out;
    transition-property: width height background-color font-size left top color;
    transition-duration:2s;
    transition-timing-function: ease-in-out;
}
function updatetransition() {
  var el = document.queryselector("div.box");
   
  if (el) {
    el.classname = "box1";
  }
 else {
    el = document.queryselector("div.box1");
    el.classname = "box";
  }
   
  return el;
}

var intervalid = window.setinterval(updatetransition, 7000);

transition-timing-function: linear

 
lorem
//CSS .parent { width: 250px; height:125px;} .box { width: 100px; height: 100px; font-size: 20px; left: 0px; top: 0px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: linear; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: linear; } .box1{ width: 50px; height: 50px; color: yellow; font-size: 18px; left: 150px; top:25px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: linear; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: linear; } //CSS function updatetransition() { var el = document.queryselector("div.box"); if (el) { el.classname = "box1"; } else { el = document.queryselector("div.box1"); el.classname = "box"; } return el; } var intervalid = window.setinterval(updatetransition, 7000);

transition-timing-function: step-start

lorem
//CSS .parent { width: 250px; height:125px;} .box { width: 100px; height: 100px; font-size: 20px; left: 0px; top: 0px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: step-start; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: step-start; } .box1{ width: 50px; height: 50px; color: yellow; font-size: 18px; left: 150px; top:25px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: step-start; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: step-start; } //CSS function updatetransition() { var el = document.queryselector("div.box"); if (el) { el.classname = "box1"; } else { el = document.queryselector("div.box1"); el.classname = "box"; } return el; } var intervalid = window.setinterval(updatetransition, 7000);

transition-timing-function: step-end

lorem
//CSS .parent { width: 250px; height:125px;} .box { width: 100px; height: 100px; font-size: 20px; left: 0px; top: 0px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: step-end; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: step-end; } .box1{ width: 50px; height: 50px; color: yellow; font-size: 18px; left: 150px; top:25px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: step-end; transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: step-end; } //JS function updatetransition() { var el = document.queryselector("div.box"); if (el) { el.classname = "box1"; } else { el = document.queryselector("div.box1"); el.classname = "box"; } return el; } var intervalid = window.setinterval(updatetransition, 7000);

transition-timing-function: steps(4, end)

lorem
//CSS .parent { width: 250px; height:125px;} .box { width: 100px; height: 100px; font-size: 20px; left: 0px; top: 0px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: steps(4, end); transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: steps(4, end); } .box1{ width: 50px; height: 50px; color: yellow; font-size: 18px; left: 150px; top:25px; position:absolute; -webkit-transition-property: width height background-color font-size left top color; -webkit-transition-duration:2s; -webkit-transition-timing-function: steps(4, end); transition-property: width height background-color font-size left top color; transition-duration:2s; transition-timing-function: steps(4, end); } //JS function updatetransition() { var el = document.queryselector("div.box"); if (el) { el.classname = "box1"; } else { el = document.queryselector("div.box1"); el.classname = "box"; } return el; } var intervalid = window.setinterval(updatetransition, 7000);
transition-timing-function:linear;
-webkit-transition-timing-function:linear;/*safari and chrome*/

transition-timing-function - 过度效果加速度曲线 - css 过度

2、为了更好地理解不同的函数值:这里有五个不同的div元素,用5个不同的值:

实例

#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;}
/*safari*/
#div1{-webkit-transition-timing-function:linear;}
#div2{-webkit-transition-timing-function:ease;}
#div3{-webkit-transition-timing-function:ease-in;}
#div4{-webkit-transition-timing-function:ease-out;}
#div5{-webkit-transition-timing-function:ease-in-out;}

3、和上面的例子一样,但指定速度曲线立方贝塞尔曲线函数:

实例

#div1{transition-timing-function:cubic-bezier(0,0,1,1;}
#div2{transition-timing-function:cubic-bezier(0.25,0.1,0.25,1);}
#div3{transition-timing-function:cubic-bezier(0.42,0,1,1);}
#div4{transition-timing-function:cubic-bezier(0,0,0.58,1);}
#div5{transition-timing-function:cubic-bezier(0.42,0,0.58,1);}
/*safari*/
#div1{-webkit-transition-timing-function:cubic-bezier(0,0,1,1;}
#div2{-webkit-transition-timing-function:cubic-bezier(0.25,0.1,0.25,1);}
#div3{-webkit-transition-timing-function:cubic-bezier(0.42,0,1,1);}
#div4{-webkit-transition-timing-function:cubic-bezier(0,0,0.58,1);}
#div5{-webkit-transition-timing-function:cubic-bezier(0.42,0,0.58,1);}

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)