How to fix a header on scroll

后端 未结 12 575
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 11:26

I am creating a header that once scrolled to a certain amount of pixels it fixes and stays in place.

Can I do this using just css and html or do i need jquery too?

12条回答
  •  无人及你
    2020-11-27 12:06

    I have modified the Coop's answer. Please check the example FIDDLE Here's my edits:

    $(window).scroll(function(){
      if ($(window).scrollTop() >= 330) {
        $('.sticky-header').addClass('fixed');
       }
       else {
        $('.sticky-header').removeClass('fixed');
       }
    });
    

提交回复
热议问题