当使用 transform: translate() 将 flexbox 中所有元素移动时,某些元素在过渡期间会出现位置不正确的情况,然后才会固定在正确位置。我发现这个问题似乎是从第13个元素开始出现的。
更新:目前我已经发现了这个问题只会在 Chrome 和 Edge 中出现,而在 Firefox 中则正常工作。我还简化了代码,并对之前的代码进行了一些更改。此外,当使用 transform 进行偏移时,问题似乎会越来越严重。
更新:问题似乎是由于重新计算图像大小/位置所引起的。当我将 overflow: hidden 添加到图像中时,问题消失了。但我不知道为什么会奏效...
React 代码:
<div
key={index}
className="item-container"
tabIndex={active ? 0 : undefined}
style={{
transform: `${activeItem < itemRefs.current.length - 4 ? `translateX(-${activeItem * 100}%)` : undefined}`,
}}
ref={(ref) => (itemRefs.current[index] = ref)}
>
<img src={url}/>
</div>
CSS:
.item-carousel {
height: 30%;
display: flex;
overflow-x: hidden;
}
.item-container{
display: flex;
box-sizing: border-box;
min-width: 20%;
max-width: 20%;
max-height: 250px;
padding: 5px;
transition: transform 0.25s ease;
}
item-carousel是包含我移动的所有项目的主要flexbox,而item-container则用于单个项目。
我尝试删除CSS文件中几乎所有的内容。只保留item-carousel不变,但问题仍然存在,所以我不认为问题出在那里。
.item-container
中的padding: 5px
替换为.item-carousel
。 - nuser137