2012-06-08 33 views
7

thể trùng lặp:
Detecting presence of a scroll bar in a DIV using jQuery?Detect nếu DIV đã di chuyển thanh hay không

Có đánh dấu như dưới đây,

<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> 

Nếu nội dung có thanh cuộn, sau đó nó sẽ thêm lớp vào div đó như "scroll-image".

Chiều cao có thể khác nhau đối với DIV. Bất kỳ giải pháp jQuery cho việc này.

+0

Có thể kiểm tra nếu chiều cao hiện nay là lớn hơn bắt đầu từ chiều cao? – silentw

Trả lời

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.. 

Taken từ How can I check if a scrollbar is visible?

+2

Bạn có đại diện, tại sao không có lời mời? – Esailija

1

Như esailija nói, bản sao của: Detecting presence of a scroll bar in a DIV using jQuery?

Giải pháp là có những điều sau

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

Bạn cần phải so sánh với scrollHeightheight của phần tử như thế này:

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

Bạn có đại diện, tại sao không có đóng góp? – Esailija

+0

@Esailija: Đã viết câu trả lời, không thấy :) đã xong – Sarfraz

Các vấn đề liên quan