Scroll indicator JavaScript

独自空忆成欢 提交于 2021-02-10 07:44:16

问题


I use the following code for a scroll indicator which I include on multiple pages.

.progress-container {
        width: 100%;
        height: 8px;
        background: #ccc;
        position: fixed;
        top: 0;
        z-index: 1000;
    }
    
    .progress-bar {
        height: 8px;
        background: #4caf50;
        width: 0%;
    }
    <div class="fixedBar">
        <div class="header">
            <div class="progress-container">
                <div class="progress-bar" id="myBar"></div>
            </div>
        </div>
    </div>
    
    
    <script>
        // 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 + "%";
        }
    </script>

But some of the pages are not able to scroll due to lack of content I am trying to get the scroll indicator to also be filled I can't figure out why it's not filling if it cant scroll ( I am not good with JavaScript either :3 )


回答1:


Two things:

  • Execute your function at least once when the page is loaded
  • Check if document has no scroll and set full width in that case.

Here it is:

// When the user scrolls the page, execute myFunction
window.onscroll = function () {
    myFunction()
};

// Execute myFunction once when the page is loaded
window.onload = function() {
  myFunction();
}

function myFunction() {
    var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
    var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
    var scrolled = 100; // Default value: fill width

    // Only if document is scrollable
    if (height > 0) {
        scrolled = (winScroll / height) * 100;
    }
    document.getElementById("myBar").style.width = scrolled + "%";
}



回答2:


Try this:

.progress-container {
        width: 100%;
        height: 8px;
        background: #ccc;
        position: fixed;
        top: 0;
        z-index: 1000;
    }
    
    .progress-bar {
        height: 8px;
        background: #4caf50;
        width: 0%;
    }
    <div class="fixedBar">
        <div class="header">
            <div class="progress-container">
                <div class="progress-bar" id="myBar"></div>
            </div>
        </div>
    </div>
    
    
    <script>

         window.onload = function () {
             if (document.documentElement.scrollHeight === document.documentElement.clientHeight){
                 document.getElementById("myBar").style.width = "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 + "%";
        }
    </script>



回答3:


You cant't divide by zero try this

function myFunction() {
            var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
            var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
            var scrolled = height !== 0 ? (winScroll / height) * 100 : 0;
            document.getElementById("myBar").style.width = scrolled + "%";
        } 



回答4:


The problem you have is due to the lack of a check of the height of the docuement and the client's window height. Yo can solve it like this:

    let heightClient = window.innerHeight;
    let body = document.body,
        html = document.documentElement;

    let heightDocument = Math.max( body.scrollHeight, body.offsetHeight,
        html.clientHeight, html.scrollHeight, html.offsetHeight );

    if(heightClient < heightDocument){
        window.onscroll = function() {myFunction()};
    }else document.getElementById("myBar").style.width = "100%";

    function myFunction() {
        let winScroll = document.body.scrollTop || document.documentElement.scrollTop;
        let height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
        let scrolled = (winScroll / height) * 100;
        document.getElementById("myBar").style.width = scrolled + "%";
    }
</script>


来源:https://stackoverflow.com/questions/53295911/scroll-indicator-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!