javascript - How to start my scroll progress bar from 10% instead of 0?
问题描述
I've managed to implement a scrolling progress bar that works fine (copied the one from w3schools). My issue is that before the user starts scrolling, I want the progress bar to not start from 0 but from 10% (width). It looks fine initially, but after you start scrolling, it resets to 0, and when you scroll back up it scrolls back to a value of 0.
Here's a fiddle to show what I mean: https://jsfiddle.net/z3s4hqvj/
Also, here's my code:
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) * 96;
document.getElementById("myBar").style.width = scrolled + "%";
};
解决方案
Try this little tweak.
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) * 96;
if(scrolled>10){
document.getElementById("myBar").style.width = scrolled + "%";
}
};
推荐阅读
- javascript - 使用 useState 反应 js 活动菜单项
- java - 我的控制器中的排序应该是什么样的?
- javascript - javascript中的fetch函数循环两次
- linkedin - LinkedIn 自动填充按钮未显示
- python - 如何平滑数据
- android - 具有分页 3 库的多视图类型的回收器视图
- html - 在vuejs中更改页面时如何触发功能
- javascript - Nodejs Web 服务器在不直接使用时处于空闲状态
- python - 在 Django 设置 TypeError: 'module' object is not callable
- r - 如何根据 r 中的最低工资回归失业