[英]Scroll Indicator
此代码使该滚动指示器。
您能帮我看看scrolled
的变量是如何在这里计算的吗?
请解释为什么从scrollHeight
减去clientHeight
,然后将winScroll
变量除以height
再乘以100
?
// When the user scrolls the page, execute myFunction window.onscroll = function() { myFunction() }; function myFunction() { var winScroll = document.body.scrollTop || document.documentElement.scrollTop; var height = document.documentElement.scrollHeight - document.documentElement.clientHeight; var scrolled = (winScroll / height) * 100; document.getElementById("myBar").style.width = scrolled + "%"; }
<div class="header"> <h2>Scroll Indicator</h2> <div class="progress-container"> <div class="progress-bar" id="myBar"></div> </div> </div> <div class="content"> <h3>Scroll Down to See The Effect</h3> <p>100 text line</p> </div>
clientHeight
返回封闭的div的高度。 clientHeight参考
scrollHeight
只读属性是元素内容高度的度量,其中包括由于溢出而在屏幕上不可见的内容。 scrollHeight参考
元素的scrollTop
值是从元素顶部到其最顶部可见内容的距离的度量。 scrollTop参考
减去clientHeight
形式的scrollHeight
可提供可以获取的最大scrollTop
值。 当滚动到达页面底部时, scrollTop
值将变为height
。
用height
除以100的乘积来获得滚动百分比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.