简体   繁体   English

页面标题中的粘性div与页面内容重叠

[英]sticky div in page header overlapping page content

I have a "sticky" div that starts in an absolute position and then switches to fixed at top: 0 once the window begins to scroll (I am using it as a navigation bar), but I also have "in-page" links. 我有一个“粘性” div,它从绝对位置开始,然后切换到top: 0 fixed top: 0一旦窗口开始滚动(我将其用作导航栏),则为0,但是我也有“页内”链接。

My problem is that the sticky overlaps the other content in the body, in other words the top 200px (the size of the navbar) become hidden (beneath the sticky navbar) as soon as they begin to scroll down. 我的问题是,即时贴与主体中的其他内容重叠,也就是说,顶部200px(导航栏的大小)一开始向下滚动,它们就会被隐藏(在sticky下方)。

Is this a CSS problem or a JavaScript problem? 这是CSS问题还是JavaScript问题? How can I fix it? 我该如何解决?

http://jsfiddle.net/b26g1ztu/ http://jsfiddle.net/b26g1ztu/

javascript: 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: 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: 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;
    }

I think your problem is a bit JS and a bit CSS. 我认为您的问题是JS和CSS。

You're using JS/JQuery to toggle between two CSS classes and essentially toggling between absolute and fixed positioning. 您正在使用JS / JQuery在两个CSS类之间切换,并且实际上在绝对定位和固定定位之间切换。 Further you are using top to make your decisions in JS, but they evaluate to different values when you are in absolute or fixed positioning. 此外,您使用top在JS中进行决策,但是当您处于absolutefixed位置时,它们将评估为不同的值。

Finally, i'd recommend that you either (a) just stick with fixed positioning and adjust the location (top/left) onscroll or (b) when you are in .stick mode add padding-top:300px to the body or margin-top:300px on body section:first-child 最后,我建议你要么(一)只要坚持用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