views:

749

answers:

2

I have a floating menu(absolutely positioned) that stays in view as the user scrolls down the page, the problem is i have quite a large footer and if you scroll all the way to the bottom of the page it will clash with the footer.

I just want to make it stop say 400px from the bottom of the page.. Does anyone know if this can be done?

Here is the code:

var name = "#about";  
var menuYloc = null;  

 $(document).ready(function(){  
     menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))  
     $(window).scroll(function () {   
         var offset = menuYloc+$(document).scrollTop()+"px";  
         $(name).animate({top:offset},{duration:500,queue:false});  
     });  
 });

Thanks in advance!

Ryan

+1  A: 

Try something like this (untested):

var name = "#about";  
var menuYloc = null;  

$(document).ready(function(){
 var menu = $(name);
 menuYloc = parseInt(menu.css("top").substring(0,menu.css("top").indexOf("px")));
 var height = parseInt( menu.attr( 'offsetHeight' ) + ( parseInt( menu.css( 'marginTop' ) ) || 0 ) + ( parseInt( menu.css( 'marginBottom' ) ) || 0 ) );
 var footerYLoc = $('#footer').offset().top;
 $(window).scroll(function () {   
  var offset = menuYloc+$(document).scrollTop();
  if ( (offset + height) >= footerYloc) offset = footerYloc - height;
  menu.animate({top:offset+"px"},{duration:500,queue:false});  
 });


});
PetersenDidIt
thanks petersendidit, this didnt seem to work though. The menu stopped moving all together, but i couldnt seem to find what made it break...
Ryan
+3  A: 
SolutionYogi
thanks SolutionYogi, but this caused it to break as well. hmm im not sure why though :S
Ryan
What exactly did break? Is it not scrolling at all? Is it overlapping the footer?
SolutionYogi
its not scrolling at all
Ryan
Yep, there was a small bug (mistyped a variable name). Created the demo page, check out my original answer.
SolutionYogi
Thanks SolutionYogi.. your demo page does not work, but the code you pasted does, i didnt check the code to see why the demo doesnt work though. Anyway, it worked pretty well so thankyou. But one thing that isnt quite right is that if the window is sized at 1024*800 the 'bottom rule' takes precedence over the 'top rule' making the menu move up out of sight
Ryan
do you know how we can make the 'top rule' a higher priority than the 'bottom rule' ?
Ryan
I am really not sure if I understand you. Check out the demo page again. I also found that I don't need to make that many calculations. The main code has been edited to be simpler. See if this works for you.
SolutionYogi
And yes, it will be nice if you could upvote/mark as answer if the code has worked for you. :)
SolutionYogi
the demo still doesnt work, the update is more efficient, but what i meant was that if say #about is 500px high, and the space for it to fit, before the footer starts is only 300px (because i use some slide down effects to get more space when needed), it will sit on the footer, and encroach into the header.. is there a way to prioritize the 'site at the top' rule rather than the 'dont encroach on the footer' rule? If not just let me know and I can put white space at the bottom to fix that. Have you noticed in FF, as soonas you start to scroll, #about jumps to the top of the window?
Ryan
i marked as answer, i will upvote as soon as i get 15 rep and it allows me too
Ryan
Could you tell me which browser are you using? I am able to get the demo to run in Firefox/Chrome.Also, I understood what you meant by 'top' rule being more important than bottom rule. I updated the code so that the floating menu never goes off the window. Let me know if this code change works.
SolutionYogi
+1 for perseverance :)
Darko Z
Thanks Darko Z. Could you confirm if the Demo is working for you?
SolutionYogi
I am using FF3.5 and IE7, that last update you made caused the the 'jump to top of the page' problem to happen in IE7 as well, it already happened in FF3.5
Ryan
Problem for me is that I don't know what kind of CSS you have setup on your page. Could you try to create a sample page on jsbin.com and put up a link here so that I can see the problem you are facing?
SolutionYogi
sure, ill do that now
Ryan
http://jsbin.com/utowi something like this, hope that makes sense
Ryan
Ryan, I don't see any jumping around in Firefox 3.5. Also, I find that the 'about' div always remain visible going over the footer if need be. Check this: http://jsbin.com/ewizo I am really not sure what else I could do at this moment.
SolutionYogi
thats fine SolutionYogi, thanks for your help. I will just keep on trying to get it going. you dont happen to still have that second edit you did do you? Where you said this: "I understood what you meant by 'top' rule being more important than bottom rule. I updated the code so that the floating menu never goes off the window."
Ryan
Click on http://stackoverflow.com/revisions/1135297/list to see the history of this post and see what changes I have made.
SolutionYogi