盒子阴影悬停过渡闪烁

2022-01-22 00:00:00 slide hover css transition box-shadow

我正在尝试通过使黑色背景从左到右来使 box-shadow 像 img 上的幻灯片动画一样工作.但是如果没有这个奇怪的闪烁问题,我就无法做到.我已经在 Stack Overflow 上寻找解决方案.

I'm trying to make box-shadow work like a slide animation over an img, by making the black background coming over from left to right. But I can't do it without this weird blinking problem. I've already looked for solutions around Stack Overflow.

我也试过用section代替img,但结果是一样的.

I also tried it with a section instead of an img, but the result was the same.

这里是 JSFiddle 演示

HTML

<section>
</section>

CSS

section {
  border:black 1px solid;
  width:500px;
  height:200px;
  transition:1s ease-out
}

section:hover{
  box-shadow:inset 500px 0 0 #222;
  float:left;
  transition:1s ease-out;
}

推荐答案

这似乎是 box-shadow 绘制方式的结果.尝试另一种方法.这是一个无闪烁的解决方案,它可以加厚左边框而不是添加框阴影:

This appears to be a result of the way box-shadow is painted. Try another approach. Here's a flicker-free solution that thickens the left border instead of adding a box-shadow:

div {
  position: relative;
  display: inline-block;
}
section {
  border: black 1px solid;
  width: 500px;
  height: 200px;
  transition: 1s ease-out;
  box-sizing: border-box;
}

div:hover section {
  border-left-width: 500px;
  float: left;
  transition: 1s ease-out;
}

section~* {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  color: #888;
}

<div class="prog">
  <section></section>
  <p>Here's some text content.</p>
</div>

https://jsfiddle.net/k5kznmvL/

相关文章