CSS/HTML : how to make something become absolute positioned once you scroll by it

前端 未结 2 470
轻奢々
轻奢々 2020-12-24 08:41

I\'m new to CSS and HTML here and I\'m trying to learn how to make something become absolutely positioned once you scroll by it on the page.

Here\'s an example of wh

相关标签:
2条回答
  • 2020-12-24 09:06

    See this jsFiddle: http://jsfiddle.net/jkdbP/2/

    var menuTop = $('.menu').offset().top;
    var menuClone = $('.menu').clone().addClass('fixed');
    
    $(window).bind('scroll', function() {
        var scrollY = window.pageYOffset;
    
        if(scrollY > menuTop) {
            if(menuClone.parent().length === 0) {
                menuClone.appendTo($('.menu').parent());
            }
        } else if(menuClone.parent().length > 0) {
            menuClone.remove();
        }
    });
    

    You can use jQuery's .offset().top to get the Y-position of your menu, and window.pageYOffset (or document.body.scrollTop for old IE compatibility) to get the page's scroll offset. You can then handle the window's scroll event.

    0 讨论(0)
  • 2020-12-24 09:12

    Read about CSS position attribute and jQuery .scroll() method

    0 讨论(0)
提交回复
热议问题