guang-comments.js
1.41 KB
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
/*
* @Author: Targaryen
* @Date: 2017-05-25 14:36:33
* @Last Modified by: Targaryen
* @Last Modified time: 2017-05-25 15:28:52
*/
const $ = require('yoho-jquery');
const qs = window.queryString;
const article_id = qs.id || $('.detail').data('id');
const $comments = $('#comments');
let page = 0;
let beforeScroll = document.body.scrollTop; // 滚动前位置记录
let onLoading = false; // 是否正在加载
/**
* 异步加载评论
*/
const getComments = () => {
if (!onLoading) {
onLoading = true;
} else {
return false;
}
$.ajax({
type: 'get',
url: '//guang.m.yohobuy.com/guang/info/getComments',
data: {
article_id: article_id,
page: page++
},
success: function(result) {
$comments.append(result);
onLoading = false;
}
});
};
/**
* 当scroll到1/2$goodsContainer高度后继续请求下一页数据
*/
const scrollHandler = function() {
if ($(window).scrollTop() > $comments.height() * 0.6) {
getComments();
}
};
/**
* 滚动加载
*/
$(window).scroll(function() {
setTimeout(function() {
let afterScroll = document.body.scrollTop;
if (afterScroll - beforeScroll > 0) {
window.requestAnimationFrame(scrollHandler);
beforeScroll = afterScroll;
} else {
return false;
}
}, 5);
});
getComments();