coupons.page.js
2.06 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 不要使用es6
'use strict';
require('home/_coupons.css');
var $ = require('yoho-jquery'),
fastclick = require('yoho-fastclick'),
ellipsis = require('yoho-mlellipsis');
var $employ = $('#employ'),
$employ2 = $('#employ2'),
statu = 0,
page = 1,
dic = {},
AjaxFlag = 0; // 防止重复请求
require('../common');
ellipsis.init();
dic[statu + '_' + page] = true;
fastclick.attach(document.body);
var couponAJAX = function(statu, page) {
if (AjaxFlag || dic[statu + '_' + page]) {
return;
}
var employDom = statu === 0 ? $employ : $employ2;
AjaxFlag = 1;
$.ajax({
type: 'POST',
url: '/home/coupons',
dataType: 'html',
data: {
status: statu,
page: page
},
success: function(data) {
dic[statu + '_' + page] = true; // tab切换时,防止频繁请求
if ($(data).find('.null').html()) {
if (page === 1) {
employDom.append($(data).find('.null'));
}
AjaxFlag = 1;
return;
}
if (!$(data).find('.employ-main').html()) {
AjaxFlag = 1;
return;
}
employDom.append($(data).find('.employ-main'));
AjaxFlag = 0;
}
});
};
$('.employ span').each(function(index, el) {
$(el).on('click', function() {
$('.employ span').removeClass('active').eq(index).addClass('active');
if (index === 0) {
$employ.removeClass('hide');
$employ2.addClass('hide');
} else {
$employ.addClass('hide');
$employ2.removeClass('hide');
}
statu = index;
page = 1;
AjaxFlag = 0;
couponAJAX(statu, page);
});
});
var scrollHandler = function() {
if ($(window).scrollTop() + $(window).height() > $('body').height() - 100) {
page++;
couponAJAX(statu, page);
return;
}
};
$(window).scroll(function() {
window.requestAnimationFrame(scrollHandler);
});