首页 > 解决方案 > 位置“粘性”在 ReactJS 应用程序中不起作用,并且在父级中没有指定“溢出”属性

问题描述

我正在构建一个 React 应用程序,但position: sticky我的story-header元素有问题。我已经检查了父样式并且没有overflow: hidden属性值。

HTML

<div className='StoryList' >
    <div className='story-header'></div>
</div>

CSS - 样式表

.App {
    width: 100%;
    overflow: visible;
}
.StoryList {
    position: relative;
    margin: 0;
    width: 100%;
}
.story-header {
    width: 100%;
    top: -20px;
    height: 50px;
    left: 0;
    background-color: lightgray;
    position: sticky;
    z-index: 1;
}

为什么story-header滑到上面?

标签: htmlcssreactjssticky

解决方案


我不确定我是否正确理解了您,但它可以正常工作。我已经在纯 html/css 中尝试过它并且它正在工作。检查我的代码,伙计:

.App {
    width: 100%;
    height: 200vh;
    overflow: visible;
}

.StoryList {
    background: #000;
    height: 40vh;
    position: fixed; // you can comment it out, I'm not sure what would you like to have
    margin: 0;
    width: 100%;
}

.story-header {
    width: 100%;
    top: -20px;
    height: 50px;
    left: 0;
    background-color: lightgray;
    position: sticky;
    z-index: 1;
}

检查两个版本 - 有固定位置和没有它。


推荐阅读