简体   繁体   English

带有锚链接javascript或jquery的菜单

[英]Menu with anchor link javascript or jquery

I found the jquery code with the anchor in the link. 我在链接中找到了带有锚点的jquery代码。

jQuery (document) .ready (function () {
    jQuery ("a"). on ('click', function (event) {
        if (this.hash !== "") {
            event.preventDefault ();
            var hash = this.hash;
            jQuery ('html, body'). animate ({scrollTop: jQuery(hash).offset ().top}, 800, function () {
              window.location.hash = hash;
            });
        }
    });
});

My menu works on anchors as one page, but I have another link there which directs to an additional subpage from which after clicking the link with an anchor I had to use a slash / to return to a single page with anchor links. 我的菜单在锚点上作为一个页面工作,但是在那里有另一个链接,该链接指向另一个子页面,在单击带有锚点的链接后,我必须使用斜杠/返回带有锚点链接的单个页面。 I would like that in the address bar of the page you can not see the anchor link only the main page address itself, but that the link would be directed to the appropriate part on the page with the anchor. 我希望在页面的地址栏中看不到锚链接,而只能看到主页地址本身,但是该链接将被定向到带有锚的页面上的适当部分。

My menu: 我的菜单:

<ul id = "categories" class="nav navbar-nav">
 <li><a href="/#anchor1">Anchor 1</a></li>
 <li><a href="/#anchor2">Anchor 2</a></li>
 <li><a href="/#anchor3">Anchor 3</a></li>
 <li><a href="new_page.html">New page</a></li>
</ul>

Address bar in the browser: 浏览器中的地址栏:

http://example.com/#anchor1

I would like to have the link moved to the anchor site after clicking on the anchor menu but hid the name of the anchor in the address: 我想在单击锚菜单后将链接移到锚站点,但将锚的名称隐藏在地址中:

http://example.com/

I am asking for help how to do it? 我正在寻求帮助怎么办?

1.You need to remove this line:- 1.您需要删除此行:-

window.location.hash = hash;

From your code as it add the hash part to the URL. 从您的代码中,将哈希部分添加到URL。

Code need to be:- 代码必须是:

<script>
jQuery (document) .ready (function () {
  jQuery ("a"). on ('click', function (event) {
    if (this.hash !== "") {
      event.preventDefault ();
      var hash = this.hash;
      jQuery('html, body').animate({scrollTop: jQuery(hash).offset ().top}, 800);
    }
  });
});
</script>

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

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