Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅通过JS禁用滚动_Javascript_Scroll_Scrollbar_Overflow - Fatal编程技术网

Javascript 仅通过JS禁用滚动

Javascript 仅通过JS禁用滚动,javascript,scroll,scrollbar,overflow,Javascript,Scroll,Scrollbar,Overflow,我有两个div,但当我在其中一个上隐藏滚动条时,这两个div上都会被窃听,所以我需要在CSS中显示溢出。但只需禁用js滚动。特别适用于id为chatcontent的div。您可以通过以下javascript通过id“chatcontent”指定元素的溢出样式: // Disables the overflow behaviour for chatcontent function disableOverflowForChatcontent() { document.getElementBy

我有两个div,但当我在其中一个上隐藏滚动条时,这两个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类,通过这些类可以控制溢出行为。假设您有以下CSS,那么您的javascript最好写为:

CSS:

.overflow-none {
   overflow:hidden;
}
// 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');
}
JS:

.overflow-none {
   overflow:hidden;
}
// 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');
}

请包含您的代码。与其设置/创建内联样式,不如使用
.classList.add()
.classList.remove()
@ScottMarcus动态添加/删除预先存在的CSS类。是的,我同意,首选使用预先存在的类。希望答案在OP工作的可能限制范围内起作用。虽然我刚刚更新了答案以获取您的建议-感谢您的反馈:-),但我希望有溢出:滚动;仅由js@WebCoder刚刚更新了使用
隐藏的答案
-这对你有用吗?@DacreDenny当我隐藏溢出时,它被窃听了。