使用JavaScript的DIV的滚动百分比

发布于 2025-01-31 04:35:47 字数 2183 浏览 3 评论 0原文

const rightSide = document.querySelector("#rightSide");

let scrollPercentage = () => {
  let h = document.documentElement;
  let st = h.scrollTop || document.body.scrollTop;
  let sh = h.scrollHeight || document.body.scrollHeight;
  let percent = st / (sh - h.clientHeight) * 100;
  console.log(percent);
}

rightSide.onscroll = scrollPercentage;
.right-side {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 90%;
  width: 90%;
  background-color: #eee;
  overflow-y: scroll;
}

.top-page {
  width: 100%;
  height: 100%;
  background-color: red;
}

.bot-page {
  width: 100%;
  height: 100%;
  background-color: yellow;
}
<div class="right-side" id="rightSide">
  <div id="top-page" class="top-page"></div>
  <div id="bot-page" class="bot-page"></div>
</div>

我正在制作一个网站,我想获得Div的滚动百分比。我在YouTube上使用了Tyler Pott的视频 https://youtu.be/youtu.be/-fjsedzaers 他做到了到整个页面(使用身体)。这是我的代码:

 const rightSide = document.querySelector("#rightSide")

  let scrollPercentage = () => {
    let h = document.documentElement;
    let st = h.scrollTop || document.body.scrollTop;
    let sh = h.scrollHeight || document.body.scrollHeight;

    let percent = st / (sh - h.clientHeight) * 100;
    console.log(percent);
  }

  rightSide.onscroll = scrollPercentage;

当我运行此操作时,控制台只会输出“ NAN”,这是可以预期的。我不确定要将该代码转移到DIVS上,该怎么办。我尝试使用类似的东西

  const topPage = document.querySelector("#top-page");

,然后在document.body.scrolltop等中添加台词而不是主体。但显然这不起作用。

这就是HTML的外观,顶页具有右侧div的宽度和高度,底面页面具有相同的宽度,但右侧Div的最小高度。

 <div class="right-side" id="rightSide">
  <div id="top-page" class="top-page"></div>
  <div id="bot-page" class="bot-page"></div>
 </div>

const rightSide = document.querySelector("#rightSide");

let scrollPercentage = () => {
  let h = document.documentElement;
  let st = h.scrollTop || document.body.scrollTop;
  let sh = h.scrollHeight || document.body.scrollHeight;
  let percent = st / (sh - h.clientHeight) * 100;
  console.log(percent);
}

rightSide.onscroll = scrollPercentage;
.right-side {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 90%;
  width: 90%;
  background-color: #eee;
  overflow-y: scroll;
}

.top-page {
  width: 100%;
  height: 100%;
  background-color: red;
}

.bot-page {
  width: 100%;
  height: 100%;
  background-color: yellow;
}
<div class="right-side" id="rightSide">
  <div id="top-page" class="top-page"></div>
  <div id="bot-page" class="bot-page"></div>
</div>

I'm making a website and I want to get the scroll percentage of a div. I've used Tyler Pott's video on YouTube https://youtu.be/-FJSedZAers and he does that to the whole page (using the body). And this is my code:

 const rightSide = document.querySelector("#rightSide")

  let scrollPercentage = () => {
    let h = document.documentElement;
    let st = h.scrollTop || document.body.scrollTop;
    let sh = h.scrollHeight || document.body.scrollHeight;

    let percent = st / (sh - h.clientHeight) * 100;
    console.log(percent);
  }

  rightSide.onscroll = scrollPercentage;

When I run this, the console just outputs "NaN" which is to be expected. I am not sure what I should do in order to transfer that code onto the divs. I've tried to use things like

  const topPage = document.querySelector("#top-page");

And then add topPage instead of the body in document.body.scrollTop etc. but that obviously doesn't work.

This is sort of what the HTML looks like, and the top page has a width and height of the rightSide div and the bottom page has the same width but a minimum height of the rightSide div.

 <div class="right-side" id="rightSide">
  <div id="top-page" class="top-page"></div>
  <div id="bot-page" class="bot-page"></div>
 </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2025-02-07 04:35:47

回到这个问题,我知道我必须了解分配给变量的值,以使方程式更简单。

因此,快速搜索纪录片使我找到了这个答案:

“纪录片返回文档的根元素(例如,HTML文档的元素)”

,然后,我意识到,也许这是问题,并且是问题。我将右侧分配给H,这就是所需要的。

让H = rightside;

不确定这是否完全正确,有时在滚动结束时您可以获得100.05或更多,但我不需要精确的数字。希望这会有所帮助。

Coming back to this I knew that I had to understand the values assigned to the variables in order to make the equation simpler.

So, a quick search for documentElement led me to this answer:

"documentElement returns the Element that is the root element of the document (for example, the element for HTML documents)"

Then, I realised that maybe that is the issue and it was. I assigned rightSide to h and that's all that was needed.

let h = rightSide;

Not sure if this is fully correct, sometimes at the end of the scroll you can get 100.05 or more, but I didn't need the precise number. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文