saunter-home.js
4.8 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var $ = require('yoho.jquery'),
lazyLoad = require('yoho.lazyload'),
Mustache = require('yoho.mustache'),
Mlellipsis = require('mlellipsis'),
Swiper = require('yoho.iswiper');
var $nav = $('#saunter-nav'),
$msgList = $('#msg-list'),
$loadMore = $('#load-more-info'),
$noMore = $loadMore.children('.no-more'),
$loading = $loadMore.children('.loading'),
loadH = $loadMore.height(),
end = false,
page = 1,
loading = false, //正在ajax标志
winH = $(window).height(),
query = $('#query').val(), //无author时
gender = $('#gender').val(),
clientType = $('#client-type').val(),
udid = $('#client-ip').val(),
tpl,
swiper;
//判断是否微信内置浏览器
function isWeixin() {
var ua = navigator.userAgent.toLowerCase();
var mm = ua.match(/MicroMessenger/i);
if (mm && mm.toString() === 'micromessenger') {
return true;
} else {
return false;
}
}
//微信浏览器隐藏头部显示
if (isWeixin()) {
$('.yoho-header').addClass('hide');
}
require('./wxshare')();
require('yoho.pjax');
Mlellipsis.init();
//init banner swiper
swiper = new Swiper('.swiper-container', {
pagination: '.swiper-pagination',
lazyLoading: true,
lazyLoadingInPrevNext: true,
autoplayDisableOnInteraction: false
});
lazyLoad();
//相关文章截取文字
function mleTxt() {
$('.tag-title, .tag-text').each(function() {
this.mlellipsis(2);
});
}
setTimeout(function() {
mleTxt();
}, 0);
//读取模板
$.get('/common/tagtpl', function (data) {
tpl = '{{#msgs}}' + data + '{{/msgs}}';
Mustache.parse(tpl);
});
//下拉加载数据
function loadMore() {
var $focusNav = $nav.find('li.focus'),
type = $focusNav.data('type'),
num = $msgList.find('.tag-content').length;
//-30,for chrome计算差
if (loading || end || $(window).scrollTop() + $(window).height() < $(document).height() - loadH - 30) {
return;
}
loading = true;
$loading.removeClass('hide');
$noMore.addClass('hide');
$.ajax({
type: 'GET',
url: '/tags/get',
data: {
max_sort_id: type,
page: page + 1,
query: query,
gender: gender,
client_type: clientType
}
}).then(function (data) {
var res, msgs, html,
$newContent;
if (data.code === 200) {
res = data.data;
if (res.end) {
end = true;
$loading.addClass('hide');
$noMore.removeClass('hide');
}
msgs = res.infos;
html = Mustache.render(tpl, {
msgs: msgs
});
$msgList.append(html);
page++;
//lazyload & mlellipsis the items appended
$newContent = $msgList.children('.tag-content:gt(' + (num - 1) + ')');
lazyLoad($newContent.find('img.lazy'));
$newContent.find('.tag-title, .tag-text').each(function() {
this.mlellipsis(2);
});
}
loading = false;
});
}
$(window).scroll(loadMore);
//资讯收藏和取消收藏
function bindPriseTap() {
$('.msg-list').delegate('.like-btn', 'touchstart', function (e) {
var $cur = $(e.currentTarget),
$info = $cur.closest('.tag-content'),
id = $info.data('id'),
url = '/guang/info/collect',
isLogin = $('#is-login').val(),
$loginTip = $('#login-tip'),
type = 'fav';
isLogin = isLogin ? isLogin : 'N';
if (isLogin === 'Y') {
if ($cur.hasClass('like')) {
type = 'cancel';
}
$.ajax({
type: 'GET',
url: url,
data: {
id: id,
type: type
}
}).then(function (data) {
if (data.code === 200) {
//更新点赞数
//$cur.next('span.like-count').text(data.data);
//收藏与取消收藏
$cur.toggleClass('like');
} else if (data.code === 400) {
//提示登录信息
$loginTip.fadeIn(500, function () {
setTimeout(function () {
$loginTip.fadeOut(500);
}, 1000);
});
}
});
}
}).bind('click', function(e) {
if (isLogin === 'Y') {
//阻止链接跳转
e.preventDefault();
}
});
}
bindPriseTap();
//pjax
// $(document).pjax('.pjax-link', '#pjax-container');
// $(document).on('pjax:end', function() {
// lazyLoad();
// mleTxt();
// //重新绑定prise
// bindPriseTap();
// });