简体   繁体   English

页面跳转时从URL隐藏哈希

[英]Hide hash from URL when page jump

I have a link on my menu that targets to an anchor on another page. 我的菜单上有一个链接,指向另一个页面上的锚点。

<a href="http://www.mylink.com.br/mylink/#anchor">

And I want to hide the #anchor from URL. 我想隐藏#anchor的URL。


I've tried another solution, look: 我尝试了另一个解决方案,看看:

$('#menu #mylink2').click(function() {
   document.location.href = "www.mysite.com/mysite/";
});

and then, I need to activate a script to scroll to the div after the page loads: 然后,我需要激活一个脚本以在页面加载后滚动到div:

$(document).ready(function(){
    $('html, body').animate({ scrollTop: $("#divtoscroll").offset().top }, 2500);
});

but I don't know how to attach that event to the previous. 但我不知道如何将该事件附加到上一个事件。 The way it is, everytime the page loads, it scrolls to the div. 它的方式是,每次页面加载时,它都会滚动到div。

Any help? 有帮助吗?


I did it! 我做的! I added "contato" on the final of the URL to differentiate it from the other links. 我在URL的最后添加了“contato”,以区别于其他链接。

$('#menu #mylink2').click(function() {
   document.location.href = "www.mysite.com/mysite/contato";
});

And used it to recognize the URL and activate the document ready function. 并用它来识别URL并激活文档就绪功能。

var url = "www.mysite.com/mysite/contato";
if (location.href==url) {
    $(document).ready(function(){
        $('html, body').animate({ scrollTop: $("#allcontentcontact").offset().top }, 2500);
    });
}
else {
}

Works perfectly! 完美的工作! Thanks for the help. 谢谢您的帮助。

As far as I am aware it is not possible to do this, as it is what tells the browser which section of the page to jump to. 据我所知,这是不可能的,因为它是告诉浏览器跳转到页面的哪个部分。

You may be able to do this using the amount of pixels and javascript, but it would not work for all cases. 您可以使用像素数量和javascript来执行此操作,但它不适用于所有情况。 Example: 例:

 window.scrollBy(0, 500);

You could use jQuery to scroll to a specific element ID on a page, but this would require a trigger on that page, such as a click, or even page load. 您可以使用jQuery滚动到页面上的特定元素ID,但这需要在该页面上触发,例如单击,甚至页面加载。 Example below uses click. 以下示例使用click。

$("#button").click(function() {
$('html, body').animate({
    scrollTop: $("#elementtoScrollToID").offset().top
}, 2500);

}); });

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

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