如何在使用effect.scrollto方法时更改URL名称

时间:2015-06-06 20:18:12

标签: javascript jquery html css

您好我正在使用滚动方法来点击某个地方的特定地点,但网址名称始终相同。我可以使用不同的网址结尾。我的意思是网址始终以#结尾。我可以拥有#products或#about。我正在使用此代码。请帮助我

<span><a href="#" id="start1"onclick="Effect.ScrollTo('about',{duration:1.0}); return false;"class="scroll"style="text-decoration:none;position:absolute;right:145px;top:30px;font-weight:bold;color:white;font-size:15px" onmouseover="big(this)" onmouseout="small(this)">ABOUT US</a></span>

1 个答案:

答案 0 :(得分:0)

当用户点击链接(https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history)时,您可以使用历史记录API将URL更改推送到页面,例如使用jQuery:

$("#start1").click(function(e){
    e.preventDefault();
    Effect.ScrollTo('about',{duration:1.0});
    history.pushState(null, null, '#about');
});

或者您可以考虑使用直接哈希链接,例如把href =&#34;#about&#34;进入锚点并阻止哈希跳转(How can I update window.location.hash without jumping the document?)。

**编辑

继承人jsFiddle:https://jsfiddle.net/3uwkcebk/1/