国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery實現判斷滾動條滾動到document底部的方法分析

2019-11-19 10:57:22
字體:
來源:轉載
供稿:網友

本文實例講述了jQuery實現判斷滾動條滾動到document底部的方法。分享給大家供大家參考,具體如下:

滾動條沒有實際的高度。只是為了呈現效果才在外型上面有長度。

在js當中也沒有提供滾動條的高度API。

參考了網上有關資料:判斷滾動條到底部的基本邏輯是滾動條滾動的高度加上視口的高度,正好是document的高度,公式表示為

滾動條滾動的高度+瀏覽器視口的高度>=document的高度。

參考網上資料,具體代碼如下:

//滾動條在Y軸上的滾動距離function getScrollTop() {    var scrollTop = 0,      bodyScrollTop = 0,      documentScrollTop = 0;    //兼容谷歌    if (document.body) {     bodyScrollTop = document.body.scrollTop;   }    //兼容火狐    if (document.documentElement) {     documentScrollTop = document.documentElement.scrollTop;   }       scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;}//文檔的總高度function getScrollHeight() {    var scrollHeight = 0,      bodyScrollHeight = 0,      documentScrollHeight = 0;    //兼容谷歌    if (document.body) {      bodyScrollHeight = document.body.scrollHeight;    }    //兼容火狐    if (document.documentElement) {      documentScrollHeight = document.documentElement.scrollHeight;    }    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;    return scrollHeight;}//瀏覽器視口的高度function getWindowHeight() {    var windowHeight = 0;    windowHeight = document.documentElement.clientHeight;    return windowHeight;}window.onscroll = function() {    if (getScrollTop() + getWindowHeight() == getScrollHeight()) {      alert("you are in the bottom!");    }};

jquery實現代碼:

$(window).scroll(function(){  var scrollTop = $(this).scrollTop();  var scrollHeight = $(document).height();  var windowHeight = $(this).height();  if(scrollTop + windowHeight == scrollHeight){    alert("you are in the bottom");  }});

代碼測試有效果。

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼運行效果。

更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery頁面元素操作技巧匯總》、《jQuery常見事件用法與技巧總結》、《jQuery常用插件及用法總結》、《jQuery擴展技巧總結》及《jquery選擇器用法總結

希望本文所述對大家jQuery程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁阳县| 大冶市| 新竹县| 永清县| 芦溪县| 麻江县| 广南县| 沂南县| 洛扎县| 武川县| 临清市| 安仁县| 隆安县| 宜宾县| 蓝田县| 通渭县| 陇南市| 洞头县| 健康| 闸北区| 彰化市| 彭州市| 高碑店市| 晋江市| 达孜县| 南部县| 雷州市| 贵南县| 依安县| 濮阳县| 英吉沙县| 沅陵县| 宁阳县| 泰顺县| 无极县| 花莲市| 濮阳市| 资中县| 建湖县| 乐东| 罗源县|