How to build simple sticky navigation with jQuery?

前端 未结 1 1909
长情又很酷
长情又很酷 2020-12-19 17:15

I\'m trying to change the css of a div when scrolling. This is my code but unfortunately it won\'t work.

$(document).ready(function() {
   $(window).scroll(f         


        
相关标签:
1条回答
  • 2020-12-19 17:48

    Try this:

    Here is working jsFiddle

    $(document).ready(function() {
       $(window).scroll(function() {
           var scrollVal = $(this).scrollTop();
            if ( scrollVal > 150) {
                $('#subnav').css({'position':'fixed','top' :'0px'});
            } else {
                $('#subnav').css({'position':'static','top':'auto'});
            }
        });
     });
    

    Note: If you have just one value you can use else, but if you have multiple values i suggest to not use else, because it creates conflict, use else if intead.

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