0

Okay, I'm trying to get my navbar to stick to the bottom of a 25px tall header that I have at the top of my page. I only want my navbar to stick (become fixed — position: fixed) when it reaches the bottom of that header.

A link to the CodePen I set up can be found below. I apologize for all the extra CSS and JavaScript/jQuery stuff you'll see.... It's all part of the site/design though.

I just can't get my navbar (located at the bottom of the screen) to become fixed when it reaches the bottom of the 25px (black-ish) header at the top of the screen. I've tried 10+ solutions from different places and none of them are doing the trick for me....

http://codepen.io/anon/pen/WrZjWG

Joshua Huff
  • 25
  • 10
  • Why do you have multiple `$(document).ready`? You only need one. This also goes for your anonymous `$(function(){ `. Either encapsulate your code in a single `$(function(){ ` or inside a single `$(document).ready` – AGE Jan 15 '16 at 21:10
  • I think you'll need to do a calculation based on vh and the width of your navbar to know when it gets to the header, then apply a class that has the position:absolute; property and set to the top of the page minus the width of the header – Cruiser Jan 15 '16 at 21:13
  • 1
    @AGE I've just recently started messing with JavaScript/jQeury so I don't know much about it and didn't even realize I had multiple. I'll definitely go fix that. – Joshua Huff Jan 15 '16 at 21:24
  • @Cruiser thanks for the suggestion! – Joshua Huff Jan 15 '16 at 21:24
  • @Portal_Zii maybe you'll have better luck than me. I'm not very experienced with JavaScript or jQuery at all....so I'm sure you will. Lol. And I know I could leave it at
    (not close it) but
    doesn't change anything (it still functions the same) and it just looks odd to me and makes me feel like something is missing or like I'm not doing something right if I don't close it. Lol.
    – Joshua Huff Jan 15 '16 at 21:24
  • @Portal_Zii haha. I'll keep that in mind. Lol. I'm just in the habit of doing
    and putting each break on its own line for the purpose of being able to tell how many breaks I have easier. In this case, however, there's no need to know how many because they won't be staying... (^_^;) Thanks a lot! I definitely look forward to seeing if you're able to figure anything out! :D
    – Joshua Huff Jan 15 '16 at 21:30
  • @JoshuaHuff my best advice so that you *learn* from this, is to try to create your own from scratch. Do not focus on making it look good at all, only on the fact that it works first. That way you can abstract a lot of extra code and focus precisely on the task at hand. In which case, making it look pretty and building on top of your solution will become a much better approach, now and in the future. Make sure you save your solution somewhere, so you can reference it and apply it anywhere else you like. – AGE Jan 15 '16 at 21:41
  • @AGE Thanks for the suggestion. I'm actually looking around different places to figure out more of the structure and syntax for JavaScript and jQuery for that very reason. :) – Joshua Huff Jan 15 '16 at 21:45
  • @JoshuaHuff easiest and free-est way to get started: **[Javascript](https://www.codecademy.com/learn/javascript)**, **[JQuery](https://www.codecademy.com/learn/jquery)**... if that's not good enough it's very worthwhile spending a few in a tutorial site. I think sites such as codeschool provide a very good learning material, lots of free beginner lessons and the price is very fair for the pro account. Not trying to advertise here, I just happened to learn a lot through them myself years back. – AGE Jan 15 '16 at 21:52
  • @AGE I love Codecademy! That's where I first started messing with HTML and CSS. Lol. – Joshua Huff Jan 15 '16 at 21:55
  • @Portal_Zii thanks for letting me know. Glad you're enjoying it. Lol. Hopefully it's not proving to be too much of a headache for you... – Joshua Huff Jan 15 '16 at 22:28

4 Answers4

1

Try this script

$(function() {
    var offset = $("#navbar").offset(),
        topPadding = 15;

    $(window).scroll(function() {
        if ($("#sidebar").height() < $(window).height() && $(window).scrollTop() > offset.top) {
            $("#sidebar").stop().animate({
                marginTop: $(window).scrollTop() - offset.top + topPadding
            });
        } else {
            $("#sidebar").stop().animate({
                marginTop: 0
            });
        };
    });
});
AGE
  • 3,752
  • 3
  • 38
  • 60
La Fortuna
  • 89
  • 6
  • 1
    Can you please explain **what** this does? The person who asked the question is trying to learn JavaScript/JQuery – AGE Jan 15 '16 at 21:37
  • @AGE Lol. Thanks. I was just about to ask what exactly is going on in this script. – Joshua Huff Jan 15 '16 at 21:43
  • @JoshuaHuff you need to understand what **[offset](http://api.jquery.com/offset/)**, **[scroll](http://api.jquery.com/scroll/)**, **[(function(){/* some code here*/}();](http://stackoverflow.com/a/9899701/1046690)** and **[animate](http://api.jquery.com/animate/)** do so you can see the above in action. Oh and **[this here](http://api.jquery.com/jQuery/)** is a nice read for the uninitiated. – AGE Jan 15 '16 at 21:47
1

What you need to do is add a class to your nav so first since you have your nav absolutely positioned in the window we will find the window height and minus your nav and the top bar that you have and then add and remove classes from there. The following will work:

$(window).on('scroll', function () {
  if ( $(window).scrollTop() >= $(window).height() - 105) {
    $( '#mainNav' ).addClass("scrolled");
  }else{
    $( '#mainNav' ).removeClass("scrolled");
  }
});

Then your css

#mainNav.scrolled { /* This will only take place when the navbar reaches the set point on the page */
  position: fixed;
  z-index: 99;
  top:25px;
  left:0;
}

Normally you would use $('div').offset().top instead of $(window).height() - 105 but your nav is absolutely positioned in the window and not in any relative div which will cause inconsistent results so we will find the windows height and then subtract the height of your nav and the top bar. Now your nav should be fixed in the correct position once it is scrolled to the bottom of your top bar. Here is a working codepen CodePen

Steve K
  • 8,505
  • 2
  • 20
  • 35
  • That's definitely a great start! Thank you so much! Only problem is that my navbar only goes to a little beneath halfway up the page and then jumps all the way to the fixed position underneath the black header. Do you have any suggestions as to how I can keep it from doing that or why it might be doing that? – Joshua Huff Jan 15 '16 at 22:35
  • It is because more px needed to be added because of other elements I have added 115 to it and it seems to be working fine now I will edit my answer to reflect that It is hard for me to see all of the different elements that you have here because there is just a pink page basically but you may need to add and subtract px dependint on what you put above and below it but here is a working codepen. http://codepen.io/wamosjk/pen/EPwXJq – Steve K Jan 15 '16 at 22:44
  • Not really sure what you all have in there but my pen seems to have some incosistencies in it but you need to find out what your top offset is going to be and then go from there. I will look at it a little better and see what the issue is – Steve K Jan 15 '16 at 22:46
  • OK so after I looked at it a little bit I see that you have your nav absolutely positioned in nothing except the body and not wrapped in any relative div. This is what is causing the inconsistency. So if you look at the codepen now it will be correct and not jumpy. So what is happening is I changed the if statement to include the window height minus the height of the top bar that you have along with your nav's height as well. Normally you would just use $('div').offset().top but with your absolute positioning it won't work. I will edit my answer to reflect all of this – Steve K Jan 15 '16 at 23:24
  • :) Do I need to change anything other than adding the new script? Like, is there anything in the CSS that is supposed to change? – Joshua Huff Jan 15 '16 at 23:30
  • Thank you so much for the help! I edited the script to match your script and it works perfectly now! You've helped me a TON! – Joshua Huff Jan 15 '16 at 23:33
  • No I think it should be working just fine as is I edited the answer to reflect all of this along with a link to a working codepen check out the pen and see if you can find any bugs. If you do let me know but I think it should be working fine. – Steve K Jan 15 '16 at 23:34
  • It will only work with your current resolution. Requiring to have your banner a fixed height instead of adapting to entire height available. – Patrick Jan 16 '16 at 01:59
1

There is a nacent (supported in Firefox, Safari, and iOS6+; in development in Chrome; "under consideration" in Edge; more data at caniuse.com) CSS property that can achieve this:

position: sticky

There are also a few polyfills to simulate the behaviour of position: sticky in other browsers:

These are all much more robust than the few hand-rolled suggestions above.

Félix Saparelli
  • 8,424
  • 6
  • 52
  • 67
0

It works! Add the height of #landingHeaderWrapper in the jquery as I did here http://codepen.io/vishnu1212/pen/jWGwop
Then add a new class with position property like

.sticky{ postion:fixed;}

Use AddClass and removeClass functions to toggle this class

vishnu
  • 624
  • 1
  • 6
  • 21