javascript progress bar code example

Example 1: css progress bar

<!-- Basic Bootstrap Progress Bar -->
<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="70"
  aria-valuemin="0" aria-valuemax="100" style="width:70%">
    <span class="sr-only">70% Complete</span>
  </div>
</div>

<!-- W3.CSS-->
<div class="w3-border">
  <div class="w3-grey" style="height:24px;width:20%"></div>
</div>

<!-- Foundation 5.2 (Yep) -->
<div class="progress [small-# large-#] [radius round]">
  <span class="meter [secondary alert success]" style="width: [1 - 100]%"></span>
</div>

<!-- Foundation 6.2 (Yep Yep)-->
<div class="progress" role="progressbar" tabindex="0" aria-valuenow="50" aria-valuemin="0" aria-valuetext="50 percent" aria-valuemax="100">
  <div class="progress-meter" style="width: 50%"></div>
</div>

Example 2: js code for webpage download progress bar

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="a1" data-filename="filename.xml">Click to download</div>
<script>
$('#a1').click(function() {
    var that = this;
    var page_url = 'download.php';

    var req = new XMLHttpRequest();
    req.open("POST", page_url, true);
    req.addEventListener("progress", function (evt) {
        if(evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            console.log(percentComplete);
        }
    }, false);

    req.responseType = "blob";
    req.onreadystatechange = function () {
        if (req.readyState === 4 && req.status === 200) {
            var filename = $(that).data('filename');
            if (typeof window.chrome !== 'undefined') {
                // Chrome version
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(req.response);
                link.download = filename;
                link.click();
            } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
                // IE version
                var blob = new Blob([req.response], { type: 'application/force-download' });
                window.navigator.msSaveBlob(blob, filename);
            } else {
                // Firefox version
                var file = new File([req.response], filename, { type: 'application/force-download' });
                window.open(URL.createObjectURL(file));
            }
        }
    };
    req.send();
});
</script>

Example 3: How to Build a Reading Progress Bar With CSS and JavaScript

// Reading Progress bar in Html, Css and Js
// Html Part
<div id="progress-bar"></div>

// Css Part
#progress-bar {
  	--scrollAmount: 0%;
	background-image: linear-gradient(to right, #4facfe 0%, #00f2fe 100%);
	width: var(--scrollAmount);
	height: 4px;
	position: fixed;
	top: 0;
	left: 0;
}
    
    
// JavaScript Part
let processScroll = () => {
    let docElem = document.documentElement,
    docBody = document.body,
    scrollTop = docElem['scrollTop'] || docBody['scrollTop'],  //browser support, docElem or docBody  // the heigth we are on currently starting from top 
    scrollBottom = (docElem['scrollHeight'] || docBody['scrollHeight']) - window.innerHeight, // the heigth of the entire content
    scrollPercent = scrollTop / scrollBottom *100 + '%';

    console.log(parseInt(scrollPercent) + '%'+ ' = ' + scrollTop + ' / ' + scrollBottom)

    document.getElementById('progress-bar').style.setProperty('--scrollAmount', scrollPercent)
}

document.addEventListener('scroll', processScroll)