簡體   English   中英

頁面標題中的粘性div與頁面內容重疊

[英]sticky div in page header overlapping page content

我有一個“粘性” div,它從絕對位置開始,然后切換到top: 0 fixed top: 0一旦窗口開始滾動(我將其用作導航欄),則為0,但是我也有“頁內”鏈接。

我的問題是,即時貼與主體中的其他內容重疊,也就是說,頂部200px(導航欄的大小)一開始向下滾動,它們就會被隱藏(在sticky下方)。

這是CSS問題還是JavaScript問題? 我該如何解決?

http://jsfiddle.net/b26g1ztu/

javascript:

function sticky_relocate() {
    var window_top = $(window).scrollTop();
    var div_top = $('#sticky-anchor').offset().top;
    if (window_top > div_top) {
        $('#sticky').addClass('stick');
    } else {
        $('#sticky').removeClass('stick');
    }
}

$(function () {
    $(window).scroll(sticky_relocate);
    sticky_relocate();
});

HTML:

<!--navigation with logos-->
<div id="sticky-anchor"></div>
<div id=sticky>
    <a href="#lccpost">
        <img alt="lansing" src="https://pbs.twimg.com/profile_images/558329813782376448/H2cb-84q_reasonably_small.jpeg">
    </a>
        </div>
<!--Articles-->

<!--Nav pics-->
<section>
    <div id=lcc1>
        <a name="lccpost"><img alt="lansing" src="https://pbs.twimg.com/profile_images/558329813782376448/H2cb-84q_reasonably_small.jpeg"></a>
    </div>
</section>
<!--titles-->
<section>
    <div id=submissions><h2>Submissions</h2></div>
<!--single submissions-->
    <div class=name>
        <h3>John Doe</h3>
    </div>
    <div class=subs>
        <a href="submissions/1.%20News%20Story/The%20Lookout_News%20Story_Shelby%20Schueller.pdf" target=_blank>
            <img src="http://www.wolfson.org.uk/media/1187/pdf_icon.png" /></a>
    </div>
    <div class=judgesub>
        <!--<a href="">
            <img src="pictures/PDF_Logo.jpg" /></a>-->
    </div>
<!---->
    <div class=name>
        <h3>Jane Doe</h3>
    </div>
    <div class=subs>
        <a href="submissions/1.%20News%20Story/The%20Lookout_News%20Story_Sarah%20Spohn.pdf" target=_blank>
            <img src="http://www.wolfson.org.uk/media/1187/pdf_icon.png" /></a>
    </div>
    <div class=judgesub>
        <!--<a href="">
            <img src="pictures/PDF_Logo.jpg" /></a>-->
    </div>
<!---->
    <div class=name>
        <h3>Jason Doe</h3>
    </div>
    <div class=subs>
        <a href="submissions/1.%20News%20Story/The%20Lookout_News%20Story_Jeremy%20Kohn.pdf" target=_blank>
            <img src="http://www.wolfson.org.uk/media/1187/pdf_icon.png" /></a>
    </div>
    <div class=judgesub>
        <!--<a href="">
            <img src="pictures/PDF_Logo.jpg" /></a>-->
    </div>

CSS:

body 
{
    background-color: RGB(95,0,0);  
}
#sticky
{
    position:absolute;
    top:150px;
    background-color: RGB(65,0,0);
    color:White;
    border-style:solid;
    border-color:RGB(255,215,0);
    padding: 5px;
    width: 500px;
    height: 150px;
    overflow: hidden;
    overflow-y: scroll;
    }

#sticky.stick {
    position: fixed;
    top: 0px;
    bottom:auto;
    z-index: 10000;
}

#lcc1
{
    position:absolute;
    top: 350px;
    left: 20px;
    }
#submissions
{
    position:absolute;
    top: 320px;
    left: 240px;
    color:White;
    }

.name
{
    position:relative;
    top:400px;
    left: 150px;
    color:White;
    }  

.subs
{
    display:inline-block;
    position:relative;
    top: 330px;
    left: 270px;
    border-style: dashed;
    border-color:Red;
    padding:5px;
    }

我認為您的問題是JS和CSS。

您正在使用JS / JQuery在兩個CSS類之間切換,並且實際上在絕對定位和固定定位之間切換。 此外,您使用top在JS中進行決策,但是當您處於absolutefixed位置時,它們將評估為不同的值。

最后,我建議你要么(一)只要堅持用fixed定位和調整位置(上/左)onscroll或(b)當你在.stick模式中添加padding-top:300pxbodymargin-top:300px body section:first-child上的margin-top:300px body section:first-child

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM