使用 Animate Plus 在按钮单击时动画整页滚动
Animating full page scrolling on button click using Animate Plus
我想通过单击 Previous Page
和 Next Page
按钮,使用 Animate Plus
.[=17= 使页面部分的水平滚动按视口的整个宽度平滑地设置动画。 ]
相关代码如下:
import animate from "https://cdn.jsdelivr.net/npm/animateplus@2/animateplus.js"
const previousPage = document.querySelector("button:nth-of-type(1)")
const nextPage = document.querySelector("button:nth-of-type(2)")
previousPage.addEventListener("click", () => {
window.scrollBy(-window.innerWidth, 0)
animate({
easing: "out-quintic"
})
})
nextPage.addEventListener("click", () => {
window.scrollBy(window.innerWidth, 0)
animate({
easing: "out-quintic"
})
})
My full code can be found here:
The animation effect I would like to achieve can be found here:
我错过了什么?
想法是使用更改回调并计算增量以滚动 window。这个增量等于进度乘以我们要滚动的距离。
但是,我假设您希望能够仅使用上一个和下一个按钮来浏览多个部分。由于 用户还可以手动滚动 到不同的部分,因此您需要一种方法来检测当前正在查看哪个部分并以编程方式转到 previous/next 部分。
以下代码通过维护按左坐标排序的部分列表来实现此目的。
对于这个例子,我认为当前部分是跨越屏幕中心线的部分。
import animate from "https://cdn.jsdelivr.net/npm/animateplus@2/animateplus.js"
const previousPage = document.querySelector("button:nth-of-type(1)")
const nextPage = document.querySelector("button:nth-of-type(2)")
const root = document.scrollingElement;
const sections = Array.from(document.querySelectorAll("section")).sort((s1, s2) => {
return s1.getBoundingClientRect().left - s2.getBoundingClientRect().left;
});
// get the section that spans the centerline
const getSectionInView = () => {
const halfWdidth = window.innerWidth / 2;
const index = sections.findIndex(s =>
s.getBoundingClientRect().left <= halfWdidth &&
s.getBoundingClientRect().right > halfWdidth
);
return index;
};
// find the next or previous section in the list
const getNextSection = (dir) => {
const sectionInViewIndex = getSectionInView();
const nextIndex = sectionInViewIndex + dir;
const numSections = sections.length;
const nextSectionIndex = nextIndex < 0 || nextIndex >= numSections ? sectionInViewIndex : nextIndex;
return sections[nextSectionIndex];
};
// animation function
const animateScroll = (dir) => {
const from = root.scrollLeft;
const { left } = getNextSection(dir).getBoundingClientRect();
return progress => root.scrollLeft = from + progress * left
};
previousPage.addEventListener("click", () => {
animate({
easing: "out-quintic",
change: animateScroll(-1)
});
});
nextPage.addEventListener("click", () => {
animate({
easing: "out-quintic",
change: animateScroll(1)
});
});
Here is a CodePen
要使其正常工作,您必须从 section
样式中删除 scroll-snap-align: center;
或将其设置为 none
,因为它与动画冲突。
我想通过单击 Previous Page
和 Next Page
按钮,使用 Animate Plus
.[=17= 使页面部分的水平滚动按视口的整个宽度平滑地设置动画。 ]
相关代码如下:
import animate from "https://cdn.jsdelivr.net/npm/animateplus@2/animateplus.js"
const previousPage = document.querySelector("button:nth-of-type(1)")
const nextPage = document.querySelector("button:nth-of-type(2)")
previousPage.addEventListener("click", () => {
window.scrollBy(-window.innerWidth, 0)
animate({
easing: "out-quintic"
})
})
nextPage.addEventListener("click", () => {
window.scrollBy(window.innerWidth, 0)
animate({
easing: "out-quintic"
})
})
My full code can be found here:
The animation effect I would like to achieve can be found here:
我错过了什么?
想法是使用更改回调并计算增量以滚动 window。这个增量等于进度乘以我们要滚动的距离。
但是,我假设您希望能够仅使用上一个和下一个按钮来浏览多个部分。由于 用户还可以手动滚动 到不同的部分,因此您需要一种方法来检测当前正在查看哪个部分并以编程方式转到 previous/next 部分。
以下代码通过维护按左坐标排序的部分列表来实现此目的。 对于这个例子,我认为当前部分是跨越屏幕中心线的部分。
import animate from "https://cdn.jsdelivr.net/npm/animateplus@2/animateplus.js"
const previousPage = document.querySelector("button:nth-of-type(1)")
const nextPage = document.querySelector("button:nth-of-type(2)")
const root = document.scrollingElement;
const sections = Array.from(document.querySelectorAll("section")).sort((s1, s2) => {
return s1.getBoundingClientRect().left - s2.getBoundingClientRect().left;
});
// get the section that spans the centerline
const getSectionInView = () => {
const halfWdidth = window.innerWidth / 2;
const index = sections.findIndex(s =>
s.getBoundingClientRect().left <= halfWdidth &&
s.getBoundingClientRect().right > halfWdidth
);
return index;
};
// find the next or previous section in the list
const getNextSection = (dir) => {
const sectionInViewIndex = getSectionInView();
const nextIndex = sectionInViewIndex + dir;
const numSections = sections.length;
const nextSectionIndex = nextIndex < 0 || nextIndex >= numSections ? sectionInViewIndex : nextIndex;
return sections[nextSectionIndex];
};
// animation function
const animateScroll = (dir) => {
const from = root.scrollLeft;
const { left } = getNextSection(dir).getBoundingClientRect();
return progress => root.scrollLeft = from + progress * left
};
previousPage.addEventListener("click", () => {
animate({
easing: "out-quintic",
change: animateScroll(-1)
});
});
nextPage.addEventListener("click", () => {
animate({
easing: "out-quintic",
change: animateScroll(1)
});
});
Here is a CodePen
要使其正常工作,您必须从 section
样式中删除 scroll-snap-align: center;
或将其设置为 none
,因为它与动画冲突。