CSS实现hover动画时鼠标移开出现跳转闪烁问题如何解决?
问题原因
- 过渡属性仅配置在
hover状态下,鼠标移出元素时过渡规则失效,属性直接跳回初始值 a标签的margin-left没有设置过渡规则,鼠标移出时会直接复位- 伪元素
::before的默认过渡属性仅声明了margin和opacity,和hover态的all规则冲突,也会引发过渡异常
修复后完整代码
/* 给a标签默认添加过渡,保证margin变化全程平滑 */ .product-categories li > a { transition: .3s ease all; } .elementor-widget-wp-widget-woocommerce_product_categories .product-categories li > a:before { background: url("/dropdown.svg") no-repeat center/contain; content: ""; border: none; display: inline-block; height: 1em; opacity: 0; transform: rotate(-90deg); /* 过渡规则统一写在默认状态,移入移出都生效 */ transition: .3s ease all; width: 1em; /* 若默认状态下伪元素占位导致排版偏移,可取消下面一行注释,隐藏时不占用宽度 margin-left: -1em; */ } .elementor-widget-wp-widget-woocommerce_product_categories .product-categories li > a:hover::before { opacity: 1; /* 若启用了上面的margin-left:-1em,可取消下面一行注释 margin-left: 0; */ } .product-categories li > a:hover{ margin-left: 30px; } .elementor-widget-wp-widget-woocommerce_product_categories .product-categories li a:after { content: none; }
修改说明
- 所有过渡规则统一放在元素的非hover默认状态,确保鼠标移入、移出的状态切换都能触发平滑过渡,避免跳转
- 给a标签本身新增过渡规则,保证
margin-left的增减变化全程顺滑 - 移除hover态下重复的transition声明,避免状态切换时规则冲突导致的闪烁问题
内容的提问来源于stack exchange,提问作者Randi Bierbaum
相关产品推荐
相关产品推荐

