当我在index.php上,然后按主页按钮,它向下滚动到元素。但是,当我不在index.php上并按下相同的按钮时,我想更改URL位置,然后滚动到该元素。这是我尝试:jquery更改位置,然后滚动到元素
$('.home').on('click', function (event) {
// If location is index.php do this:
if (location.href.indexOf("/index.php") > -1) {
$('html, body').animate({
scrollTop: $("#anotherelement").offset().top
}, 1000);
// If location is not index.php do this:
} else {
window.location = "index.php";
$('html, body').animate({
scrollTop: $("#anotherelement").offset().top
}, 1000);
}
});
它只是改变了URL,但是当我不在的index.php它不滚动到元素
你需要的时候你改变你加载一个新的页面位置,以便您的滚动代码是无用的,因为使用它(滚动效应)对DOM已准备就绪功能 – slashsharp
你正在从“上一页”的页面 – Saar