zoukankan      html  css  js  c++  java
  • jQuery scroll事件实现监控滚动条分页示例(转)

    这篇文章主要介绍了jQuery scroll事件实现监控滚动条分页简单示例,使用ajax加载,同时介绍了(document).height()与$(window).height()的区别,需要的朋友可以参考下。

    scroll事件适用于window对象,但也可滚动iframe框架与CSS overflow属性设置为scroll的元素。

     1 $(document).ready(function () { //本人习惯这样写了
     2     $(window).scroll(function () {
     3         //$(window).scrollTop()这个方法是当前滚动条滚动的距离
     4         //$(window).height()获取当前窗体的高度
     5         //$(document).height()获取当前文档的高度
     6         var bot = 50; //bot是底部距离的高度
     7         if ((bot + $(window).scrollTop()) >= ($(document).height() - $(window).height())) {
     8            //当底部基本距离+滚动的高度〉=文档的高度-窗体的高度时;
     9             //我们需要去异步加载数据了
    10             $.getJSON("url", { page: "2" }, function (str) { alert(str); });
    11         }
    12     });
    13 });

    注意:(window).height()和(document).height()的区别

    jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用.

    注意当浏览器窗口大小改变时(如最大化或拉大窗口后) jQuery(window).height() 随之改变,但是jQuery(document).height()是不变的。

    $(document).scrollTop() //获取垂直滚动的距离  即当前滚动的地方的窗口顶端到整个页面顶端的距离
    $(document).scrollLeft() //这是获取水平滚动条的距离

    要获取顶端 只需要获取到scrollTop()==0的时候  就是顶端了

    要获取底端 只要获取scrollTop()>=$(document).height()-$(window).height()  就可以知道已经滚动到底端了

    $(document).height()  //是获取整个页面的高度
    $(window).height()  //是获取当前 也就是你浏览器所能看到的页面的那部分的高度  这个大小在你缩放浏览器窗口大小时 会改变 与document是不一样的  根据英文应该也能理解吧

    自己做个实验就知道了

    $(document).scroll(function(){
        $("#lb").text($(document).scrollTop());
    })
    <span id="lb" style="top:100px;left:100px;position:fixed;"></span><!--一个固定的span标记 滚动时方便查看-->

    来源:http://www.jb51.net/article/48714.htm

  • 相关阅读:
    背水一战 Windows 10 (26)
    背水一战 Windows 10 (25)
    背水一战 Windows 10 (24)
    背水一战 Windows 10 (23)
    背水一战 Windows 10 (22)
    背水一战 Windows 10 (21)
    背水一战 Windows 10 (20)
    背水一战 Windows 10 (19)
    背水一战 Windows 10 (18)
    背水一战 Windows 10 (17)
  • 原文地址:https://www.cnblogs.com/rushlight/p/4450051.html
Copyright © 2011-2022 走看看