Skip to content

Commit

Permalink
setConfig Working + demo.html in prev commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SH20RAJ committed Mar 27, 2024
1 parent db29ac6 commit a218bb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
29 changes: 20 additions & 9 deletions ScrollProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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
Expand All @@ -64,11 +71,15 @@ var ScrollProgress = (function() {
}
}

// Auto-initialize if data-autoload is set to true
if (document.currentScript.dataset.autoload) {
init();
}

// Expose public methods
return {
init: init,
setConfig: setConfig,
destroy: destroy
};
})();
if(document.currentScript.dataset.autoload) ScrollProgress.init()
8 changes: 8 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ <h1>Lorem Ipsum</h1>
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()
</script>

<!-- Include the scrollLoader.js script -->
Expand Down

0 comments on commit a218bb6

Please sign in to comment.