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;

}

相关推荐
吴长建先生16 小时前
Flutter IM 即时聊天应用开发:从 0 到 1 实
css
htzyl2062 天前
前端样式-弹窗淡入淡出,为什么你的 `transition` 一直不生效
前端·css·html
Brookty2 天前
CSS基础知识(二):盒模型、内外边距、外边距折叠与父子元素的布局关系
前端·css
Mh2 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
Brookty3 天前
CSS基础知识(一):选择器、优先级、层叠与继承、边框属性及盒模型
前端·css
htzyl2063 天前
CSS中,`:has()` 能帮你删掉的 4 类 JS
前端·css
jianqiang.xue4 天前
Espressif-IDE进入维护模式:乐鑫把ESP32开发的宝押给VS Code
前端·css·ide
feixing_fx4 天前
CSS 变量(Custom Properties):构建可扩展的主题切换系统
前端·css·前端框架·html·css3
晓得迷路了5 天前
栗子前端技术周刊第 154 期 - Parcel 3 Alpha、JS 调查报告、Vite 漏洞...
前端·javascript·css
ZhengEnCi7 天前
登录页电脑端怎么设计才不空:5 种布局方案与可粘贴 AI 提示词
前端·css·设计