我正在使用插件fullpage.js
example.com/#sectionname)
我想要的是干净的URL,而不是像这样的部分的哈希URL example.com/sectionname
。
该插件不提供此选项,但是我很好奇是否有足够简单的方法可以实现此目的。
更新1:我已经尝试过此回调:
onLeave: function(index, nextIndex, direction) {
if (nextIndex == 2) {
history.replaceState(null, null, "/about")
}
}
以及这个:
afterLoad: function(anchorLink, index) {
if (index == 2) {
history.replaceState(null, null, "/about");
}
}
但是,当我滚动到第二部分时,URL如下所示:www.example.com/about/#about
。
我也尝试过以下代码:
function locationHashChanged() {
if (location.hash === "#about") {
history.replaceState(null, null, "/about");
}
}
window.onhashchange = locationHashChanged;
但是,当加载新的部分时,哈希会在更改为非哈希URL之前短暂显示。
您始终可以使用HTML 5历史记录API。您可以使用诸如onLeave
或的回调afterLoad
:
$('#fullpage').fullpage({
afterLoad: function(anchorLink, index) {
history.pushState(null, null, "bar.html");
}
});
考虑到您需要为旧版浏览器使用一些polyfill。或者直接使用history.js。
请注意,上面的示例在某些浏览器上可能不适用于localhost。您最好在服务器上尝试一下。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句