简体   繁体   中英

Disable scroll only by JS

I have two divs but when I hide scrollbar on one it will be bugged on both, so I need to have overflow visible in CSS. But just disable scrolling with js. Specifically for div with id chatcontent .

You can specify the overflow styling for an element by id 'chatcontent', via the following javascript:

// Disables the overflow behaviour for chatcontent
function disableOverflowForChatcontent() {
    document.getElementById('chatcontent').style.overflow = 'hidden';
}

// Resets the overflow behaviour for chatcontent
function resetOverflowForChatcontent() {
    document.getElementById('chatcontent').style.overflow = '';
}

Ideally, you would have CSS classes defined through which you would control overflow behaviour. Supposing you had the following CSS, then your javascript would be better written as:

CSS:

.overflow-none {
   overflow:hidden;
}

JS:

// Disables the overflow behaviour for chatcontent
function disableOverflowForChatcontent() {
    document.getElementById('chatcontent').classList.add('overflow-none');
}

// Resets the overflow behaviour for chatcontent
function resetOverflowForChatcontent() {
    document.getElementById('chatcontent').classList.remove('overflow-none');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM