浏览器无法平滑过渡linear-gradient,应改用background-color或opacity动画模拟,或通过background-position位移实现“流动”效果,transition须写在常态样式中并明确指定属性。
hover时背景渐变不连续,本质是浏览器无法对background-image(比如linear-gradient)做平滑过渡——它默认当作“不可动画属性”,直接跳变。
最稳妥的方式:不直接过渡linear-gradient,而是用纯色+伪元素遮罩模拟渐变效果。例如:
#4a6fa5),再用::before覆盖一层半透明渐变层opacity或background-color,这些属性天然支持transition
如果必须用linear-gradient,可固定渐变色值,只过渡其位置:
linear-gradient(90deg, #ff6b6b, #4ecdc4, #4
4b5b1))background-size: 200% 200%,再用background-position: 0% 50%定位起始点background-position: 100% 50%,配合transition: background-position 0.4s ease
容易忽略的细节:
transition必须写在**常态样式里**(非hover),否则首次hover无过渡transition: all .3s不推荐——可能意外触发其他属性动画;应明确写transition: background-position .3s ease, background-color .3s ease
background简写,hover时务必也用background简写(避免因属性覆盖导致过渡失效)基本上就这些。核心就一条:别指望linear-gradient本身能过渡,换思路——要么用可动画属性模拟,要么用位移骗眼睛。