是否有窗口元素可用于确定您何时在网页的底部?
我正在尝试创建一个进度栏,以显示某人在网页上有多远,我还希望用户在单击进度栏后能够跳到下一节。当前有2个问题:
1。)Progress Bar只有在您已经滚动的情况下才会将您移至下一节。我希望它能够向前转到每个部分,无论用户是否滚动。
2.)当您进入最后一部分时,按钮不会将您移回网页顶部的第一部分。
[JavaScript]
const progressBarFN = () => {
//ProgressBarFN Variables
const pageViewportHeight = window.innerHeight;
const pageHeight = document.documentElement.scrollHeight;
const scrolledPortion = window.pageYOffset;
const scrolledPortionDegree = (scrolledPortion / (pageHeight - pageViewportHeight)) * 360;
const scrollBool = scrolledPortion + pageViewportHeight === pageHeight;
//End of Variables
//Progress Bar Tracker Conditional
halfCircle.forEach(el => {
el.style.transform = `rotate(${scrolledPortionDegree}deg)`;
if(scrolledPortionDegree >= 180) {
halfCircle[0].style.transform = 'rotate(180deg)';
halfCircleTop.style.opacity = "0";
} else {
halfCircleTop.style.opacity = "1";
}
});
//End of Progress Bar Tracker Conditional
//Progress Bar Click
progressBar.onclick = e => {
e.preventDefault();
//Progress Bar OnClick Variables
const sectionPositions = Array.from(sections).map(section => scrolledPortion + section.getBoundingClientRect().top);
const position = sectionPositions.find(sectionPosition => {
return sectionPosition > scrolledPortion;
});
//End of Progress Bar OnClick Variables
//OnClick conditional
if (scrollBool) {
window.scrollTo(0,0);
} else {
window.scrollTo(0,position);
};
//End of OnClick Conditional
};
}
progressBarFN();
- Sidenote:我在声明后并在滚动EventObjectListEner中声明了此功能。
我相信,对于progressbarfn的初始变量,这个问题需要解决。尤其是我的滚动变量。
I am trying to create a progress bar that shows how far someone is on the webpage, I also wanted the user to be able to jump ahead to the next section after clicking on the progress bar. There are currently 2 problems:
1.) The progress bar will only move you to the next section if you have already scrolled. I would like it to move ahead to each section regardless if the user has scrolled.
2.) When you get to the final section the button won't move you back to the first section at the top of the webpage.
[javascript]
const progressBarFN = () => {
//ProgressBarFN Variables
const pageViewportHeight = window.innerHeight;
const pageHeight = document.documentElement.scrollHeight;
const scrolledPortion = window.pageYOffset;
const scrolledPortionDegree = (scrolledPortion / (pageHeight - pageViewportHeight)) * 360;
const scrollBool = scrolledPortion + pageViewportHeight === pageHeight;
//End of Variables
//Progress Bar Tracker Conditional
halfCircle.forEach(el => {
el.style.transform = `rotate(${scrolledPortionDegree}deg)`;
if(scrolledPortionDegree >= 180) {
halfCircle[0].style.transform = 'rotate(180deg)';
halfCircleTop.style.opacity = "0";
} else {
halfCircleTop.style.opacity = "1";
}
});
//End of Progress Bar Tracker Conditional
//Progress Bar Click
progressBar.onclick = e => {
e.preventDefault();
//Progress Bar OnClick Variables
const sectionPositions = Array.from(sections).map(section => scrolledPortion + section.getBoundingClientRect().top);
const position = sectionPositions.find(sectionPosition => {
return sectionPosition > scrolledPortion;
});
//End of Progress Bar OnClick Variables
//OnClick conditional
if (scrollBool) {
window.scrollTo(0,0);
} else {
window.scrollTo(0,position);
};
//End of OnClick Conditional
};
}
progressBarFN();
- sidenote: I have declared this function after its declaration and within a scroll eventObjectlistener.
I believe the problem has something to due with the progressBarFN's initial variables. Especially with my scrollBool variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论