原理很简单,就是为window添加一个scroll事件,浏览器每次触发scroll事件时判断是否滚动到了浏览器底部,如果到了底部则加载新数据。关键是计算滚动条是否滚动到了浏览器底部,算法如下
滚动条卷起来的高度 + 窗口高度 > 文档的总高度 + 50/*我这里将滚动响应区域高度取50px*/;如果这个判断为true则表示滚动条滚动到了底部。
实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<style type=
"text/css"
>
html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{
margin: 0;
padding:0;
}
*{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.waterfllow-loading{
z-index: 2000;
display:none;
}
.waterfllow-loading.active{
display:block;
}
.waterfllow-loading img.loading-progress{
position: fixed;
/*设置等待条水平居于窗口正中*/
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
/*不能设置margin-top:auto和margin-bottom:auto否则IE下bottom就不顶用了*/
bottom: 30px;
}
</style>
<div class=
"waterfllow-loading"
>
<img class=
"loading-progress"
src=
"busy.gif"
>
</div>
<script type=
"text/javascript"
>
//图片查询中正对浏览器主页面滚动事件处理(瀑布流)。只能使用window方式绑定,使用document方式不起作用
$(window).on(
'scroll'
,
function
(){
if
(scrollTop() + windowHeight() >= (documentHeight() - 50
/*滚动响应区域高度取50px*/
)){
waterallowData();
}
});
function
waterallowData(){
$(
'.waterfllow-loading'
).addClass(
'active'
);
/*$.ajax({
url:url,
type:"post",
data: params,
success:function(data,textStatus,jQXHR){
//添加数据
...
//隐藏加载条
$('.waterfllow-loading.active').removeClass('active');
}
});*/
}
|
获取页面顶部被卷起来的高度函数
1
2
3
4
5
6
7
8
|
//获取页面顶部被卷起来的高度
function
scrollTop(){
return
Math.max(
//chrome
document.body.scrollTop,
//firefox/IE
document.documentElement.scrollTop);
}
|
chrome浏览器和Firefox/IE对滚动条是属于body还是html理解不同导致处理不同。
获取页面文档的总高度
1
2
3
4
5
|
//获取页面文档的总高度
function
documentHeight(){
//现代浏览器(IE9+和其他浏览器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
return
Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
}
|
这个算法和jQuery计算文档高度的方法一致。
获取页面浏览器视口的高度
1
2
3
4
5
|
function
windowHeight(){
return
(document.compatMode ==
"CSS1Compat"
)?
document.documentElement.clientHeight:
document.body.clientHeight;
}
|
这里需要说明的是document.compatMode这个东东。很陌生,一般情况貌似没有遇到过。
document.compatMode有两个取值"BackCompat"和"CSS1Compat"。官方解释是BackCompat:标准兼容模式关闭。CSS1Compat:标准兼容模式开启。
IE对盒模型的渲染在 Standards Mode和Quirks Mode是有很大差别的,在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样,但在Quirks Mode模式下则有很大差别,而在不声明Doctype的情况下,IE默认又是Quirks Mode。
举个例子说明两种模式之间的差别有多大。
当document.compatMode等于"BackCompat"时,浏览器客户区宽度是document.body.clientWidth;
当document.compatMode等于CSS1Compat时,浏览器客户区宽度是document.documentElement.clientWidth。
还有其他属性类似。这里不说了,但是我们可以预见两种模式导致IE渲染的基石都更改了,可想而知构建出来的建筑物差别当有多大。
所以请为每一个页面声明Doctype不仅仅是一个好习惯,而且是一个必要的处理。Quirks Mode可以进垃圾堆了。
好了下面附上完整的代码,有一个小例子(没有后台刷数据,只是显示等待条)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
<!DOCTYPE html>
<
html
lang
=
"ch-cn"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
script
type
=
"text/javascript"
src
=
'jquery-1.9.1.js'
></
script
>
<
style
type
=
"text/css"
>
html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{
margin: 0;
padding:0;
}
*{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.waterfllow-loading{
z-index: 2000;
display:none;
}
.waterfllow-loading.active{
display:block;
}
.waterfllow-loading img.loading-progress{
position: fixed;
/*设置等待条水平居于窗口正中*/
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
/*不能设置margin-top:auto和margin-bottom:auto否则IE下bottom就不顶用了*/
bottom: 30px;
}
</
style
>
</
head
>
<
body
style
=
"background:#ff0;height:1000px;"
>
<
div
class
=
"waterfllow-loading"
>
<
img
class
=
"loading-progress"
src
=
"busy.gif"
>
</
div
>
</
body
>
<
script
type
=
"text/javascript"
>
//获取页面顶部被卷起来的高度
function scrollTop(){
return Math.max(
//chrome
document.body.scrollTop,
//firefox/IE
document.documentElement.scrollTop);
}
//获取页面文档的总高度
function documentHeight(){
//现代浏览器(IE9+和其他浏览器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
}
//获取页面浏览器视口的高度
function windowHeight(){
//document.compatMode有两个取值。BackCompat:标准兼容模式关闭。CSS1Compat:标准兼容模式开启。
return (document.compatMode == "CSS1Compat")?
document.documentElement.clientHeight:
document.body.clientHeight;
}
</
script
>
<
script
type
=
"text/javascript"
>
//图片查询中正对浏览器主页面滚动事件处理(瀑布流)。只能使用window方式绑定,使用document方式不起作用
$(window).on('scroll',function(){
if(scrollTop() + windowHeight() >= (documentHeight() - 50/*滚动响应区域高度取50px*/)){
waterallowData();
}
});
function waterallowData(){
$('.waterfllow-loading').addClass('active');
/*$.ajax({
url:url,
type:"post",
data: params,
success:function(data,textStatus,jQXHR){
//添加数据
...
//隐藏加载条
$('.waterfllow-loading.active').removeClass('active');
}
});*/
}
</
script
>
</
html
>
|
里面的加载条图片为
以上就是滚动条滚动到页面底部继续加载的处理实例,希望对大家的学习有所帮助。