javascript - 仅通过 JS 禁用滚动
问题描述
我有两个 div,但是当我在其中一个上隐藏滚动条时,两者都会被窃听,所以我需要在 CSS 中显示溢出。但只需禁用 js 滚动。专门针对 id 为chatcontent的 div 。
解决方案
您可以通过以下 javascript 通过 id 'chatcontent' 指定元素的溢出样式:
// 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 = '';
}
理想情况下,您将定义 CSS 类,通过这些类您可以控制overflow
行为。假设你有以下 CSS,那么你的 javascript 最好写成:
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');
}