Make named anchor bookmarks appear always at top of the screen when clicked

后端 未结 12 650
耶瑟儿~
耶瑟儿~ 2021-01-03 04:21

I have my markup as follows:

  • Sheds & Housing
  • <
    12条回答
    •  佛祖请我去吃肉
      2021-01-03 05:20

      Add the id "last_anchor" to the last link in your list and this will achieve it with jquery. How and if you decide to change the margin of the body back is up to you...

      $(document).ready(function(){
        $("#last_anchor").click(function(){
          var content_id = $(this).attr("href");
          var win_height = $(window).height();
          var content_height = $(content_id).height();
          var target_margin = win_height - content_height;
          $("body").css("margin-bottom", target_margin)
        });
      });​
      

    提交回复
    热议问题