0

window.addEventListener('resize', function() {
    document.addEventListener('scroll', function() {
        checkScrollHeight();
    });
});

    function checkScrollHeight() {
        let stickyNavbar = document.getElementById('sticky-navbar');
        let currentPosition = window.pageYOffset || document.documentElement.scrollTop;
        let bannerHeight = document.getElementById('banner-height').offsetHeight || document.getElementById('banner-height').clientHeight;

        if(currentPosition > bannerHeight) {
            stickyNavbar.style.display = "block";
            return;
        } else {
            stickyNavbar.style.display = "none";
        }
    }

Currently the function is working when the user resizes the page, but how can I write the code so the function will be called after reloading the page? The navbar should be displayed when it passes the specific height of the banner which will dynamically change based on the screen width.

2 Answers2

0

Just call it. All your JS is executed afresh when the page reloads.

window.addEventListener('resize', checkScrollHeight );

checkScrollHeight();
Ben West
  • 4,398
  • 1
  • 16
  • 16
0

Use cookies, if you are using only js here. Like personal lands on page store cookie. After reloading check the cookie. https://www.w3schools.com/js/js_cookies.asp Use this functions for set and get cookies

Picks Prabhakar
  • 335
  • 1
  • 3
  • 13