如何在点击时将新页面滚动到顶部?
How to scroll new page to top on load on click?
我正在使用 barba.js 淡入新页面。代码必须放在 barba 包装器 div 之间。但是,当我单击 link 进入新页面时,新页面会在与前一页相同的位置淡入。因此,如果用户单击 link,新页面将在附近加载,我不想要。
如何让新页面加载到顶部?
我在堆栈上找到了关于 scrollRestoration
函数的答案,但我仍然不明白如何让它工作。
有人可以帮我编译下面的js代码,添加一些css或html来解决这个问题吗?
$(window).scrollTop(0);
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
function delay(n) {
n = n || 2000;
return new Promise((done) => {
setTimeout(() => {
done();
}, n);
});
}
function pageTransition() {
var tl = gsap.timeline();
tl.to(".animate-this", {
duration: .2, opacity: 0, delay: 0, y: 30, //scale:0, delay: 0.2,
});
tl.to(".animate-this", {
duration: 0, opacity: 0, delay: 0, scale:0, //scale:0, delay: 0.2,
});
tl.fromTo(".loading-screen", {width: 0, left: 0}, {
duration: 1,
width: "100%",
left: "0%",
ease: "expo.inOut",
delay: 0.3,
});
tl.to("#svg-1", {
duration: 1, opacity: 0, y: 30, stagger: 0.4, delay: 0.2,
});
tl.to(".loading-screen", {
duration: 1,
width: "100%",
left: "100%",
ease: "expo.inOut",
delay: 0, //CHANGE TIME HERE END TRANS
});
tl.set(".loading-screen", { left: "-100%", });
tl.set("#svg-1", { opacity: 1, y: 0 });
}
function contentAnimation() {
var tl = gsap.timeline();
tl.from(".animate-this", { duration: .5, y: 30, opacity: 0, stagger: 0.4, delay: 0.2 });
}
$(function () {
barba.init({
sync: true,
transitions: [
{
async leave(data) {
const done = this.async();
pageTransition();
await delay(2500);
done();
},
async enter(data) {
contentAnimation();
},
async once(data) {
contentAnimation();
},
},
],
});
});
看看:
https://pasteboard.co/Ja33erZ.gif
这里还有一个代码笔来检查我的问题 (html,css):
https://codepen.io/cat999/project/editor/AEeEdg
scrollRestoration
不是必需的,这可以通过在过渡期间重置滚动位置来完成:
tl.fromTo(".loading-screen", {width: 0, left: 0}, {
duration: 1,
width: "100%",
left: "0%",
ease: "expo.inOut",
delay: 0.3,
onComplete: function() {
window.scrollTo(0, 0);
}
});
这应该是您要查找的内容:
barba.hooks.enter(() => {
window.scrollTo(0, 0);
});
(可以在文档中找到)
我正在使用 barba.js 淡入新页面。代码必须放在 barba 包装器 div 之间。但是,当我单击 link 进入新页面时,新页面会在与前一页相同的位置淡入。因此,如果用户单击 link,新页面将在附近加载,我不想要。
如何让新页面加载到顶部?
我在堆栈上找到了关于 scrollRestoration
函数的答案,但我仍然不明白如何让它工作。
有人可以帮我编译下面的js代码,添加一些css或html来解决这个问题吗?
$(window).scrollTop(0);
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
function delay(n) {
n = n || 2000;
return new Promise((done) => {
setTimeout(() => {
done();
}, n);
});
}
function pageTransition() {
var tl = gsap.timeline();
tl.to(".animate-this", {
duration: .2, opacity: 0, delay: 0, y: 30, //scale:0, delay: 0.2,
});
tl.to(".animate-this", {
duration: 0, opacity: 0, delay: 0, scale:0, //scale:0, delay: 0.2,
});
tl.fromTo(".loading-screen", {width: 0, left: 0}, {
duration: 1,
width: "100%",
left: "0%",
ease: "expo.inOut",
delay: 0.3,
});
tl.to("#svg-1", {
duration: 1, opacity: 0, y: 30, stagger: 0.4, delay: 0.2,
});
tl.to(".loading-screen", {
duration: 1,
width: "100%",
left: "100%",
ease: "expo.inOut",
delay: 0, //CHANGE TIME HERE END TRANS
});
tl.set(".loading-screen", { left: "-100%", });
tl.set("#svg-1", { opacity: 1, y: 0 });
}
function contentAnimation() {
var tl = gsap.timeline();
tl.from(".animate-this", { duration: .5, y: 30, opacity: 0, stagger: 0.4, delay: 0.2 });
}
$(function () {
barba.init({
sync: true,
transitions: [
{
async leave(data) {
const done = this.async();
pageTransition();
await delay(2500);
done();
},
async enter(data) {
contentAnimation();
},
async once(data) {
contentAnimation();
},
},
],
});
});
看看: https://pasteboard.co/Ja33erZ.gif
这里还有一个代码笔来检查我的问题 (html,css): https://codepen.io/cat999/project/editor/AEeEdg
scrollRestoration
不是必需的,这可以通过在过渡期间重置滚动位置来完成:
tl.fromTo(".loading-screen", {width: 0, left: 0}, {
duration: 1,
width: "100%",
left: "0%",
ease: "expo.inOut",
delay: 0.3,
onComplete: function() {
window.scrollTo(0, 0);
}
});
这应该是您要查找的内容:
barba.hooks.enter(() => {
window.scrollTo(0, 0);
});
(可以在文档中找到)