[英]Hide/Show div with Javascript if a div has scrollbar
I have a div that has an inline height set to be 100% height of the browser window: 我有一个div,其内联高度设置为浏览器窗口的100%高度:
element.style {
height: 400px;
}
#scrollable-div {
overflow-x: hidden;
overflow-y: scroll;
position: relative;
width: 270px;
}
With JS or jQuery, I want to determine whether that div currently has a scrollbar, and if so, hide/show a different div. 使用JS或jQuery,我要确定该div当前是否具有滚动条,如果是,则隐藏/显示其他div。 Any thoughts?
有什么想法吗?
You can use this function to know if the element has scrollbar: 您可以使用此函数来知道元素是否具有滚动条:
$(document).ready(function() {
var mydiv = $('#scrollable-div');
console.log(mydiv1[0].scrollHeight);
console.log(mydiv1.height());
if (mydiv1[0].scrollHeight > mydiv1.height()) {
console.log(1);
//Has scrollbar
}
else {
console.log(2);
//Dont has scrollbar
}
});
I'd suggest having another div that contains the content and is within scrollable-div
. 我建议另一个包含内容
scrollable-div
并在scrollable-div
。 This will help track the height of the content for you to perform logic on. 这将有助于跟踪内容的高度,以便您执行逻辑。
if ($('#scrollable-inner').height() > $('#scrollable-div').height())
$('#dependent-div').hide();
else
$('#dependent-div').show();
See full code & demo at this jsFiddle . 在此jsFiddle上查看完整的代码和演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.