admin管理员组

文章数量:1390254

I have a fixed header with on my website at 65px tall. I have a secondary navigation about 3/4 down the page that I want to fix to the bottom of my header once scrolled to it.

I've used Josh Lee's answer on this post to get the functionality to work, however, because my header is fixed, the secondary navigation scrolls right past it and bees fixed once it hits the top of the page.

Since it pletely bypasses my header, how can I set an offset for the trigger so that it happens 65px from the top of the screen?

In my <head>:

<script>
function moveScroller() {
    var move = function() {
        var st = $(window).scrollTop();
        var ot = $("#scroller-anchor").offset().top;
        var s = $("#mydiv");
        if(st > ot) {
            s.css({
                position: "fixed",
                top: "65px"
            });
        } else {
            if(st <= ot) {
                s.css({
                    position: "relative",
                    top: ""
                });
            }
        }
    };
    $(window).scroll(move);
    move();
}
</script>

On my page:

<div id="scroller-anchor"></div>
<div id="mydiv" class="">
    <ul class="wrap">
        <li> ... </li>
        <li> ... </li>
        <li> ... </li>
        <li> ... </li>
    </ul>
</div>
<script type="text/javascript"> 
  $(function() {
    moveScroller();
  });
</script> 

I have a fixed header with on my website at 65px tall. I have a secondary navigation about 3/4 down the page that I want to fix to the bottom of my header once scrolled to it.

I've used Josh Lee's answer on this post to get the functionality to work, however, because my header is fixed, the secondary navigation scrolls right past it and bees fixed once it hits the top of the page.

Since it pletely bypasses my header, how can I set an offset for the trigger so that it happens 65px from the top of the screen?

In my <head>:

<script>
function moveScroller() {
    var move = function() {
        var st = $(window).scrollTop();
        var ot = $("#scroller-anchor").offset().top;
        var s = $("#mydiv");
        if(st > ot) {
            s.css({
                position: "fixed",
                top: "65px"
            });
        } else {
            if(st <= ot) {
                s.css({
                    position: "relative",
                    top: ""
                });
            }
        }
    };
    $(window).scroll(move);
    move();
}
</script>

On my page:

<div id="scroller-anchor"></div>
<div id="mydiv" class="">
    <ul class="wrap">
        <li> ... </li>
        <li> ... </li>
        <li> ... </li>
        <li> ... </li>
    </ul>
</div>
<script type="text/javascript"> 
  $(function() {
    moveScroller();
  });
</script> 
Share Improve this question edited May 23, 2017 at 10:24 CommunityBot 11 silver badge asked Jan 15, 2015 at 18:23 pruoccojrpruoccojr 4821 gold badge3 silver badges19 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 1

I believe you will need to account for the space when you are checking when to change the position to fixed for example change ot variable to

var ot = $("#scroller-anchor").offset().top - 65;

I don't know if this could be considered the "correct" way of solving my problem, but it seemed to have worked for me... I added top:-65px to the scroller-anchor element.

<div id="scroller-anchor" style="top: -65px;"></div>

本文标签: javascriptHow can I set an offset on my quotstickyquot navigationStack Overflow