7wp*_*7wp 22 javascript jquery scrollbar
我想使用jQuery检测DIV中是否存在滚动条.我正在考虑使用$('div').scrollTop()
但在滚动条位于顶部且根本没有滚动条的情况下返回0.
有什么想法吗?
bob*_*nce 47
假设overflow
div是auto
:
var div= document.getElementById('something'); // need real DOM Node, not jQuery wrapper
var hasVerticalScrollbar= div.scrollHeight>div.clientHeight;
var hasHorizontalScrollbar= div.scrollWidth>div.clientWidth;
Run Code Online (Sandbox Code Playgroud)
Plu*_*com 18
// plugtrade.com - jQuery detect vertical scrollbar function //
(function($) {
$.fn.has_scrollbar = function() {
var divnode = this.get(0);
if(divnode.scrollHeight > divnode.clientHeight)
return true;
}
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
例:
if($('#mydiv').has_scrollbar()) { /* do something */ }
Run Code Online (Sandbox Code Playgroud)