YouTube iframe 显示在 `position: sticky` CSS div

YouTube iframe is shown over the `position: sticky` CSS div

我使用导航栏 div 通过 position: sticky; CSS 技巧保持在屏幕顶部。这是伪代码:

CSS:

#hdr_main_menu{
    margin: .1em 0 2em 0;
    padding: 0.9em 0.1em 0.6em 0.1em;
    position: -webkit-sticky; /* Safari */
    position: sticky;
    top: 0;
    background-color: #ffff00;
}
#hdr_mm{
    float: right;
    margin-right: 1em;
    margin-left: 1em;
    text-align: center;
}
#clr{
    clear: both;
}

#main_content{
    margin: 2em 1em 0 1em;
}

这里是 HTML:

<body>
    <div id="hdr_main_menu">
        <nav>
            <div id="hdr_mm">
                <span>menu items</span>
            </div>
            <div id="clr"></div>
        </nav>
    </div>

    <div id="main_content">
        <main> page content </main>
    </div>

</body>

这很好用,除非我在主要内容中嵌入了 YouTube iframe

是CSS:

#yt0{
    max-width: 1024px;
}

.ytp{
    position: relative;
    padding-bottom: 56.25%; /* 16:9 */
    padding-top: 1em;
    height: 0;
}

是HTML:

<div id="yt0">
    <div class="ytp">
    <iframe title="Title" class="ytp" src="https://www.youtube.com/embed/QjHNHonuCQU" frameborder="0" allowfullscreen></iframe>
    </div>
</div>

当我向上滚动此 YouTube iframe 时,它会以 position: sticky 样式在我的顶部导航栏上滚动。

知道如何让它在下方滚动吗?

z-index 或许可以解决问题。 试试这个:

.ytp {
    /* 
       your code
       ....
    */

    z-index: 1000;
    position: relative;
}

您需要在置顶 div 上使用 z-index。使用下面 CSS

#hdr_main_menu{
   z-index: 99;
}

在这里查看:https://codepen.io/rvtech/pen/GRgrMgE