cookieconsent: Problem scrollbar in Chrome

I noticed that in chrome it happens that when loading the page, sometimes the vertical scrollbar does not appear. If I delete the cookie plugin from code, the scrollbar is always displayed. Don’t know where the problem might be?

I think it’s probably caused by the div#cc--main sticking to the bottom of the page.

Thank You

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

A much simpler solution would be to just use css. Don’t know why I didn’t think of this:

/* Hide cc-main if none of the modals are visible */
html:not(.show--consent):not(.show--settings) #cc--main {
    display: none!important;
}

This should achieve the same end result as the JavaScript code. Short, simple & light.

It works, thank you very much!

Adding display: none will also remove the fade-in/fade-out transitions so this is not a viable solution for me.

You will have to either modify yourself the source javascript file or use the below suggested solution.

Place the fixScrollbar function before (or after) cc.run(...) and then call it inside the onAccept method. This code will detect if modals are visible; if they aren’t => add display: none to the element with id="cc--main";

function fixScrollbar(){
    const html = document.documentElement;
    const ccMain = document.getElementById('cc--main');

    /**
     * Returns true if at least one modal is visible
     * @returns {boolean}
     */
    function modalsAreVisible(){
        return (
            html.className.indexOf('show--settings') > -1 ||
            html.className.indexOf('show--consent') > -1
        )
    }

    /**
     * If modals are not visible add 'display: none'
     */
    if(!modalsAreVisible())
        ccMain.style.display = 'none';
    
    const observer = new MutationObserver(function() {
        if(modalsAreVisible()){
            // Add 'display: block' if at least one modal is visible
            ccMain.style.display = 'block';
        }else{
            // Add 'display: none' if modals were just closed
            ccMain.style.display = 'none';
        }
    });

    /**
     * Observe 'html' element's classes
     */
    observer.observe(html, { 
        attributes: true, 
        attributeFilter: ['class']
    });
}
const cc = initCookieConsent();

cc.run({
    // ...
    onAccept: function(){
        fixScrollbar();
    }
});

Since I can’t see the issue in your website (using chrome) and I don’t know how to reproduce it, it’s tricky to determine exactly what is causing this. Leaving this open until a viable solution is proposed.