star-classroom.js
6.62 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
/**
* 星潮教室-首页
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/4/11
*/
var $ = require('jquery'),
Swiper = require('yoho.iswiper'),
calendar = require('../guang/calendar'),
lazyLoad = require('yoho.lazyload'),
tip = require('../plugin/tip');
var bannerSwiper, collocationSwiper;
lazyLoad($('img.lazy'));
$('body').addClass('star-class-body');
function loadIntimacyAvatar(myImage, src) {
var newSrc = '';
myImage.onload = function() {
newSrc = 'background-image:url(' + src + ')';
};
return newSrc;
}
// 日历弹出框显示及粉丝排行榜数据组装
function intimacyData(data) {
var $ul = $('.fan-charts-cont'),
$myIntimacy = $('.my-intimacy'),
$increased = $('.increased'),
signDay = data.signDay.split(','),
html = '',
style = '',
bgStyle = '',
nowDate = new Date(),
myImage = new Image(),
nowYear = nowDate.getFullYear(),
nowMonth = nowDate.getMonth(),
nowDaysNub = calendar.calculateMonthDays(nowMonth, nowYear);
var i, j;
if (signDay.length > 0) {
for (i = 0; i < nowDaysNub; i++) {
for (j = 0; j < signDay.length; j++) {
if (i === parseInt(signDay[j])) {
$('.now-days').eq(i - 1).addClass('signItem');
}
}
}
}
for (i = 0; i < data.fanCharts.length; i++) {
if (i === 0) {
style = 'font-bold';
} else {
style = '';
}
if (data.fanCharts[i].img !== '' && data.fanCharts[i].img !== null) {
myImage.src = data.fanCharts[i].img;
bgStyle = loadIntimacyAvatar(myImage, data.fanCharts[i].img);
} else {
bgStyle = '';
}
html += '<li>' +
'<i class="rank-ico">' + data.fanCharts[i].num + '</i>' +
'<span data-avatar="' + data.fanCharts[i].img + '" style="' + bgStyle +
'" class="rank-avatar"></span>' +
'<span class="fans-name">' + data.fanCharts[i].name + '</span>' +
'<div class="fans-intimacy">亲密度<span class="' + style + '">' +
data.fanCharts[i].intimacyNum + '</span></div>' +
'</li>';
}
$ul.html(html);
$myIntimacy.find('span').html(data.todayIntimacy);
$increased.find('span').html(data.todayIntimacy);
if (data.intimacyNum === 0) {
$('.my-intimacy').hide();
$('.increased').show();
} else {
$('.my-intimacy').show();
$('.increased').hide();
}
$('.pop-intimacy').show();
$('.classroom-mask').show();
$('body').css({
overflow: 'hidden'
});
}
//当前字符串字节数统计
function bytesCountAction(code, bytesCount) {
if (/^[\u0000-\u00ff]$/.test(code)) {
bytesCount += 1;
} else {
bytesCount += 2;
}
return bytesCount;
}
// 亲密度用户名字字数限制
function limitUsername() {
var $name = $('.home-floor-sign').find('.user-name'),
nameVal = $name.html(),
nameSize = nameVal.length,
bytesCount = 0,
bcount = 0,
newName = '';
var i, code;
for (i = 0; i < nameSize; i++) {
code = nameVal.charAt(i);
bytesCount = bytesCountAction(code, bytesCount);
if (bytesCount < 5) {
newName += code;
}
}
if (bytesCount > 8) {
newName += '..';
}
for (i = 0; i < nameSize; i++) {
code = nameVal.charAt(i);
bcount = bytesCountAction(code, bcount);
if (bcount > bytesCount - 2) {
newName += code;
}
}
$name.html(newName);
}
if ($('.banner-swiper').find('li').length > 1) {
bannerSwiper = new Swiper('.banner-swiper', {
lazyLoading: true,
lazyLoadingInPrevNext: true,
loop: true,
autoplay: 3000,
autoplayDisableOnInteraction: false,
paginationClickable: true,
slideElement: 'li',
pagination: '.banner-top .pagination-inner'
});
}
if ($('.collocation-swiper').find('li').length > 1) {
collocationSwiper = new Swiper('.collocation-swiper', {
lazyLoading: true,
lazyLoadingInPrevNext: true,
lazyLoadingOnTransitionStart: true,
grabCursor: true,
slidesPerView: 'auto',
slideElement: 'li',
watchSlidesVisibility: true
});
}
// 星鲜事显示点赞人数的区域
if ($('.artice-zan').find('li').length > 0) {
$('.zan-more').show();
}
if ($('.home-floor-sign').length > 0) {
limitUsername();
}
// 星鲜事点赞事件
$(document).on('touchstart', '.like-ico', function() {
var $that = $(this),
addString = '';
event.stopPropagation();
$.ajax({
type: 'POST',
url: '/guang/starclass/setPraise',
data: {
articleId: $that.parents('li').attr('articleId')
},
success: function(data) {
var code = data.code;
if (code === 200) {
if ($that.hasClass('like')) {
$that.removeClass('like');
} else {
$that.addClass('like');
}
if (data.num > 99) {
addString = '+';
} else {
addString = '';
}
$that.parent().find('.zan-more').html(data.num + addString);
}
},
error: function() {
tip.show('网络断开连接了~');
}
});
});
// 增加亲密度请求
$('.add-intimacy').on('touchstart', function() {
event.stopPropagation();
$.ajax({
type: 'GET',
url: '/guang/starclass/sign',
success: function(data) {
var code = data.code;
if (code === 200) {
intimacyData(data.data);
}
if (code === 201) {
window.location = data.data;
}
},
error: function() {
tip.show('网络断开连接了~');
}
});
});
// 关闭日历弹出窗事件
$('.pop-intimacy .pop-close, .classroom-mask').on('click', function() {
$('.pop-intimacy').hide();
$('.classroom-mask').hide();
$('body').css({
overflow: 'visible'
});
});
function setAvatar() {
var $userAvatar = $('.user-head'),
myImage = new Image(),
avatar;
// 部分老用户没有头像,显示默认头像
avatar = $userAvatar.data('avatar');
myImage.src = avatar;
myImage.onload = function() {
$userAvatar.css('background-image', 'url(' + avatar + ')');
};
}
if ($('.user-head').attr('data-avatar') !== '') {
setAvatar();
}