是否有窗口元素可用于确定您何时在网页的底部?

发布于 2025-01-22 10:29:15 字数 1857 浏览 3 评论 0原文

我正在尝试创建一个进度栏,以显示某人在网页上有多远,我还希望用户在单击进度栏后能够跳到下一节。当前有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文