Jump to content

Jquery/javascript- else/if statement not refreshing

martinrox1568

hi guys. So I'm having trouble with my website design. My goal is to have the navigation bar start from the bottom and once it reaches the top of the page for it to just stay there. My method for doing this was to use jquery to alter the css once it gets to the top of the page and create the navigation div to be fixed. I know that this isn't the best way to do it most likely, and if you have any better ways to do this, feel free to suggest them. However, by now, I'd just really like an explanation on why its not working. What's happening is if you load the page, the navigation div stays absolute no matter how much i scroll;, but if you scroll down and reload the page, then the navigation div becomes fixed, but it won't change to absolute if i scroll up. I feel like it is a really noob mistake so go easy on me :P Also, be aware this is just a snippet of the code and doesn't even contain the document.ready portion. If you feel like I haven't explained my problem enough, feel free to tell me to expand. 

 

      var totalHeight = $(document).scrollTop();   
      if (totalHeight > 1) {
        $('#nav').css('position,'fixed');
     }

Finally my Santa hat doesn't look out of place

Link to comment
Share on other sites

Link to post
Share on other sites

it would be nice to see how this function is invoked, post more code please

and you're missing an apostrophe after "position"

Link to comment
Share on other sites

Link to post
Share on other sites

So you'd have a navigation vertically aligned and when you're on top of the page it'll stay on the same position but when you scroll it'll scroll with you correct?

 

if so then

$(window).scroll(function () {	if($(window).scrollTop() < 10 ){		$("#navigation").css({			position: "absolute"		});	}else{		$("#navigation").css({			position: "fixed"		});	}}

So this is what happens

If your window position is less then 10, than it'll set #navigation css to absulote (stick to position)

else it'll be greater then 10 so you'd set it to fixed instead (move with window)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×