【问题标题】:CSS3 - Sticky position not workCSS3 - 粘性位置不起作用
【发布时间】:2016-03-28 21:28:05
【问题描述】:

我正在尝试使用 sticky 定位在标题部分下创建导航菜单,但我无法让它工作,这是我的 css 代码:

    body{

}
header{
    position: relative;
    top: 0px;
    left: 0px;
    height: 100px;
    width: 100%;
    background-color: whitesmoke;
    text-align: left;
    float: left;
    padding: 5px;
    z-index:100;
}
    #logo{
        position: relative;
        height: 85px;
        width: 120px;
        /*border: 1px solid black;*/
        float: left;
        background-image: url('logoArdi.svg');
        background-position: center;
        background-size: contain;
    }
    #judulWeb{
        position: relative;
        height: 85px;
        width: 200px;

        text-align: center;
        float: left;
    }
    #kontak{
        position: inherit;
        float: right;
        height: 100px;
        top: 20px;
        right: 10px;
    }
        .imghead{
            margin: auto 5px auto auto;
        }
    .sticky{
        position: sticky;
        position: -webkit-sticky;
        position: -o-sticky;
        top: 0px;
        float: left;
        width: 100%;
        height:30px;
        background-color: #26282b;
        text-align: center;
        box-shadow: 0px 2px 10px #26282b;
    }

        ul li{
            position:relative;
            bottom:11px;
            display: inline-block;
            margin: auto 30px auto auto;
        }
        ul li{
            color:whitesmoke;

            /*background-color:black;
            border-radius: 5px 5px 0px 0px;
            padding: 5px;*/
        }
        ul li:hover{
            color:#26282b;
            background-color: whitesmoke ;
        }
    #kontak{
        position: inherit;
        float: right;
        height: 100px;
    }
#wrapper{
    position: relative;
    top: 132px;
    width: 80%;
    float: left;
    margin-right: 10%;
    margin-left: 10%;
    text-align: center;
    background-color: transparent;
}
    .imgutama{
        width: 200px;
    }

我现在没有错误,但我只是打算创建页眉和导航,导航在页眉下方,即使我向下滚动也会停留在顶部。

注意:导航类是“粘性的”。

【问题讨论】:

  • position:sticky 仅在 firefox 和 safari 中工作。 (caniuse.com/#search=sticky) 这不是最好的解决方案
  • 然后我被 javascript 卡住了 :) 谢谢你的链接!
  • 如果您已经解决了问题,您可以自己发布答案并选择它作为接受的答案。我们这里的标题中没有“SOLVED”。
  • 我刚刚创建了这个解决方案jsfiddle.net/Nath/Ue7yT。这很简单。一探究竟。也许会对你有所帮助。

标签: html css


【解决方案1】:
#sidebar {
    position: -webkit-sticky;  // required for Safari
    position: sticky;
    top: 0; // required as well.
}   

【讨论】:

  • 您的回答(还)没有帮助。问题中没有#sidebar
猜你喜欢
  • 2020-06-10
  • 1970-01-01
  • 2020-02-29
  • 2020-09-06
  • 1970-01-01
  • 2020-02-22
  • 2018-06-10
  • 2018-07-04
  • 2018-09-25
相关资源
最近更新 更多