我有一个网页在使用带有片段标识符(如#Heading1
)的history.pushState
和jQuery的animate
方法来在文档内导航。
这是我如何导航到文档中的位置:
$('nav a').click(function(e){
e.preventDefault();
var href = $(this).attr('href');
history.pushState(null, null, href);
$('#address').val(location.pathname + href);
$('html, body').animate({
'scrollTop': $(href).offset().top + 'px'
});
使用 Google Chrome 在 iOS 上,地址栏更新如预期,并且滚动动画正常,但是“后退/前进”按钮无法跳转到标识的标签。需要注意的是,在使用“后退/前进”按钮时,地址栏中的URL确实已更改。它只是无法跳转到标识的标签。
我只在使用 Google Chrome 时遇到过这个问题,包括 iPhone 和 iPad。
我在 CodePen 上创建了一个带有子集代码的笔记本,可以演示问题:
它在以下设备上运行良好: - Safari on iPhone / iPad - Google Chrome on Android - Mozilla Firefox on Android - Google Chrome on Windows - Mozilla Firefox on Windows - Internet Explorer on Windows
需要注意的是,我没有iOS设备可以测试,在这方面我有一个可靠的测试人员向我发送任何问题的视频和截屏。
既然动画正常工作,则似乎不是jQuery的问题。
hashchange
事件,但我无法想象为什么这么少的人注意到了这一点。 - Vince