为什么列没有粘附到其父div?滚动时,左侧和右侧粘性容器滚动到RecommendedProducts div的顶部。预期行为是当它们到达其包含父div的底部时,它们应该停止/粘附。
代码和框:https://codesandbox.io/s/fragrant-darkness-uefhr2?file=/src/index.js
<>
<div className="h-full w-screen">
<div className="bg-[#EFEFEF] relative h-auto w-full pt-12">
{images &&
images.map((image) => (
<div className="flex items-center justify-center" key={image.id}>
<div className="relative h-[calc(100vh-48px)] w-full">
<img
src="https://i.ibb.co/mDTWnb2/hej.webp"
alt={image.alt}
className="object-contain w-full h-full"
/>
</div>
</div>
))}
<div className="fixed top-0 w-full">
<div className="mx-auto flex w-11/12 flex-col items-center justify-center space-y-8 sm:grid sm:w-full sm:max-w-screen-3xl sm:grid-cols-3 sm:space-y-0 sm:px-6 lg:px-8">
{/* Desktop */}
{/* Left column */}
<div className="flex h-screen w-full items-center justify-center self-start sm:sticky sm:inset-y-0">
<div className="flex w-[300px] flex-col items-center justify-start bottom-0 overflow-y-auto">
<ProductDescription />
</div>
</div>
{/* Middle column */}
<div className="hidden sm:block"></div>
{/* Right column */}
<div className="flex h-screen w-full items-center justify-center self-start sm:sticky sm:inset-y-0">
<div className="flex w-[200px] flex-col items-center justify-start bottom-0 overflow-y-auto">
<ProductDescription />
</div>
</div>
</div>
</div>
</div>
</div>
<RecommendedProducts />
</>
1条答案
按热度按时间np8igboo1#
position: sticky
要求(i)父元素具有height
属性;(ii)已经用类似top
、bottom
、right
或left
的属性指定了阈值。因此,要实现您希望在示例中实现的行为,您需要做两件事:
(i)将一个
height
设置为作为列容器的div
。如果你想保持它的灵活性,你不能将它的height
和其父的height
设置为full
。在tailwind中,这是h-full
类。(ii)将
position: sticky
和top: 0
属性添加到列中,在Tailwind中这是sticky
和top-0
类。