Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript nav bar changing position?

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #188399
    Doodayer
    Participant

    so in a basic rundown of what i want to happen, the nav bar should always be fixed at the top of the screen, which i’ve effectively done. but when i’m at the top of the home webpage, i would like for my logo to go above my nav bar and for the nav bar to stay securely below it. once i scroll down, the nav bar can move back up to its position and the top of its screen and stay there. I honestly have no clue how to do this, and wouldn’t begin to know how to summarize that in three key words for a google search.
    http://codepen.io/Doodayer/pen/KwKxqg

    if theres a more efficient way of using the image as a link, feel free to inform me. I’m fairly new to this, as in maybe 3 hours of total writing experience. thanks for all the help.

    #188400
    deeve007
    Participant

    Use Jquery.
    Default has navbar positioned relatively so it sits below the logo.
    Then when y position of nav bar reaches “0” use Jquery to change its position to “fixed” at the top of the screen.
    Then when bottom y position of the logo is >0, revert the position of the navbar back to default.

    #188430
    Doodayer
    Participant

    Ok i don’t have much experience with jquery, so heres what i have so far:

    /*jquery navbar*/
    var x = $(".nav").position().top;
    
    if (x = 0) {
        $(".nav").css("position", "fixed");
    }else{
        $(".nav").css("position", "relative");
    };

    I’m not sure if those are the methods i should use, or how to properly find the y position of the nav bar, but thats what i’ve got.

    also a weird bug i’m getting is the nav bar doesn’t have the full width, even though its set to 100%.

    #188439
    Paulie_D
    Member
    #188440
    deeve007
    Participant

    Not quite. Elements need to start off with position:relative, and then switch to position: fixed, and then vice versa when scrolling back.

    #188441
    deeve007
    Participant

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

    (click on the “edit” link to view it non-embedded)

    #188442
    Paulie_D
    Member

    Not sure position:relative is necessary…the ‘staticswitching tofixed` is equally valid BUT it does look better.

    :)

    #188443
    deeve007
    Participant

    But it’s what he wants. The nav needs to move up with the logo/content, and THEN become fixed at the top of the screen.

    Why would you want the nav to be “stuck” part way down the screen?? (until the logo disappears)

    #188444
    Paulie_D
    Member

    It’s fixed below the header / logo and sticks to the top when the page is scrolled..which he wanted.

    Yours is better though…as always, there’s many ways of doing something.

    #188446
    deeve007
    Participant

    Well we’ve certainly interpreted what he was after in different ways, but I’m sure he can make his choice with the info we’ve given him now.

    #188464
    Doodayer
    Participant

    thank you both for all the help. it definitely helped me get a better understanding of what i needed to do.

    #188467
    Doodayer
    Participant

    I’m having another issue. for some reason the code all works in the codepen, but when i run it in firefox, it doesn’t work. Possible reasons?

    #188468
    Doodayer
    Participant

    doesn’t work in chrome either.

    #188469
    deeve007
    Participant

    Have you included the required library(ies)? (eg: jquery)

    #188470
    Doodayer
    Participant

    thats what i’m having issues with. when i try to download jquery it takes me into a webpage full of open javascript. should it do this? also if i copy all of that can i just access it like this?

    <script type=”text/javascript” src=”jquery.js”></script>

Viewing 15 posts - 1 through 15 (of 21 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.