繁体   English   中英

如何获得此代码以仅平滑滚动所需的元素?

[英]How can I get this code to only smooth scroll only the elements I want?

我有一个平滑的滚动代码,效果很好,但问题是它工作得很好。 我还有其他使用“#”标记的元素(例如:但我不希望这些标签以平滑滚动作为目标。我有以下平滑滚动代码:

    jQuery(document).ready(function($)  {
       $('a[href*=#]:not([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) {
       $('html,body').animate({
       scrollTop: target.offset().top
     }, 1000);
     return false;
   }
 }
});
});

无论如何,有没有修改它只针对页面的锚,而不是选项卡锚

如果您不想使用#作为href值影响链接,则应使用如下语法:

$("[attribute!='value']")

因此,在您的情况下,您将href指定为属性,将#指定为值。

$("a[href!='#']").on('click', function(e){
    e.preventDefault();
    //do stuff
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM