简体   繁体   English

是否可以仅禁用锚滚动

[英]Is it possible to disable only the anchor scroll

I actually have a list of item, that open each a diffrent menu, thanks to an href (the #mat for example display a menu) 我实际上有一个项目列表,借助href(例如,#mat显示菜单),可以打开每个不同的菜单

  <div class ="row  d-none d-lg-block classer no-gutters ">
                <h4>Order by
<a class = "navigation" href="#mat" id="navMat"  >localisation</a> /  
<a class = "navigation" href="#new" id="navNew"  >new</a>

and this jquery, to underline the current active item (ex : localisation) 和这个jQuery,以突出显示当前活动项(例如:本地化)

    jQuery( document ).ready(function() {
        jQuery('.navigation').on('click', function(e){
            jQuery('.navigation').removeClass('navigationU');
            this.className = 'navigationU navigation';
        })
    });

The problem is that i would like this href to underline the current active item, to open the menu, but it scrolls to it and i don't want it. 问题是我希望此href在当前活动项目下划线,以打开菜单,但它会滚动到该菜单,因此我不想要它。

I already tried e.preventDefault(); 我已经尝试过e.preventDefault(); but i disable everything : the scroll, but also the underline and the display of the menu. 但我禁用了所有功能:滚动,以及菜单的下划线和显示。

Do you have any idea ? 你有什么主意吗 ? Thanks 谢谢

Try this: 尝试这个:

$(document).on('click', '[href^="#"]', function(e) {
   location.hash = e.target.hash;
   e.preventDefault();
});

it will disable all links that start with # and recreated updating the hash in address bar. 它将禁用所有以#开头并重新创建的更新链接,以更新地址栏中的哈希值。

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

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