下面的代码有什么问题?
我需要根据另一个元素是否有滚动条来检测和设置 1 个元素的宽度。这似乎不起作用:
console.log("print Scroll..here...");
if(($(".printer-details-container").offsetHeight < $(".printer-details-container").scrollHeight) || ($(".printer-details-container").offsetWidth < $(".printer-details-container").scrollWidth)){
// your element have overflow
console.log("print Scroll..here...");
$(".printer-header").css({"width":"96.9999%;"});
}
else{
//your element don't have overflow
console.log("no print Scroll..here...");
$(".printer-header").css({"width":"100%;"});
//console.log($(".printer-header").print-header.css({"width":"100%;"});
}
最佳答案
看看这个:
https://jsfiddle.net/4Lms5uzz/
首先,第二个 div 设置为 50 x 50。
如果 #rules div 中存在滚动条,则第二个 div 的尺寸将发生变化。
基本上方法是divWithScrollbar.clientHeight < divWithScrollbar.scrollHeight
scrollHeight 获取div 内内容的高度,clientHeight 获取div 的大小。
如果 scrollHeight 小于 scrollHeight,则滚动条可见。
关于javascript - 动态检测滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33805052/