product.js
7.94 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
/**
*Description: 商品列表页
*Author: chenglong.wang@yoho.cn
*Date: 2015/12/2
*/
var $ = require('yoho.jquery'),
lazyLoad = require('yoho.lazyload'),
Handlebars = require('yoho.handlebars'),
productEvent = require('../common/product-event');
var $goodsContainer = $('.goods-container'),
$goodItem = $goodsContainer.find('.good-info'),
$goodItemWrapper = $goodsContainer.find('.good-item-wrapper'),
$goodInfoMain = $goodsContainer.find('.good-info-main'),
$goodSelectColor = $goodsContainer.find('.good-select-color'),
$productListNav = $('.product-list-nav'),
productList;
lazyLoad($('img.lazy'));
/**
* @description 初始化鼠标移入商品列表弹层效果
* @num 每列存放商品的个数
*/
exports.init = function(num) {
var $shopEntry = $('.shop-entry'),
$shopEntryArea = $('.shop-entry-area'),
shopEntryLen = $shopEntry.length;
if (shopEntryLen > 1) {
if (shopEntryLen === 3) {
$shopEntry.css({
width: '32%'
});
} else if (shopEntryLen % 2) {
// 大于 2 ,并且是基数
$('.shop-entry:gt(' + (shopEntryLen - 4) + ')').css({
width: '32%'
});
}
$('.shop-entry:nth-child(2n+1)').css({
marginLeft: 0
});
$shopEntry.last().css({
marginLeft: '2%'
});
if (shopEntryLen % 2 === 0) {
$shopEntry.find('.name').css({
width: '50%'
});
} else if (shopEntryLen % 3 === 0) {
$shopEntry.find('.name').css({
width: '26%'
});
}
} else {
$shopEntry.css({
width: '100%'
});
}
$shopEntryArea.slideDown();
productList = null;
$goodItem.unbind();
productList = productEvent($goodItem, num);
/**
* @description 构造商品颜色列表的html结构
* @param data 商品颜色的数组,[url: '',src: '']
* @return json {'colorListStr': '', 'ulNum': ''}
* */
function createColorList(data, _from) {
var colorListStr = '',
len = data.length,
row = 4, //每列ul放4个li
col = Math.ceil(len / row), //需要几**列**ul
i,
j,
index,
ulNum = 0;
for (i = 0; i < col; i++) {
colorListStr += '<ul>';
for (j = 0; j < row; j++) {
index = i * row + j;
if (index === len) {
break;
}
colorListStr +=
'<li>' +
'<a target="_blank" href="' + data[i * row + j].url + _from + '">' +
'<img src="' + data[i * row + j].src + '" data-cover="' + data[i * row + j].coverImg + '" />' +
'</a>' +
'</li>';
if (j === row - 1) {
colorListStr += '</ul>';
ulNum++;
}
}
}
if (ulNum < col) {
colorListStr += '</ul>';
}
return {
colorListStr: colorListStr,
ulNum: col
};
}
function removeHtmlFn() {
$goodInfoMain.html('');
$goodSelectColor.html('');
$goodItemWrapper.css({
display: 'none'
});
}
productList.addHandler('MouseEnter', function(event) {
var itemMr = 10, //list的右边距
itemMb = 35, //list的下边距
ulStr = '',
ulNum,
wrapperWidth,
diffWidth,
wrapperX,
wrapperY,
wrapperPl, //鼠标移入时弹层的左内边距
wrapperPt, //鼠标移入时弹层的上内边距
containerPt, //商品列表容器的上内边距
_from = event.target.attr('data-from') || '';
$.ajax({
type: 'POST',
url: '/product/list/getProductPic',
dataType: 'jsonp',
jsonp: 'callback',
data: {
skn: event.target.attr('data-skn'),
skc: event.target.attr('data-skc')
}
}).then(function(res) {
var data = res.data,
colorList = createColorList(data.pics, _from);
removeHtmlFn();
ulStr = colorList.colorListStr; //ajax请求的颜色列表
ulNum = colorList.ulNum;//ajax请求的颜色的数量
$goodInfoMain.append(event.targetDuplicate);
$goodSelectColor.append($(ulStr));
// 点击收藏商品不需要了
// if (data.isFavorite) {
// $goodInfoMain.find('.col-btn').addClass('coled');
// } else {
// $goodInfoMain.find('.col-btn').removeClass('coled');
// }
wrapperPl = $goodItemWrapper.css('paddingLeft');
wrapperPt = $goodItemWrapper.css('paddingTop');
containerPt = $goodsContainer.css('paddingTop');
wrapperWidth = 10 + (15 + 50) * ulNum + event.targetWidth;
//wrapperWidth = $goodItemWrapper.width();
wrapperX = (event.targetX - 1) * (event.targetWidth + itemMr) - (parseInt(wrapperPl) + 1);
wrapperY = (event.targetY - 1) *
(event.targetHeight + itemMb) + parseInt(containerPt) - (parseInt(wrapperPt) + 1);
//todo
//event.offsetR表示当前列表距离浏览器右侧边缘的距离
diffWidth = event.offsetR - ((15 + 50) * ulNum + 25);
if (diffWidth <= 0) {
wrapperX = wrapperX + diffWidth - 25;
}
$goodItemWrapper.css({
width: wrapperWidth,
left: wrapperX,
top: wrapperY,
display: 'inline-block'
});
// 鼠标悬浮获取到商品信息后显示第一张图片
if (data.pics[0] && data.pics[0].src) {
$goodInfoMain.find('.good-thumb img').attr('src', data.pics[0].src);
}
});
});
productList.addHandler('MouseLeave', function() {
removeHtmlFn();
});
$goodItemWrapper.mouseleave(function() {
removeHtmlFn();
});
};
//鼠标放在颜色列表上效果
$(document).on('hover', '.good-select-color li', function() {
var coverImg = $(this).find('img').attr('data-cover'),
$coverImg = $(this).closest('.good-item-wrapper').find('.good-detail-img').find('img');
$coverImg.attr('src', coverImg);
});
// 左侧导航
$productListNav.find('h3').click(function(event) {
var $this = $(this).closest('.product-list-nav');
if ($this.hasClass('active')) {
$this.find('.sort-child-list').stop(true, true).slideUp();
} else {
$this.find('.sort-child-list').stop(true, true).slideDown();
}
$this.toggleClass('active');
});
//左侧需要ajax内容
(function() {
var $brandCard = $('#brand-card');
var $brandFavor = $('#brand-favor');
var html;
if ($brandCard.length) {
$.ajax({
type: 'POST',
url: '/product/index/getNodeContent',
data: {
node: $brandCard.data('node')
}
}).then(function(data) {
$brandCard.html(data);
});
}
//品牌页请求
if ($brandFavor.length) {
html = $('#pic-link-tpl').html();
$.ajax({
type: 'POST',
url: '/product/index/getAdnav',
data: {
brandId: $brandFavor.data('id')
}
}).then(function(data) {
if (data) {
$('#brand-ad').html(Handlebars.compile(html)({
picLink: data
}));
}
});
}
}());