jquery 监听所有锚点链接实现平滑移动,地址栏上不会改变链接地址

    //监听所有锚点链接实现平滑移动
    $('a[href*=#],area[href*=#]').click(function() {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                //为提升用户体验,做出算法改进 start
                var clientHeight    =    document.body.clientHeight;    //浏览器可视高度
                targetOffset    =    targetOffset-(clientHeight/2)+100;
                //end
                $('html,body').stop(true).animate({
                    scrollTop: targetOffset
                },
                1000);
            return false;
            }
        }
    });

 

相关文章:

  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-27
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2021-09-14
  • 2021-12-03
  • 2021-12-14
相关资源
相似解决方案