簡體   English   中英

使用JQuery / Javascript檢測垂直滾動條是否存在

[英]Detect vertical scrollbar present or not using JQuery/Javascript

有一個HTML報告,其中我已將樣式應用為“ overflow:auto” 。數據是動態填充的,因此我需要檢測報告中是否存在垂直滾動條 在研究了許多論壇之后,所有這些論壇都建議比較一下scrollHeightclientHeight ,我嘗試了一下:

  $( document ).ready(function() {
  var myReport = document.getElementById("report_id");
  if (myReport.scrollHeight > myReport.clientHeight) { 
  alert("has a scrollbar");   
  } else {
  alert("has NO scrollbar");   
  }
  });

這是行不通的。 結果始終為true,即“沒有滾動條”,即使沒有垂直滾動條也是如此。


請提出建議。
PS:以上代碼僅用於測試目的。

<script>
jQuery(document).ready(function(){
   var outerHeight = $("#report_id").outerHeight();
   var scrollHeight = $('#report_id')[0].scrollHeight
   if (outerHeight < scrollHeight) {
       alert("has a scrollbar"); 
   }
   else
   {
      alert("has NO scrollbar"); 
   }
});
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM