As I have a div sliding menu that doesn't move unless the page scrolls past a specific point

I have a div in the menu that I want to move down so that it is always visible, but I want it to be under my div heading. I do not want it to move until the top of the menu hits the top of the screen, and then remains in its place. Basically I want a sliding menu with the maximum height that it can slide on.

+3
source share
2 answers

I think I understand what you're talking about, we used a similar technique on The King with jQuery. Here's how:

///// CONFIGURATION VARIABLES:

var name                = "#rightsidebar";
var menu_top_limit      = 241;
var menu_top_margin     = 20;
var menu_shift_duration = 500;
var menuYloc = null;
///////////////////////////////////

$(window).scroll(function() 
{ 
    // Calculate the top offset, adding a limit
    offset = menuYloc + $(document).scrollTop() + menu_top_margin;

    // Limit the offset to 241 pixels...
    // This keeps the menu out of our header area:
    if(offset < menu_top_limit)
        offset = menu_top_limit;

    // Give it the PX for pixels:
    offset += "px";

    // Animate:
    $(name).animate({top:offset},{duration:menu_shift_duration,queue:false});
});

(a tip for the hat @soyrex who wrote this code.)

+8

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


All Articles