jQuery平滑滚动到锚点?

jQuery平滑滚动到锚点?,第1张

jQuery平滑滚动到锚点?

这是我的方法

    var hashTagActive = "";    $(".scroll").on("click touchstart" , function (event) {        if(hashTagActive != this.hash) { //this will prevent if the user click several times the same link to freeze the scroll. event.preventDefault(); //calculate destination place var dest = 0; if ($(this.hash).offset().top > $(document).height() - $(window).height()) {     dest = $(document).height() - $(window).height(); } else {     dest = $(this.hash).offset().top; } //go to destination $('html,body').animate({     scrollTop: dest }, 2000, 'swing'); hashTagActive = this.hash;        }    });

然后,您只需要像这样创建锚:

<a  href="#destination1">Destination 1</a>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5602150.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存