js 点击锚点滑动定位

HTML部分: 

<a onclick="changeGo('#content')">从这里出发 >></a>
<div id="content" class=" ">我家在这里</div>
 
 

JS部分:

  方法一://滑动到指定位置

  function changeGo(id){
             var ele = document.querySelector(idName);
             ele.scrollIntoView({
                 behavior: "smooth",
                 block: "start",
            });
        }
 
  方法二://滑动到距离指定位置的50px
    function changeGo(id){
            var tops = $(id).offset().top - 50;
            $('html,body').animate({
                scrollTop: tops
            }, 500);
        }
posted @ 2021-03-11 20:17  芳香四溢713051  阅读(127)  评论(0编辑  收藏  举报