页眉中的粘性div重叠页面内容

时间:2015-04-14 03:16:31

标签: javascript jquery html css

我有一个“粘性”div,它以绝对位置开始,然后在窗口开始滚动时切换到fixed top: 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;
    }

1 个答案:

答案 0 :(得分:0)

我认为你的问题有点JS和一点CSS。

您正在使用JS / JQuery在两个CSS类之间切换,并基本上在绝对和固定定位之间切换。此外,您使用top在JS中做出决定,但当您处于absolutefixed定位时,它们会评估为不同的值。

最后,我建议您(a)坚持fixed定位并调整位置(上/左)onscroll或(b)当您处于.stick模式时将padding-top:300px添加到body上的margin-top:300pxbody section:first-child