From a218bb6c8206a39c0fc12cbdd8ff3d0bb988b1a9 Mon Sep 17 00:00:00 2001 From: sh20raj Date: Wed, 27 Mar 2024 20:51:50 +0530 Subject: [PATCH] setConfig Working + demo.html in prev commit --- ScrollProgress.js | 29 ++++++++++++++++++++--------- demo.html | 8 ++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ScrollProgress.js b/ScrollProgress.js index e5609ad..ca6a129 100644 --- a/ScrollProgress.js +++ b/ScrollProgress.js @@ -8,6 +8,16 @@ var ScrollProgress = (function() { position: 'top' }; + // Function to update the progress bar + function updateProgressBar(progressBar) { + return function() { + var scroll = document.documentElement.scrollTop || document.body.scrollTop; + var height = document.documentElement.scrollHeight - document.documentElement.clientHeight; + var scrolled = (scroll / height) * 100; + progressBar.style.width = scrolled + '%'; + }; + } + function init(customConfig) { // Merge custom configuration with default config if (customConfig && typeof customConfig === 'object') { @@ -34,25 +44,22 @@ var ScrollProgress = (function() { document.body.appendChild(progressBar); // Function to update the progress bar - function updateProgressBar() { - var scroll = document.documentElement.scrollTop || document.body.scrollTop; - var height = document.documentElement.scrollHeight - document.documentElement.clientHeight; - var scrolled = (scroll / height) * 100; - progressBar.style.width = scrolled + '%'; - } + var onScroll = updateProgressBar(progressBar); // Event listener for scroll - window.addEventListener('scroll', updateProgressBar); + window.addEventListener('scroll', onScroll); // Initial call to set the progress bar - updateProgressBar(); + onScroll(); } // Public method to update configuration function setConfig(newConfig) { + destroy() if (newConfig && typeof newConfig === 'object') { config = Object.assign({}, config, newConfig); } + init() } // Public method to destroy the progress bar @@ -64,6 +71,11 @@ var ScrollProgress = (function() { } } + // Auto-initialize if data-autoload is set to true + if (document.currentScript.dataset.autoload) { + init(); + } + // Expose public methods return { init: init, @@ -71,4 +83,3 @@ var ScrollProgress = (function() { destroy: destroy }; })(); -if(document.currentScript.dataset.autoload) ScrollProgress.init() \ No newline at end of file diff --git a/demo.html b/demo.html index 5d609c1..9a77da8 100644 --- a/demo.html +++ b/demo.html @@ -91,6 +91,14 @@

Lorem Ipsum

height: '4px', // Progress bar height position: 'top' // Progress bar position ('top' or 'bottom') }); + + ScrollProgress.setConfig({ + color: 'red', // Progress bar color + height: '4px', // Progress bar height + position: 'top' // Progress bar position ('top' or 'bottom') + }); + + // ScrollProgress.destroy()