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

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 15:35:23
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 临沭县| 平江县| 曲周县| 府谷县| 平顶山市| 嘉义市| 宜城市| 通化县| 澎湖县| 台前县| 清镇市| 宁晋县| 安庆市| 牡丹江市| 吉安市| 乐安县| 山东省| 桐梓县| 原平市| 临洮县| 察雅县| 资源县| 苏尼特左旗| 晋江市| 凤翔县| 芦溪县| 海丰县| 巴林右旗| 紫金县| 开封市| 云浮市| 汉川市| 兰西县| 北安市| 蒙自县| 墨竹工卡县| 昌邑市| 兴山县| 玉山县| 嵊泗县| 南靖县|