CSS中的transition属性

CSS中的transition属性可以让你在一定的时间间隔内平滑地改变一个元素从一个样式到另一个样式。这个属性主要应用于以下几种CSS属性:

  • 宽度(width)
  • 高度(height)
  • 背景颜色(background-color)
  • 颜色(color)
  • 位置(position)
  • 透明度(opacity)
  • 字体大小(font-size)等

例如,如果你想让一个元素的背景颜色在两秒内平滑过渡,可以这样写:

复制代码
div {  
  background-color: red;  
  transition: background-color 2s;  
}

当div的背景颜色发生变化时,它将在一个2秒的过渡期间平滑地从红色变为新的颜色。

此外,还可以使用transition属性来指定转变过程中的速度曲线,例如:

复制代码
div {  
  background-color: red;  
  transition: background-color 2s ease-in-out;  
}

在这个例子中,转变过程的速度曲线是ease-in-out,这意味着转变开始时会缓慢,然后速度会增加,在转变结束前会再次减慢。

其他

在CSS中,transition属性是一个简写属性,用于设置四个过渡属性:transition-property、transition-duration、transition-timing-function和transition-delay。这些属性的顺序是固定的,必须按照指定的顺序排列。

例如,以下代码将使元素的背景颜色在2秒内平滑过渡,并在1秒后开始过渡效果:

复制代码
div {  
  background-color: red;  
  transition: background-color 2s ease-in-out 1s;  
}

选中全部属性:transition:all 3s;

(转换属性名称)

transition-property

(转换时间)

transition-duration

(转换曲线)

transition-timing-function

(延时)

transition-delay

转换曲线

linear(匀速)

ease(慢快慢)

ease-in(慢快)

ease-out(快慢)

ease-in-out(慢快慢)比ease慢些

创建动画

@keyframes 动画名{

from{}

to{

transform:translatex(1222px);

}

}

animation:动画名 1s 1(次数)(infinite无穷) alternate(原路返回) ;

animation:start 4s 1 forwards(停留在最后)

.box:hover{

animation-play-state:paused;

}

相关推荐
zhanghaha13144 小时前
HTML系列教程:18_HTML / CSS 颜色零基础详解
css·html·tensorflow
Aaswk4 小时前
从 HTML/CSS/JS 到 Vue + Axios 的一篇实战笔记
前端·css·vue.js·html
xieliyu.1 天前
前端基础:常见CSS选择器用法
前端·css·笔记·vscode
cll_8692418913 天前
WordPress Single Post 文章页美化:响应式表格、图片优化与自动悬浮 Table of Contents(CSS + JS)
前端·css
愛芳芳3 天前
基于 Electron + Vue3 的仿 PC 微信客户端项目实战
前端·javascript·css·elementui·typescript·electron·vue
feixing_fx3 天前
伪类与伪元素的魔法:纯 CSS 打造炫酷的按钮悬停特效
前端·css·css3
Peter-Code4 天前
微前端避坑指南:主应用与子应用的高度及滚动条协调
css·前端框架·微前端
梦帮科技5 天前
Next.js 16 模块化单体实战:App Router、领域模块与 Route Handler 分层
css·数据结构·链表·正则表达式·node.js·json·html5
我的div丢了肿么办5 天前
顶部区域固定,左侧区域滚动,右侧区域滚动,彼此独立
前端·css
抓不住时间的沙6 天前
Butterfly主题 5.7 导航栏添加相册同时设置相册入口密码
css·python·node.js