判斷滾動條是否到底部

舞動乾坤發表於2017-10-21

```

// 解決鍵盤彈出後擋表單的問題

 window.addEventListener('resize', function() { 

 if( document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA' ) { 

 window.setTimeout(function() { 

 if('scrollIntoView' in document.activeElement) { 

 document.activeElement.scrollIntoView(); } 

else { document.activeElement.scrollIntoViewIfNeeded(); } }, 0);

 } });

```

document.documentElement.clientHeight+document.body.scrollTop =document.body.scrollHeight

判斷滾動條是否到底部


$(document).ready(function() {
  $(window).scroll(function() {
  
    if ($(document).scrollTop()<=0){
      alert("滾動條已經到達頂部為0");
    }
  
    if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
      alert("滾動條已經到達底部為" + $(document).scrollTop());
    }
  });
});
複製程式碼
HTML精確定位屬性:scrollLeft,scrollWidth,clientWidth,offsetWidth

scrollHeight: 獲取物件的滾動高度。
scrollLeft:設定或獲取位於物件左邊界和視窗中目前可見內容的最左端之間的距離
scrollTop:設定或獲取位於物件最頂端和視窗中可見內容的最頂端之間的距離
scrollWidth:獲取物件的滾動寬度
offsetHeight:獲取物件相對於版面或由父座標 offsetParent 屬性指定的父座標的高度
offsetLeft:獲取物件相對於版面或由 offsetParent 屬性指定的父座標的計算左側位置
offsetTop:獲取物件相對於版面或由 offsetTop 屬性指定的父座標的計算頂端位置
event.clientX 相對文件的水平座標
event.clientY 相對文件的垂直座標
event.offsetX 相對容器的水平座標
event.offsetY 相對容器的垂直座標
document.documentElement.scrollTop 垂直方向滾動的值
event.clientX+document.documentElement.scrollTop 相對文件的水平座標+垂直方向滾動的量
IE,FireFox 差異如下:
複製程式碼


clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
IE5.0/5.5:
clientWidth = width – border
clientHeight = height – border
offsetWidth = width
offsetHeight = height
複製程式碼


提示:CSS中的margin屬性,與clientWidth、offsetWidth、clientHeight、offsetHeight均無關

網頁可見區域寬: document.body.clientWidth
網頁可見區域高: document.body.clientHeight
網頁可見區域寬: document.body.offsetWidth (包括邊線的寬)
網頁可見區域高: document.body.offsetHeight (包括邊線的高)
網頁正文全文寬: document.body.scrollWidth
網頁正文全文高: document.body.scrollHeight
網頁被捲去的高: document.body.scrollTop
網頁被捲去的左: document.body.scrollLeft
網頁正文部分上: window.screenTop
網頁正文部分左: window.screenLeft
螢幕解析度的高: window.screen.height
螢幕解析度的寬: window.screen.width
螢幕可用工作區高度: window.screen.availHeight
螢幕可用工作區寬度: window.screen.availWidth
複製程式碼
在IE中:
document.body.clientWidth ==> BODY物件寬度
document.body.clientHeight ==> BODY物件高度
document.documentElement.clientWidth ==> 可見區域寬度
document.documentElement.clientHeight ==> 可見區域高度
document.documentElement.scrollTop =>視窗滾動條滾動高度
在FireFox中:
document.body.clientWidth ==> BODY物件寬度
document.body.clientHeight ==> BODY物件高度
document.documentElement.clientWidth ==> 可見區域寬度
document.documentElement.clientHeight ==> 可見區域高度
document.documentElement.scrollTop =>視窗滾動條滾動高度
在chrome中:
document.body.clientWidth ==> BODY物件寬度
document.body.clientHeight ==> BODY物件高度
document.documentElement.clientWidth ==> 可見區域寬度
document.documentElement.clientHeight ==> 可見區域高度
document.body.scrollTop =>視窗滾動條滾動高度
在Opera中:
document.body.clientWidth ==> 可見區域寬度
document.body.clientHeight ==> 可見區域高度
document.documentElement.clientWidth ==> 頁面物件寬度(即BODY物件寬度加上Margin寬)
document.documentElement.clientHeight ==> 頁面物件高度(即BODY物件高度加上Margin高
滾動到頂部 window.scrollTo(0,0)
滾動到尾部 window.scrollTo(0,document.body.clientHeight)複製程式碼


相關文章