[英]Navigating to default route ('') causes page to scroll
I'm using route.navigate()
to trigger routes in my application, with pushState disabled. 我在禁用pushState的情况下使用route.navigate()
触发了应用程序中的路由。 When triggering the default route ( router.navigate('', { trigger: true })
, the page scrolls to the top, as if a regular #
link had been followed. 触发默认路由( router.navigate('', { trigger: true })
,页面滚动到顶部,就像遵循常规的#
链接一样。
You can see this in action here: 您可以在这里看到实际效果:
https://jsfiddle.net/k58tstas/ https://jsfiddle.net/k58tstas/
From reading the Backbone source, it uses window.location.hash
to update the URL, and when that is set to ''
it translates to '#'
and causes a scroll jump to that root anchor. 通过阅读Backbone源,它使用window.location.hash
来更新URL,并将其设置为''
它将转换为'#'
并导致滚动跳转到该根锚点。
A quick, dirty solution is to have a named route for the default and use that, but is there a nicer way to disable this functionality? 一种快速,肮脏的解决方案是为默认路由使用命名路由并使用该路由,但是是否有更好的方法来禁用此功能?
If you start the router with the pushState
option 如果使用pushState
选项启动路由器
Backbone.history.start( { pushState: true } );
the issue will go away (except in some older browsers). 该问题将消失(某些较旧的浏览器除外)。 I realize you have made a conscious decision to leave it off, but unfortunately, from what I've been able to gather, turning it on is the only way to suppress that behaviour. 我意识到您已经做出了一个明智的决定,将其搁置一旁,但是不幸的是,根据我的能力,将其打开是抑制该行为的唯一方法。
Try placing the Backbone.history.start(); 尝试放置Backbone.history.start(); before instantiating the router object. 在实例化路由器对象之前。
$(function() {
Backbone.history.start();
window.router = new Router();
$('.js-root').on('click', function(e) {
e.preventDefault();
window.router.navigate('', { trigger: true });
});
$('.js-queenslanders').on('click', function(e) {
e.preventDefault();
window.router.navigate('queenslanders', { trigger: true });
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.