简体   繁体   English

在加载或单击时将新页面滚动到顶部

[英]Scroll new page to top on load or on click

I'm using barba.js to fade new pages in. Code must be placed between the barba wrapper div.我正在使用 barba.js 淡入新页面。代码必须放在 barba 包装器 div 之间。 When I click a link to a new page though, the new page fades in at the same position as the previous page was in. So if the user clicks a link near the footer, the new page is loading in near it's footer, which I don't want.但是,当我单击指向新页面的链接时,新页面会在与前一页相同的位置淡入。因此,如果用户单击页脚附近的链接,新页面会在其页脚附近加载,我不想。 How can I get the new page to load in at the top?如何让新页面加载到顶部? I tried adding a class to the links:我尝试向链接添加一个类:

<a class="foo" href="newpage.html">link</a>

and adding this to the footer (inside the barba wrapper like all the other js):并将其添加到页脚(像所有其他 js 一样在 barba 包装器内):

<script>
    $(".foo").click( function() {
        $(window).scrollTop(0);
    });
</script>

but it doesn't work.但它不起作用。

Found the answer.找到了答案。 On the barba js you have to add:在 barba js 上你必须添加:

$(window).scrollTop(0);
if ('scrollRestoration' in history) {
    history.scrollRestoration = 'manual';
}

to the top of the到顶部

fadeIn: function () {

For me there was only one solution that works:对我来说,只有一种解决方案有效:

barba.hooks.enter(() => {
document.body.scrollTop = 0;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM