Floating menu to stop before it reaches the bottom of the page

I have a floating menu (absolutely positioned) that remains in view when the user scrolls down the page, the problem is that I have a rather large footer, and if you scroll all the way to the bottom of the page, it will collide with the footer .

I just want it to stop talking 400px at 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

+3
source share
2 answers

, " " , # , . , :

var name = "#about";  
var menuYloc = null;  
var footer = '#yourFooterId'; //Specify the ID for your footer.

 $(document).ready(
    function()
    {  
        menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))  
        $(window).scroll(
            function() 
            {   
                var offset = menuYloc + $(document).scrollTop();
                var anotherOffset = offset;

                var docTop = $(window).scrollTop();
                var footerTop = $(footer).offset().top;

                var maxOffset = footerTop - $(name).height() - 20;
                var minOffset = docTop;

                offset = offset > maxOffset ? maxOffset : offset;
                offset = offset < minOffset ? minOffset : offset;

                $(name).animate({top:offset + 'px'},{duration:500,queue:false});      
            }
        );  
    }
);

1:

​​ . .

2:

-, . [ console.log, , Firefox]

3:

, , .

:

div , div ​​.

cBpHrl.png

+3

- (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});  
    });


});
+1

Source: https://habr.com/ru/post/1712738/


All Articles