tag.js
4.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* tag相关js
* @author: yue.liu@yoho.cn
* @date;2015/3/31
*/
var $ = require('jquery'),
ellipsis = require('mlellipsis'),
Mustache = require('mustache'),
timer = null;
require('lazyload');
/**
* 初始化页面加载时的文字截取和图片懒加载功能
*/
exports.init = function() {
var tpl;
//相关文章截取文字
ellipsis.init();
clearTimeout(timer);
timer = setTimeout(function(){
$('.tag-text').each(function() {
$(this).mlellipsis(2);
});
}, 0);
$(function() {
var $tagList = $('#tag-list'),
$loadMore = $('#load-more-info'),
$loadStatus = $loadMore.children('.status'),
$noMore = $loadStatus.filter('.no-more'),
$loading = $loadStatus.filter('.loading'),
winH = $(window).height(),
loadMoreH = $loadMore.height(),
canScroll = true,
dataEnd = false;
//请求相关数据
var page = 1,
query = $('#query').val(),
gender = $('#gender').val();
//定位登录提示相关变量
var $loginTip = $('#login-tip'),
winW,
tipH,
tipW;
//定位登录提示
winW = $(window).width();
tipH = $loginTip.height();
tipW = $loginTip.width();
$loginTip.css({
top: (winH - tipH) / 2,
left: (winW - tipW) / 2
});
//读取模板
$.get('/common/matchtpl', function(data) {
tpl = data.data;
Mustache.parse(tpl);
});
//文章点赞
$('#container').delegate('.like-btn', 'touchstart', function(e) {
var $cur = $(e.currentTarget),
$info = $cur.closest('.tag-content'),
id = $info.data('id');
$.ajax({
type: 'GET',
url: '/favorite/praise',
data: {
id: id
}
}).then(function(data) {
if (data.code === 200) {
$cur.toggleClass('like');
//更新点赞数
$cur.next('span.like-count').text(data.data);
} else if (data.code === 400) {
//提示登录信息
$('#login-tip').fadeIn(500, function() {
setTimeout(function() {
$('#login-tip').fadeOut(500);
}, 1000);
});
}
});
});
$(window).scroll(function() {
var setting;
if (!canScroll) {
return;
}
if ($(window).scrollTop() + winH >= $(document).height() - loadMoreH) {
if (!dataEnd) {
setting = {
page: page++,
query: query,
gender: gender
};
canScroll = false;
$.ajax({
type: 'GET',
url: '/tag/loadMatchs',
data: setting
}).then(function(data) {
var html = '',
res,
i;
if (data.success) {
if (data.end) {
dataEnd = true;
$loading.addClass('hide');
$noMore.removeClass('hide');
}
res = data.data;
for (i = 0; i < res.length; i++) {
html += Mustache.render(tpl, res[i]);
}
if (html !== '') {
$tagList.append(html);
//lazyload 不包含src即未加载的图片
$('img.lazy').lazyload({
container: $tagList
});
}
canScroll = true;
}
});
}
}
})
});
};