【问题标题】:YouTube iframe is shown over the `position: sticky` CSS divYouTube iframe 显示在 `position:sticky` CSS div 上
【发布时间】:2019-12-18 09:25:28
【问题描述】:

我使用导航栏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 样式滚动到我的顶部导航栏。

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

【问题讨论】:

    标签: html css youtube css-position


    【解决方案1】:

    您需要在粘性 div 上使用 z-index。在 CSS 下面使用

    #hdr_main_menu{
       z-index: 99;
    }
    

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

    【讨论】:

    • 太棒了。谢谢!
    【解决方案2】:

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

    .ytp {
        /* 
           your code
           ....
        */
    
        z-index: 1000;
        position: relative;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-20
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 2016-05-22
      • 2023-04-09
      相关资源
      最近更新 更多