star.page.js
9.07 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* 星潮教室首页
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/5/31
*/
var $ = require('yoho-jquery'),
Swiper = require('yoho-swiper'),
tip = require('../plugin/tip'),
lazyLoad = require('yoho-jquery-lazyload'),
ellipsis = require('yoho-mlellipsis'),
loading = require('../plugin/loading'),
PullRefresh = require('../plugin/pull-refresh');
var $window = $(window),
stopLoading = false;
/*
$headTab = $('.head-tab'),
$swiperView = $('.swiper-view'),
$viewImg = $('.view-img'),
$body = $('body'),
imgs = [],
allImgs = {};
var mySwiper;
*/
var avatarKey, bannerLen, res, avatarSwiper, avatarSwiperClone, getIndexHtml, $avatarClone, $avatar, $starArticle;
require('../common');
lazyLoad($('img.lazy'));
ellipsis.init();
$window.on('mousewheel', false);
// 限制标题字数
function txtLimit(dom, item1, item2) {
var $title = dom.find(item1),
$cont = dom.find(item2);
$title[0].mlellipsis(2);
$cont[0].mlellipsis(3);
}
// 设置默认头像
function setAvatar($userAvatar) {
var myImage = new Image(),
avatar = '';
// 部分老用户没有头像,显示默认头像
avatar = $userAvatar.data('avatar');
myImage.onload = function() {
$userAvatar.css('background-image', 'url(' + avatar + ')');
};
if (avatar) {
myImage.src = avatar;
} else {
$userAvatar.addClass('default-avater');
}
}
// 明星头像点击居中显示或跳转
function bindAvatar(dom) {
avatarKey = dom.index();
if (!dom.hasClass('swiper-slide-active')) {
if (avatarKey >= (2 * bannerLen + 1)) {
avatarSwiperClone && avatarSwiperClone.slideTo(bannerLen + 1, 200);
avatarSwiper.slideTo(bannerLen + 1, 200);
} else if (avatarKey <= (bannerLen - 1)) {
avatarSwiperClone && avatarSwiperClone.slideTo(2 * bannerLen - 1, 200);
avatarSwiper.slideTo(2 * bannerLen - 1, 200);
} else {
avatarSwiperClone && avatarSwiperClone.slideTo(avatarKey, 200);
avatarSwiper.slideTo(avatarKey, 200);
}
return false;
}
}
function setIndexAction() {
var starIScroll;
var $loadingTip = $('.loading-tip'),
$avatarWrap = $('.avatar-wrap:not(.avatar-clone)'),
avatarHeight = $('.avatar').height();
// 下拉刷新,上拉加载
starIScroll = new PullRefresh('.star-wrap', {
height: $(window).height() - avatarHeight,
pullDown: function() {
if (!stopLoading) {
stopLoading = true;
if (avatarSwiperClone) {
avatarSwiperClone.destroy(true, true);
avatarSwiperClone = '';
}
getIndexHtml();
}
}
});
starIScroll.iScroll.on('scrollStart', function() {
// 下拉
if (this.directionY === -1) {
// $headTab.slideDown();
$loadingTip.slideDown();
}
});
starIScroll.iScroll.on('scroll', function() {
if ($avatarWrap.offset().top <= 0) {
$avatarClone.show();
if (!avatarSwiperClone) {
avatarSwiperClone = new Swiper('.avatar-0', {
loop: true,
initialSlide: $('.avatar-1').find('.swiper-slide-active').index() % bannerLen,
centeredSlides: true,
slidesPerView: 'auto',
loopedSlides: bannerLen,
spaceBetween: 10,
slidesOffsetBefore: -($('.swiper-num').width()),
watchSlidesProgress: true,
watchSlidesVisibility: true,
onInit: function() {
$('.avatar-0 li').on('click', function() {
res = bindAvatar($(this));
return res;
});
}
});
avatarSwiperClone.params.control = avatarSwiper;
avatarSwiper.params.control = avatarSwiperClone;
}
}
if ($avatarWrap.offset().top > $('.avatar-1')[0].scrollHeight) {
$avatarClone.hide();
}
});
starIScroll.iScroll.on('scrollEnd', function() {
// 上滑
// if (this.directionY === 1) {
// $headTab.slideUp(600);
// }
$loadingTip.slideUp();
});
$('.avatar').each(function(key, item) {
$(item).addClass('avatar-' + key);
});
// 明星头像 swiper 初始化
if (bannerLen > 1) {
avatarSwiper = new Swiper('.avatar-1', {
loop: true,
centeredSlides: true,
slidesPerView: 'auto',
loopedSlides: bannerLen,
spaceBetween: 10,
slidesOffsetBefore: -($('.swiper-num').width()),
watchSlidesProgress: true,
watchSlidesVisibility: true,
onInit: function() {
$('.avatar-1 li').on('click', function() {
res = bindAvatar($(this));
return res;
});
}
});
}
// 明星动态头像左右滑动
if ($('.article-avatar-swiper').find('li').length > 1) {
new Swiper('.article-avatar-swiper', {
initialSlide: 0,
lazyLoading: true,
lazyLoadingInPrevNext: true,
loop: true,
autoplay: 5000
});
}
// banner swiper 初始化
if ($('.banner-swiper').find('li').length > 1) {
new Swiper('.banner-swiper', {
lazyLoading: true,
lazyLoadingInPrevNext: true,
loop: true,
autoplay: 3000,
autoplayDisableOnInteraction: false,
paginationClickable: true,
slideElement: 'li',
pagination: '.banner-top .pagination-inner'
});
}
if ($('.star-info').find('li').length > 0) {
$starArticle.each(function(key, item) {
txtLimit($(item), '.article-title', 'p');
// articleImgAction($(item), key);
});
}
$('.rank-avatar').each(function(key, item) {
setAvatar($(item));
});
$('#yoho-header, .head-tab').on('touchmove', function() {
return false;
});
$('.artice-imgs-area img').on('load', function() {
starIScroll.iScroll.refresh();
});
}
// 首页数据请求
getIndexHtml = function() {
var $starMain = $('.star-main');
loading.showLoadingMask();
$.ajax({
url: '/guang/star/getIndexHtml',
dataType: 'html',
success: function(data) {
stopLoading = false;
if (!data) {
stopLoading = true;
tip.show('没有更多内容了');
}
$starMain.html(data);
$avatar = $('.avatar');
$starArticle = $('.star-article');
bannerLen = $avatar.find('.swiper-slide').length;
if ($avatarClone) {
$avatarClone.remove();
}
$starMain.before($('.avatar-swiper').clone().addClass('avatar-clone').hide());
$avatarClone = $('.avatar-clone');
// 限制标题字数
$('.cont-area').each(function() {
txtLimit($(this), '.title', '.cont-txt');
});
lazyLoad($('img.lazy'));
setTimeout(function() {
setIndexAction();
loading.hideLoadingMask();
}, 100);
},
error: function() {
tip.show('网络断开连接了~');
}
});
};
getIndexHtml();
// 明星动态文章图片相关操作
/*
function articleImgAction(dom, key) {
var $articeImgs = dom.find('.artice-imgs'),
$li = $articeImgs.find('li'),
imgLen = $articeImgs.find('img').length,
addFlagDom = $li.eq(2);
var i;
imgs = [];
$li.each(function() {
imgs.push($(this).find('img').attr('src'));
});
allImgs[key] = imgs;
$li.on('click', function() {
for (i = 0; i < imgLen; i++) {
$swiperView.find('ul').append('<li class="swiper-slide">' +
'<img class="swiper-lazy" data-src="' + allImgs[key][i] + '">' +
'<div class="swiper-lazy-preloader"></div></li>');
}
setTimeout(function() {
mySwiper = new Swiper('.swiper-view', {
lazyLoading: true,
lazyLoadingInPrevNext: true,
slideElement: 'li'
});
$('.swiper-view li').on('click', function(e) {
if (e.target.nodeName === 'IMG') {
return;
}
$viewImg.hide();
$body.css({
overflow: 'auto'
});
$viewImg.find('li').remove();
mySwiper.destroy(false);
});
}, 100);
$viewImg.show();
$body.css({
overflow: 'hidden'
});
});
if (imgLen > 3 && addFlagDom.find('.img-size').length < 1) {
addFlagDom.append('<div class="img-size"><i class="pic-icon"></i>' + imgLen + '</div>');
}
}
*/