I am using the Bootstrapmade.com’s Impact web template:
https://bootstrapmade.com/demo/Impact/
The problem with this Bootstrap web template is the page is automatically being scrolled to the top every time page is refreshed/reloaded.
Steps to reproduce:
- Download the template
- Open it in a web browser
- Scroll down to a few thousands pixels
- Refresh/reload the page using the browser’s Refresh button or F5 or Ctrl+R.
Expected output:
The page should refresh/reload and scroll to the same Y position as before.
What is happening instead:
The page is always scrolled to top after a refresh/reload.
I have tried removing some unnecessary code from assets/js/main.js, and the current code is as below:
/**
* Template Name: Impact - v1.1.0
* Template URL: https://bootstrapmade.com/impact-bootstrap-business-website-template/
* Author: BootstrapMade.com
* License: https://bootstrapmade.com/license/
*/
document.addEventListener('DOMContentLoaded', () => {
"use strict";
/**
* Sticky Header on Scroll
*/
const selectHeader = document.querySelector('#header');
if (selectHeader) {
let headerOffset = selectHeader.offsetTop;
let nextElement = selectHeader.nextElementSibling;
const headerFixed = () => {
if ((headerOffset - window.scrollY) <= 0) {
selectHeader.classList.add('sticked');
if (nextElement)
nextElement.classList.add('sticked-header-offset');
} else {
selectHeader.classList.remove('sticked');
if (nextElement)
nextElement.classList.remove('sticked-header-offset');
}
}
window.addEventListener('load', headerFixed);
document.addEventListener('scroll', headerFixed);
}
/**
* Navbar links active state on scroll
*/
let navbarlinks = document.querySelectorAll('#navbar a');
function navbarlinksActive() {
navbarlinks.forEach(navbarlink => {
if (!navbarlink.hash)
return;
let section = document.querySelector(navbarlink.hash);
if (!section)
return;
let position = window.scrollY + 200;
if (position >= section.offsetTop && position <= (section.offsetTop + section.offsetHeight)) {
navbarlink.classList.add('active');
} else {
navbarlink.classList.remove('active');
}
})
}
window.addEventListener('load', navbarlinksActive);
document.addEventListener('scroll', navbarlinksActive);
/**
* Mobile nav toggle
*/
const mobileNavShow = document.querySelector('.mobile-nav-show');
const mobileNavHide = document.querySelector('.mobile-nav-hide');
document.querySelectorAll('.mobile-nav-toggle').forEach(el => {
el.addEventListener('click', function(event) {
event.preventDefault();
mobileNavToogle();
})
});
function mobileNavToogle() {
document.querySelector('body').classList.toggle('mobile-nav-active');
mobileNavShow.classList.toggle('d-none');
mobileNavHide.classList.toggle('d-none');
}
/**
* Hide mobile nav on same-page/hash links
*/
document.querySelectorAll('#navbar a').forEach(navbarlink => {
if (!navbarlink.hash)
return;
let section = document.querySelector(navbarlink.hash);
if (!section)
return;
navbarlink.addEventListener('click', () => {
if (document.querySelector('.mobile-nav-active')) {
mobileNavToogle();
}
});
});
/**
* Toggle mobile nav dropdowns
*/
const navDropdowns = document.querySelectorAll('.navbar .dropdown > a');
navDropdowns.forEach(el => {
el.addEventListener('click', function(event) {
if (document.querySelector('.mobile-nav-active')) {
event.preventDefault();
this.classList.toggle('active');
this.nextElementSibling.classList.toggle('dropdown-active');
let dropDownIndicator = this.querySelector('.dropdown-indicator');
dropDownIndicator.classList.toggle('bi-chevron-up');
dropDownIndicator.classList.toggle('bi-chevron-down');
}
})
});
/**
* Scroll top button
*/
const scrollTop = document.querySelector('.scroll-top');
if (scrollTop) {
const toggleScrollTop = function() {
window.scrollY > 100 ? scrollTop.classList.add('active') : scrollTop.classList.remove('active');
}
window.addEventListener('load', toggleScrollTop);
document.addEventListener('scroll', toggleScrollTop);
scrollTop.addEventListener('click', window.scrollTo({
top: 0,
behavior: 'smooth'
}));
}
});
There doesn’t seem any code in main.js which is doing this. Can anybody tell me how to stop scroll-to-top on page refresh/reload?