HTML — How can I “stick” my navbar after reaching a specific section on page?

别等时光非礼了梦想. 提交于 2019-12-30 14:44:28

问题


I thought of 2 different methods to approaching this, but I need assistance.

  1. Scroll to section and then stick.
  2. Hide element while scrolling, then unhide element once you have reached point on page.

How can I do this?

I'm using stickyjs currently.

But I don't see a feature for doing what I asked.


回答1:


demo - http://jsfiddle.net/m6q6j8xL/3/

this is custom js

in this demo the header changes to green(fixed) and when you reach to blue div changes back to normal and when u are out from the blue div it changes back to fixed

padding is added to div so that it doesn't effect window scroll when changed to fixed

var stickyNavTop = $('.header').offset().top;

function scrolling() {
    doc = $(document).height()
    hidingtop = $('.hiding').offset().top;
    hidingbottom = $('.hiding').position().top + $('.hiding').outerHeight(true) // finding the outer height
    if ($(window).scrollTop() > hidingtop && $(window).scrollTop() < hidingbottom) {
        $('.header').removeClass('sticky');
        $('.container').css('padding-top', '0');
    }
}

var stickyNav = function () {
    var scrollTop = $(window).scrollTop();

    if (scrollTop > stickyNavTop) {
        $('.header').addClass('sticky');
        $('.container').css('padding-top', '100px');
    } else {
        $('.header').removeClass('sticky');
        $('.container').css('padding-top', '0');
    }
};

stickyNav();

$(window).scroll(function () {
    stickyNav();
    scrolling()
});



回答2:


you can use jquery-waypoints plugin for this.

Demo



来源:https://stackoverflow.com/questions/26500923/html-how-can-i-stick-my-navbar-after-reaching-a-specific-section-on-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!