shop.js
22.1 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/**
* 品牌店铺首页
*/
var $ = require('jquery'),
IScroll = require('iscroll/iscroll-probe'),
lazyLoad = require('yoho.lazyload'),
Swiper = require('yoho.iswiper'),
bannerSwiper,
multiSwiper,
myScroll,
imgH,
nav1H,
main1H,
main1oH,
nav2H,
main2oH,
scH,
$nav1 = $('#pos-nav'),
$nav2 = $('#pos-list'),
sTop;
var tip = require('../plugin/tip'),
filter = require('../plugin/filter'),
loading = require('../plugin/loading');
var $subNav = $('.home-sub-nav'),
$goodsContainer = $('#goods-container'),
$goodsChildren = $goodsContainer.children(),
$ngc = $($goodsChildren.get(0)),
$pgc = $($goodsChildren.get(1)),
$dgc = $($goodsChildren.get(2)),
shopId = $('input[name="shop_id"]').val(),
appVersion = $('input[name="app_version"]').val(),
brand = $('input[name="brand"]').val(),
favId = $('input[name="favId"]').val();
var winH = $(window).height(),
noResult = '<p class="no-result">未找到相关搜索结果</p>';
//默认筛选条件
var defaultOpt = require('./query-param');
var $listNav = $('#list-nav'),
//导航数据信息
navInfo = {
new: {
order: 1,
reload: true,
page: 1,
end: false
},
hot: {
order: 1,
reload: true,
page: 1,
end: false
},
newest: {
order: 1,
reload: true,
page: 0,
end: false
},
price: {
order: 1,
reload: true,
page: 0,
end: false
},
discount: {
order: 1,
reload: true,
page: 0,
end: false
}
},
$pre = $listNav.find('.active'), //纪录进入筛选前的active项,初始为选中项
searching;
var viewType = 1,// 1-首页,2-上新,3-人气
listCount = $('.category-list li').length,
listModValue = 4 - listCount % 4,
listIndex;
lazyLoad($('img.lazy'));
//焦点效果
if ($('.banner-swiper').find('li').size() > 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 ($('.multi-browse').find('li').size() > 1) {
multiSwiper = new Swiper('.multi-browse', {
lazyLoading: true,
lazyLoadingInPrevNext: true,
lazyLoadingOnTransitionStart: true,
grabCursor: true,
slidesPerView: 'auto',
slideElement: 'li',
watchSlidesVisibility: true
});
}
//根据热门品类的个数来改变样式展示
//
if ($('.category-list li').length % 4 !== 0) {
$('.category-list li:last-child').addClass('category-list-last-li');
}
if ($('.category-list li').length < 4) {
$('.category-list').addClass('category-list-top-board');
$('.category-list').find('.buriedpoint').addClass('category-list-only-one-row');
}
if ($('.category-list li').length >= 5) {
for (listIndex = listModValue; listIndex >= 0; listIndex--) {
$('.category-list li').eq(listCount - 4 + listIndex - 1).addClass('category-list-last-full-row');
}
}
function getPageGoods(info) {
var nav,navType;
if (searching) {
return;
}
navType = info.data.type;
nav = navInfo[navType];
//不需要重新加载并且数据请求结束
if (nav.end) {
return;
}
searching = true;
$.ajax({
type: 'GET',
url: info.url,
data: info.data,
success: function(data) {
if (data === ' ') {
nav.end = true;
}
info.callBack(data);
},
error: function() {
alert('网络断开连接了~');
searching = false;
}
});
}
function getParam(req) {
if (brand) {
req.data.brand = brand;
}
if (shopId) {
req.data.shop_id = shopId;
}
}
function newData(callback) {
var req = {};
req.url = location.protocol + '//m.yohobuy.com/index/search/search';
req.data = {
type: 'newest',
order: '1',
page: navInfo.new.page,
tagNew: '1'
};
getParam(req);
req.callBack = function(data) {
$('#new-arrival').append(data);
navInfo.new.page++;
myScroll.refresh();
lazyLoad($('#new-arrival .lazy'));
scH = $('#scroller').outerHeight();
searching = false;
callback && $.isFunction(callback) && callback();
};
getPageGoods(req);
}
function hotData(callback) {
var req = {};
req.url = location.protocol + '//m.yohobuy.com/index/search/search';
req.data = {
type: 'hot',
order: '1',
page: navInfo.hot.page,
showTag: '1'
};
getParam(req);
req.callBack = function(data) {
$('#popularity').append(data);
navInfo.hot.page++;
myScroll.refresh();
lazyLoad($('#popularity .lazy'));
scH = $('#scroller').outerHeight();
searching = false;
callback && $.isFunction(callback) && callback();
};
getPageGoods(req);
}
function tabChange(dom, index) {
var li = dom.eq(index);
dom.removeClass('active');
li.addClass('active');
dom.removeClass('color');
li.addClass('color');
}
//首页导航
$.jqtab = function(nav, posNav, main) {
$(nav + ' li, ' + posNav + ' li').not('li.all-goods').on('touchstart', function() {
var index = $(this).index(),
activeTab = $(this).attr('tab');
$nav1.removeClass('fixed-top');
tabChange($(nav + ' li'), index);
tabChange($(posNav + ' li'), index);
$(main).hide();
$('#' + activeTab).fadeIn();
if (activeTab === 'new-arrival') {
newData();
viewType = 2;
} else if (activeTab === 'popularity') {
hotData();
viewType = 3;
} else if (activeTab === 'home-page') {
myScroll && myScroll.refresh();
scH = $('#scroller').outerHeight();
$nav1.removeClass('fixed-top');
$nav1.removeClass('absolute');
$nav2.removeClass('fixed-top');
$nav2.removeClass('absolute');
}
if (!$nav1.hasClass('hide')) {
myScroll.scrollTo(0, 0 - imgH);
}
$nav1.addClass('hide');
});
};
$.jqtab('#nav', '#pos-nav', '.main');
$('#wrapper').addClass('scroll-wrapper');
// lazyLoad($('img.lazy'));
myScroll = new IScroll('#wrapper', {
probeType: 3,
mouseWheel: true,
click: true
});
myScroll.on('scroll', function() {
var scrollCall,
sTop = -this.y;
if (sTop + winH * 2 > scH) {
switch (viewType) {
case 2:
scrollCall = function() {
$nav1.css({
transform: 'translate(0, ' + (-scH) + 'px)'
});
};
newData(scrollCall);
break;
case 3:
scrollCall = function() {
$nav1.css({
transform: 'translate(0, ' + (-scH) + 'px)'
});
};
hotData(scrollCall);
break;
}
}
if (sTop < imgH) {
if (!$nav1.hasClass('hide')) {
$nav1.addClass('hide');
}
if (!$nav2.hasClass('hide')) {
$nav2.addClass('hide');
} //滑动距离不到导航1
} else if (sTop < imgH + nav1H + main1oH) {
if ($nav1.hasClass('hide')) {
$nav1.removeClass('hide');
}
if (!$nav2.hasClass('hide')) {
$nav2.addClass('hide');
}//滑动距离不到导航2
if (sTop < imgH + main1H) {
if (!$nav1.hasClass('fixed-top')) {
$nav1.addClass('fixed-top')
.removeClass('absolute')
.css({
transform: 'translate(0, ' + (-scH) + 'px)'
});
}
} else if (sTop < imgH + nav1H + main1H) {
if ($('#home-page').is(':hidden')) {
return false;
}
if (!$nav1.hasClass('absolute')) {
$nav1.addClass('absolute')
.removeClass('fixed-top');
}
$nav1.css({
transform: 'translate(0, ' + (imgH + main1H - sTop - scH) + 'px)'
});
}
} else {
if ($('#home-page').is(':hidden')) {
return false;
}
if (!$nav1.hasClass('hide')) {
$nav1.addClass('hide');
}
if ($nav2.hasClass('hide')) {
$nav2.removeClass('hide');
}
if (sTop < scH) {
if (!$nav2.hasClass('absolute')) {
$nav2.addClass('absolute')
.removeClass('fixed-top');
}
$nav2.css({
transform: 'translate(0, ' + (-scH) + 'px)'
});
}
}
$('#scroller').trigger('scroll');
});
document.addEventListener('touchmove', function(e) {
e.preventDefault();
}, false);
/*if (!isIphone) {
return;
}*/
//window onload 后重新refresh iscroll
$(window).load(function() {
myScroll && myScroll.refresh();
imgH = $('#nav-top').outerHeight();
nav1H = $('#nav').outerHeight();
main1H = $('#nav-main').height();
main1oH = $('#nav-main').outerHeight();
nav2H = $('#list-nav').outerHeight();
main2oH = $('#goods-container').outerHeight();
setTimeout(function() {
scH = $('#scroller').outerHeight();
}, 1000);
});
function bindGoodThumbClick() {
$(document).trigger('rebindBpEvent');
}
function search(opt) {
var setting = {},
ext,
att,
nav, navType,
page;
if (searching) {
return;
}
if (!opt.url) {
opt.url = location.protocol + '//m.yohobuy.com/index/search/search';
}
if (!opt.nextPage) {
//筛选项变更则重置reload为true
for (att in navInfo) {
if (navInfo.hasOwnProperty(att)) {
navInfo[att].reload = true;
}
}
$listNav.children('.active').removeClass('active');
$pre.addClass('active');
if (opt.hasOwnProperty('id')) {
switch (opt.type) {
case 'shop_id':
ext = {
shop_id: opt.id
};
break;
case 'gender':
ext = {
gender: opt.id
};
break;
case 'brand':
ext = {
brand: opt.id
};
break;
case 'sort':
ext = {
sort: opt.id
};
break;
case 'color':
ext = {
color: opt.id
};
break;
case 'size':
ext = {
size: opt.id
};
break;
case 'price':
ext = {
price: opt.id
};
break;
case 'discount':
ext = {
discount: opt.id
};
break;
case 'ageLevel':
ext = {
age_level: opt.id
};
break;
}
$.extend(defaultOpt, ext); //扩展筛选项
}
}
//导航类别
if ($pre.hasClass('new')) {
navType = 'newest';
} else if ($pre.hasClass('price')) {
navType = 'price';
} else if ($pre.hasClass('discount')) {
navType = 'discount';
}
nav = navInfo[navType];
page = nav.page + 1;
if (nav.reload) {
page = 1;
} else if (nav.end) {
//不需要重新加载并且数据请求结束
return;
}
$.extend(setting, defaultOpt, {
type: navType,
order: nav.order,
page: page
});
if (brand) {
setting.brand = brand;
}
if (shopId) {
setting.shop_id = shopId;
}
searching = true;
loading.showLoadingMask();
$.ajax({
type: 'GET',
url: opt.url ? opt.url : '',
data: setting,
success: function(data) {
var $container,
num;
switch (navType) {
case 'newest':
$container = $ngc;
break;
case 'price':
$container = $pgc;
break;
case 'discount':
$container = $dgc;
break;
}
if (data === ' ') {
nav.end = true;
if (nav.reload) {
$container.html(noResult);
}
} else {
if (nav.reload) {
$container.html(data);
lazyLoad($container.find('.lazy'));
} else {
num = $container.find('.good-info').length;
$container.append(data);
lazyLoad($container.find('.good-info:gt(' + (num - 1) + ') .lazy'));
}
}
nav.reload = false;
nav.page = page;
searching = false;
loading.hideLoadingMask();
window.rePosFooter();
setTimeout(function() {
myScroll.refresh();
scH = $('#scroller').outerHeight();
}, 1000);
bindGoodThumbClick();
},
error: function() {
tip.show('网络断开连接了~');
searching = false;
loading.hideLoadingMask();
}
});
}
/**
* 获取url参数
*/
function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return window.unescape(r[2]);
}
return null;
}
$.ajax({
type: 'GET',
url: location.protocol + '//m.yohobuy.com/search/filter',
data: {
shop_id: brand ? '' : shopId,
brand: brand,
gender: getQueryString('gender'),
channel: getQueryString('channel')
},
success: function(data) {
$goodsContainer.append(data);
//初始化filter&注册filter回调
filter.initFilter({
fCbFn: search,
hCbFn: function() {
//切换active状态到$pre上
$pre.addClass('active');
$pre.siblings('.filter').removeClass('active');
},
missStatus: true
});
setTimeout(function() {
myScroll.refresh();
scH = $('#scroller').outerHeight();
}, 1000);
}
});
$listNav.bind('contextmenu', function(e) {
return false;
});
$subNav.on('touchend touchcancel', function(e) {
var $this = $(e.target).closest('li'),
cname,
nav,
navType,
$active;
var bpIdData = $this.attr('data-bp-id') || '';
e.preventDefault();
$(document).trigger('shouldSendBpData', [bpIdData]);
if (typeof $this === 'undefined' || $this.length === 0) {
return;
}
if ($this.hasClass('filter')) {
//筛选面板切换状态
if ($this.hasClass('active')) {
filter.hideFilter();
//点击筛选钱的active项回复active
$pre.addClass('active');
$this.removeClass('active');
} else {
$pre = $this.siblings('.active');
$pre.removeClass('active');
$this.addClass('active');
filter.showFilter();
}
} else {
if ($this.hasClass('new')) {
cname = '.new';
navType = 'newest';
} else if ($this.hasClass('price')) {
cname = '.price';
navType = 'price';
} else if ($this.hasClass('discount')) {
cname = '.discount';
navType = 'discount';
}
nav = navInfo[navType];
if ($this.hasClass('active')) {
//最新无排序切换
if ($this.hasClass('new')) {
return;
}
if ($this.hasClass('price') || $this.hasClass('discount')) {
$this = $subNav.find(cname);
// 价格/折扣切换排序状态
$this.find('.icon > .iconfont').toggleClass('cur');
$pre = $this; //更新pre为当前项
nav.reload = true; //重置reload,HTML会被替换为逆序的HTML
nav.order = nav.order === 0 ? 1 : 0; //切换排序
$goodsContainer.children('.container:not(.hide)').addClass('hide');
switch (navType) {
case 'newest':
$ngc.removeClass('hide');
break;
case 'price':
$pgc.removeClass('hide');
break;
case 'discount':
$dgc.removeClass('hide');
break;
}
}
} else {
$active = $subNav.find('.active');
$pre = $this; //$pre为除筛选导航的其他导航项,若当前active的为筛选,则把$pre置为当前点击项
if ($active.hasClass('filter')) {
//若之前active项为筛选,则隐藏筛选面板
filter.hideFilter();
} else {
//切换container显示
$goodsContainer.children('.container:not(.hide)').addClass('hide');
switch (navType) {
case 'newest':
$ngc.removeClass('hide');
break;
case 'price':
$pgc.removeClass('hide');
break;
case 'discount':
$dgc.removeClass('hide');
break;
}
}
$subNav.children().removeClass('active');
$subNav.find(cname).addClass('active');
}
if (nav.reload) {
search({
type: 'shop_id',
id: shopId,
brand: brand,
url: location.protocol + '//m.yohobuy.com/index/search/search',
nextPage: false
});
}
}
e.stopPropagation();
});
function scrollHandler() {
sTop = -myScroll.y;
//当scroll到1/4$goodsContainer高度后继续请求下一页数据
myScroll && myScroll.refresh();
if (sTop + winH > $('#scroller').height() - 0.25 * $goodsContainer.height() - 50) {
if ($pre !== undefined) {
search({
type: 'shop_id',
id: shopId,
brand: brand,
url: location.protocol + '//m.yohobuy.com/index/search/search',
nextPage: true
});
}
}
}
//srcoll to load more
$(window).scroll(function() {
window.requestAnimationFrame(scrollHandler);
});
//初始请求最新第一页数据
search({
type: 'shop_id',
id: shopId,
brand: brand,
url: location.protocol + '//m.yohobuy.com/index/search/search',
nextPage: false
});
$listNav.on('touchstart', 'li', function(e) {
$(this).addClass('bytouch');
}).on('touchend touchcancel', function() {
$listNav.find('li').removeClass('bytouch');
myScroll.refresh();
});
$nav2.on('touchstart', 'li', function(e) {
$(this).addClass('bytouch');
}).on('touchend touchcancel', function() {
$nav2.find('li').removeClass('bytouch');
myScroll.refresh();
});
//底部导航点击
function fotterClick(group) {
$(group + ' li').on('touchstart', function() {
$('.sub-group').addClass('hide');
if ($(this).find('.sub-group').length > 0) {
$(this).find('.sub-group').removeClass('hide');
}
});
}
fotterClick('.shop-foot-wrapper');
function stopPropagation(e) {
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
}
$(document).bind('touchstart', function() {
$('.sub-group').addClass('hide');
});
$('.shop-foot-wrapper').bind('touchstart', function(e) {
stopPropagation(e);
});
//收藏
function postCollect(info) {
var url = '';
if (searching) {
return;
}
searching = true;
$.ajax({
method: 'post',
url: info.url,
data: info.data,
success: function(data) {
if (data.code === 200) {
if ($('#collect').hasClass('alreadyCollect')) {
$('#collect').attr('class', 'notCollect');
} else {
$('#collect').attr('class', 'alreadyCollect');
}
}
if (data.code === 401) {
url = data.url;
$('body').append('<a href=\'' + url + '\'><span class="jump-login"><span></a>');
$('.jump-login').click();
}
setTimeout(function() {
myScroll.refresh();
scH = $('#scroller').outerHeight();
}, 1000);
searching = false;
},
error: function() {
alert('网络断开连接了~');
searching = false;
}
});
}
function collectShop() {
var qew = {};
qew.url = '/product/index/shopFav';
qew.data = {
shop_id: favId ? favId : shopId,
app_version: appVersion
};
postCollect(qew);
}
function cancelShop() {
var qew = {};
qew.url = '/product/index/cancelShopFav';
qew.data = {
shop_id: favId ? favId : shopId,
app_version: appVersion
};
postCollect(qew);
}
$('#collect').on('touchstart', function(e) {
if ($('#collect').hasClass('alreadyCollect')) {
cancelShop();
} else {
collectShop();
}
});