2012-06-08 8 views
7

Powiel możliwe:
Detecting presence of a scroll bar in a DIV using jQuery?Detect jeśli DIV mają pasek przewijania lub nie

Jest jak poniżej znaczników,

<div class="content">Lorem</div> 
<div class="content">Lorem Iorem Lorem Iorem Lorem Iorem Lorem Iorem Lorem IoremLorem Iorem Lorem Iorem Lorem Iorem</div> 
<div class="content">Lorem</div> 
<div class="content">Lorem</div> 

Jeśli zawartość mają pasek przewijania, a następnie powinien dodać klasę do tego div jak "scroll-image".

Wysokość może być różna dla DIV. Każde rozwiązanie jQuery do tego.

+0

Może sprawdzić, czy prąd jest większy niż wysokość począwszy wysokość? – silentw

Odpowiedz

26
(function($) { 
    $.fn.hasScrollBar = function() { 
     return this.get(0).scrollHeight > this.height(); 
    } 
})(jQuery); 

$('#my_div1').hasScrollBar(); // returns true if there's a `vertical` scrollbar, false otherwise.. 

Zrobione z How can I check if a scrollbar is visible?

+2

Masz przedstawiciela, dlaczego nie ma zamknięcia? – Esailija

1

Jak esailija powiedział duplikat: Detecting presence of a scroll bar in a DIV using jQuery?

Rozwiązaniem było następujące

var div= document.getElementById('something'); // need real DOM Node, not jQuery wrapper 
var hasVerticalScrollbar= div.scrollHeight>div.clientHeight; 
var hasHorizontalScrollbar= div.scrollWidth>div.clientWidth; 
6

trzeba porównać scrollHeight z height elementu tak:

$('.content').each(function(){ 
    if ($(this)[0].scrollHeight > $(this).height()) { 
    $(this).addClass('scroll-image'); 
    } 
}); 
+1

Masz przedstawiciela, dlaczego nie ma zamknięcia? – Esailija

+0

@Esailija: Pisałem odpowiedź, nie widziałem :) zrobione – Sarfraz

Powiązane problemy