brand.js
5.25 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
/**
* 品牌一览
* @author: liangzhifeng<zhifeng.liang@yoho.cn>
* @date: 2015/10/23
*/
var $ = require('jquery'),
Hammer = require('yoho.hammer'),
Swiper = require('yoho.iswiper'),
loading = require('../plugin/loading'),
lazyLoad = require('yoho.lazyload');
var swiper,
$fixTitleBar,
$brandList = $('.brand-list'),
$icon = $('.search-icon'),
$genderItem = $('.genderNav li'),
hotBrandsSwiper;
var searchH = $('.newbrand-search').outerHeight(),
headerH = $('.yoho-header').outerHeight(),
brandSwipe = parseInt(searchH) + parseInt(headerH) - 1,
minBrandListTop;
var brandsData,
$keyword,
clearTextHammer;
loading.showLoadingMask();
//热门品牌滑动
hotBrandsSwiper = new Swiper('.brands-swiper', {
grabCursor: true,
slidesPerView: 'auto',
wrapperClass: 'brands-list',
slideElement: 'li'
});
swiper = new Swiper('.swiper-container', {
lazyLoading: true,
loop: true,
autoplay: 3000,
pagination: '.swiper-pagination .pagination-inner'
});
lazyLoad($('img.lazy'));
$('.yoho-header').css({
'z-index': 2,
position: 'fixed',
top: 0
});
(function() {
if ($('.banner-top').length > 0) {
$('.hot-brands').css('padding-top', '0');
}
$('.hide-when-loading').show();
loading.hideLoadingMask();
})();
$fixTitleBar = $('<div class="title-bar fixed-title-bar"><h2></h2></div>');
$fixTitleBar.css({
position: 'fixed',
top: brandSwipe
}).hide();
minBrandListTop = brandSwipe + $('.hot-brands').outerHeight() + $('.banner-top').outerHeight();
$brandList.last().append($fixTitleBar);
function scrollHandler() {
var scrTop = $(window).scrollTop();
if ($brandList.eq(0).offset().top < minBrandListTop) {
$fixTitleBar.hide();
}
$brandList.each(function() {
var offTop = $(this).offset().top - brandSwipe;
if (scrTop >= offTop) {
$fixTitleBar.css({
display: 'block'
}).find('h2').html($(this).find('.title-bar').text());
}
});
}
//srcoll to load more
$(window).scroll(function() {
window.requestAnimationFrame(scrollHandler);
});
function changeBackground() {
var $brandList = $('.brand-list').find('p');
$brandList.on('touchstart', function() {
$brandList.css('background', '#fff');
$(this).css('background', '#eee');
}).on('touchend touchcancel', function() {
$(this).css('background', '#fff');
});
}
changeBackground();
function searchResult() {
var keyword = ($keyword.val() + '').toLowerCase();
var result = {},
i = 0,
html = '';
if (keyword !== '') {
// 遍历首字母搜索
$.each(brandsData, function(k, v) {
if ($.isArray(v)) {
// 遍历品牌,进行匹配
$.each(v, function(i, brand) {
if (brand.name.toLowerCase().indexOf(keyword) > -1) {
result[k] = result[k] || [];
result[k].push(brand);
}
});
}
});
// 根据搜索结果生成 HTML
$.each(result, function(k, v) {
var brandHtml = ['<div class="brand-list bar-', i, '">'];
i++;
brandHtml.push('<div class="title-bar"><h2>');
brandHtml.push(k);
brandHtml.push('</h2></div>');
$.each(v, function(i, brand) {
brandHtml.push('<p><a href="' + brand.url + '">' + brand.name);
if (brand.isNew) {
brandHtml.push('<i class="icon-new">NEW</i>');
}
if (brand.isHot) {
brandHtml.push('<i class="icon-hot">HOT</i>');
}
brandHtml.push('</a></p>');
});
brandHtml.push('</div>');
html += brandHtml.join('');
});
}
// 插入 dom,绑定事件
$('.search-result').html(html);
changeBackground();
}
if ($('.brand-search-page').length) {
brandsData = $.parseJSON($('#brands-data').html());
$keyword = $('#keyword');
$keyword.on('input', function() {
if ($keyword.val().length) {
$icon.css('color', '#000');
$(this).closest('.search-box').css('width', '11.25rem');
$('.search-action').show();
} else {
$icon.css('color', '#b2b2b2');
$(this).closest('.search-box').css('width', '12.5rem');
$('.search-action').hide();
}
searchResult();
}).focus();
clearTextHammer = new Hammer($('.clear-text')[0]);
clearTextHammer.on('tap', function(e) {
e.preventDefault();
$('.search-result').html('');
$('#keyword').val('').trigger('input');
e.srcEvent.stopPropagation();
});
$('form.search-box').on('submit', function() {
return false;
});
}
if ($genderItem.length > 0) {
$genderItem.on('touchstart', function() {
var index = $(this).data('id') + 1;
$('.genderNav ul .active').removeClass('active');
$(this).addClass('active');
$('.hide-when-loading').hide();
loading.showLoadingMask();
function reload() {
window.location.search = 'channel=' + index;
}
setTimeout(reload.bind(this), 100);
});
}