Authored by 郭成尧

reds-shop-scroll

@@ -14,6 +14,7 @@ const lazyLoad = require('yoho-jquery-lazyload'); @@ -14,6 +14,7 @@ const lazyLoad = require('yoho-jquery-lazyload');
14 14
15 class ProductListWithFilter { 15 class ProductListWithFilter {
16 constructor(filterParams, searchUrl) { 16 constructor(filterParams, searchUrl) {
  17 + this.scrollActived = true;
17 this.filterParams = filterParams; 18 this.filterParams = filterParams;
18 this.searchUrl = location.protocol + '//m.yohobuy.com/' + (searchUrl || 'product/search/search'); 19 this.searchUrl = location.protocol + '//m.yohobuy.com/' + (searchUrl || 'product/search/search');
19 this.filterUrl = location.protocol + '//m.yohobuy.com/product/search/filter'; 20 this.filterUrl = location.protocol + '//m.yohobuy.com/product/search/filter';
@@ -68,18 +69,20 @@ class ProductListWithFilter { @@ -68,18 +69,20 @@ class ProductListWithFilter {
68 * 滚动加载 69 * 滚动加载
69 */ 70 */
70 $(window).scroll(() => { 71 $(window).scroll(() => {
71 - setTimeout(() => {  
72 - let afterScroll = document.body.scrollTop;  
73 -  
74 - if (afterScroll - this.beforeScroll > 0) {  
75 - window.requestAnimationFrame(() => {  
76 - this.scrollHandler();  
77 - });  
78 - this.beforeScroll = afterScroll;  
79 - } else {  
80 - return false;  
81 - }  
82 - }, 5); 72 + if (this.scrollActived) {
  73 + setTimeout(() => {
  74 + let afterScroll = document.body.scrollTop;
  75 +
  76 + if (afterScroll - this.beforeScroll > 0) {
  77 + window.requestAnimationFrame(() => {
  78 + this.scrollHandler();
  79 + });
  80 + this.beforeScroll = afterScroll;
  81 + } else {
  82 + return false;
  83 + }
  84 + }, 5);
  85 + }
83 }); 86 });
84 } 87 }
85 88
@@ -3,25 +3,38 @@ require('product/search/list.page.css'); @@ -3,25 +3,38 @@ require('product/search/list.page.css');
3 require('product/shop/redshop.page.css'); 3 require('product/shop/redshop.page.css');
4 4
5 const $ = require('yoho-jquery'); 5 const $ = require('yoho-jquery');
6 -const allProduct = require('./shop/all-product'); 6 +const shopId = $('#shopId').val();
  7 +const ProductListWithFilter = require('product/product-list-with-filter');
7 let Tab = require('../plugin/tab'); 8 let Tab = require('../plugin/tab');
8 9
9 let $filterBox = $('.filter-box'); 10 let $filterBox = $('.filter-box');
10 let $shopCategory = $('#shop-category'); 11 let $shopCategory = $('#shop-category');
11 let $listNav = $('#list-nav'); // 筛选项列表 12 let $listNav = $('#list-nav'); // 筛选项列表
12 13
  14 +/**
  15 + * 初始化全部商品 TAB 页
  16 + */
  17 +let productListWithFilter = new ProductListWithFilter({
  18 + shop_id: shopId
  19 +});
  20 +
  21 +productListWithFilter.scrollActived = false;
  22 +
13 Tab.prototype.home = function() { // 隐藏筛选 TAB 23 Tab.prototype.home = function() { // 隐藏筛选 TAB
14 $filterBox.css('display', 'none'); 24 $filterBox.css('display', 'none');
  25 + productListWithFilter.scrollActived = false;
15 }; 26 };
16 Tab.prototype.getallgoods = function() { // 显示筛选 TAB 27 Tab.prototype.getallgoods = function() { // 显示筛选 TAB
17 $listNav.find('li').removeClass('active'); 28 $listNav.find('li').removeClass('active');
18 $listNav.find('.default').addClass('active'); 29 $listNav.find('.default').addClass('active');
19 $filterBox.css('display', 'block'); 30 $filterBox.css('display', 'block');
20 - allProduct.getGoodsList({type: 'default', page: 1}); 31 + productListWithFilter.getGoodsList({type: 'default', page: 1});
  32 + productListWithFilter.scrollActived = true;
21 }; 33 };
22 Tab.prototype.getnewgoods = function() { 34 Tab.prototype.getnewgoods = function() {
23 $filterBox.css('display', 'none'); 35 $filterBox.css('display', 'none');
24 - allProduct.getGoodsList({type: 'new', page: 1}); 36 + productListWithFilter.getGoodsList({type: 'new', page: 1});
  37 + productListWithFilter.scrollActived = true;
25 }; 38 };
26 39
27 /** 40 /**
@@ -35,7 +48,7 @@ if ($shopCategory.length > 0) { @@ -35,7 +48,7 @@ if ($shopCategory.length > 0) {
35 48
36 49
37 // 页面打开直接加载筛选项 50 // 页面打开直接加载筛选项
38 -allProduct.getFilter(); 51 +productListWithFilter.getFilter();
39 52
40 require('../plugin/sticky'); 53 require('../plugin/sticky');
41 require('./shop/coupon'); 54 require('./shop/coupon');
1 -/*  
2 - * @Author: Targaryen  
3 - * @Date: 2017-03-24 13:43:55  
4 - * @Last Modified by: Targaryen  
5 - * @Last Modified time: 2017-06-20 09:50:01  
6 - */  
7 -  
8 -/** *****************************  
9 - * 红人店铺使用的全部商品页面碎片  
10 - *******************************/  
11 -  
12 -const filter = require('../../plugin/filter');  
13 -const lazyLoad = require('yoho-jquery-lazyload');  
14 -  
15 -let noResultHbs = require('product/search/no-result-new.hbs');  
16 -  
17 -let $goodsContainer = $('#goods-container');  
18 -let $container = $goodsContainer.children('.default-goods'); // 承载商品列表的容器  
19 -let $listNav = $('#list-nav'); // 筛选项列表  
20 -let $pre = $listNav.find('.active'); // 记录进入筛选前的active项,初始为选中项  
21 -let $allGoodsTabPage = $(document.getElementById('all-goods'));  
22 -let $filterBody = $('.filter-body');  
23 -let $dropList = $('.drop-list');  
24 -  
25 -const shopId = $('#shopId').val();  
26 -  
27 -let navInfo = {  
28 - price: {  
29 - order: 1,  
30 - end: false  
31 - },  
32 - discount: {  
33 - order: 1,  
34 - end: false  
35 - },  
36 - default: {  
37 - order: 0,  
38 - end: false,  
39 - type: 'default'  
40 - },  
41 - new: {  
42 - order: 0,  
43 - end: false  
44 - },  
45 - sale: {  
46 - order: 0,  
47 - end: false  
48 - },  
49 - popularity: {  
50 - order: 0,  
51 - end: false  
52 - }  
53 -};  
54 -  
55 -let beforeScroll = document.body.scrollTop; // 滚动前位置记录  
56 -let navType = 'default'; // 目前激活的导航页面  
57 -let defaultOpt = Object.assign({}, navInfo.default, {shop_id: shopId}); // 默认参数  
58 -let onSearching = false; // 是否正在搜索  
59 -let isScrollLoad = false; // 是否是滚动加载  
60 -let page = 1; // 页码  
61 -let nav;  
62 -let $firstText = $('.first-li-more').find('.nav-txt');  
63 -let $thisLi = '';  
64 -  
65 -// 物料商品列表标记  
66 -if ($('#material-flag').val() === 'material') {  
67 - let material = true;  
68 -  
69 - if ($('.first-li-more').hasClass('active')) {  
70 - page = 2; // 已在服务端渲染第一页  
71 - }  
72 - Object.assign(defaultOpt, {  
73 - material: material,  
74 - unionType: window.queryString.union_type  
75 - });  
76 -}  
77 -  
78 -/**  
79 - * 处理筛选参数  
80 - */  
81 -const handleChoseFilter = function() {  
82 - $.each($filterBody.find('.chosed'), function(index, elem) {  
83 - let choseOpt = {};  
84 -  
85 - choseOpt[$(elem).parent().data('type')] = $(elem).data('id');  
86 - Object.assign(defaultOpt, choseOpt);  
87 - });  
88 -};  
89 -  
90 -/**  
91 - * 获取商品列表  
92 - */  
93 -const getGoodsList = params => {  
94 - handleChoseFilter();  
95 -  
96 - if (!onSearching) {  
97 - Object.assign(defaultOpt, {  
98 - page: page++  
99 - });  
100 - }  
101 -  
102 - // 有参数,参数优先,滚动加载相关参数重置  
103 - if (params && !params.filtering) {  
104 - Object.assign(defaultOpt, params);  
105 - if (params.page) {  
106 - page = params.page + 1;  
107 - }  
108 - isScrollLoad = false;  
109 - beforeScroll = document.body.scrollTop;  
110 - }  
111 -  
112 - if (nav && nav.end) {  
113 - return false;  
114 - }  
115 -  
116 - if (!onSearching) {  
117 - onSearching = true;  
118 -  
119 - $.ajax({  
120 - type: 'GET',  
121 - url: location.protocol + '//m.yohobuy.com/product/search/search',  
122 - data: defaultOpt,  
123 - xhrFields: {  
124 - withCredentials: true  
125 - },  
126 - beforeSend: function() {  
127 - if ($('.no-result-new').length > 0) {  
128 - $('.no-result-new').remove();  
129 - }  
130 - $container.append('<div class="search-divide">正在加载...</div>');  
131 - },  
132 - success: function(result) {  
133 - // 去掉正在加载  
134 - $('.search-divide').remove();  
135 -  
136 - let noResult = !result || result.length < 1 || (result.list && result.list.length < 1);  
137 -  
138 -  
139 - // 没有结果输出没有结果页面  
140 - if (noResult) {  
141 - if (isScrollLoad) {  
142 - $container.append('<div class="search-divide">没有更多内容了...</div>');  
143 - } else {  
144 - $container.html(noResultHbs());  
145 - }  
146 -  
147 - if (nav) {  
148 - nav.end = true;  
149 - }  
150 -  
151 - onSearching = false;  
152 - return false;  
153 - }  
154 -  
155 - if (isScrollLoad) {  
156 - $container.append(result);  
157 - } else {  
158 - $container.html(result);  
159 - }  
160 -  
161 - lazyLoad($container.find('img[class=lazy]').not('img[src]'));  
162 -  
163 - onSearching = false;  
164 - },  
165 - error: function() {  
166 - let $divide = $('.search-divide');  
167 -  
168 - $divide.text('加载失败,点击重试');  
169 - $divide.one('click', function() {  
170 - $divide.text('正在加载...');  
171 - getGoodsList();  
172 - });  
173 - onSearching = false;  
174 - }  
175 - });  
176 - }  
177 -};  
178 -  
179 -/**  
180 - * 加载筛选数据  
181 - */  
182 -const getFilter = function() {  
183 - $.ajax({  
184 - type: 'GET',  
185 - url: location.protocol + '//m.yohobuy.com/product/search/filter',  
186 - data: defaultOpt,  
187 - success: function(data) {  
188 - if (!data) {  
189 - return false;  
190 - }  
191 -  
192 - $goodsContainer.append(data);  
193 -  
194 - // 初始化filter&注册filter回调  
195 - filter.initFilter({  
196 - fCbFn: getGoodsList,  
197 - hCbFn: function() {  
198 -  
199 - // 切换active状态到$pre上  
200 - $pre.addClass('active');  
201 - $pre.siblings('.filter').removeClass('active');  
202 - }  
203 - });  
204 -  
205 - $filterBody = $('.filter-body');  
206 - }  
207 - });  
208 -};  
209 -  
210 -$listNav.bind('contextmenu', function() {  
211 - return false;  
212 -});  
213 -  
214 -const popularityFilter = function(thisLi, type) {  
215 - if (thisLi.hasClass('active')) {  
216 - $dropList.hide();  
217 - return false;  
218 - }  
219 - $thisLi.addClass('active').siblings('li').removeClass('active');  
220 -  
221 - defaultOpt.type = type;  
222 -  
223 - nav = navInfo[type];  
224 -  
225 - if (thisLi.hasClass('asc')) {  
226 - nav.order = 1;  
227 - } else {  
228 - nav.order = 0;  
229 - }  
230 -  
231 - Object.assign(defaultOpt, nav);  
232 - getGoodsList();  
233 - $dropList.hide();  
234 -};  
235 -  
236 -$listNav.on('touchend touchcancel', function(e) {  
237 - page = 1;  
238 - beforeScroll = $(window).scrollTop();  
239 - isScrollLoad = false;  
240 -  
241 - let $this = $(e.target).closest('li'); // 被点击的 Tab  
242 - let $active;  
243 -  
244 - // 默认列表增加人气筛选  
245 - if ($this.hasClass('first-li-more') && $this.hasClass('active')) {  
246 - $this.siblings('.active').removeClass('active');  
247 - $this.addClass('active');  
248 - $dropList.toggle();  
249 - $dropList.find('.default').on('touchend touchcancel', function() {  
250 - $thisLi = $(this);  
251 - $firstText.html('默认');  
252 - popularityFilter($thisLi, 'default');  
253 - return false;  
254 - });  
255 - $dropList.find('.discount').on('touchend touchcancel', function() {  
256 - $thisLi = $(this);  
257 - $firstText.html($(this).data('text'));  
258 - popularityFilter($thisLi, 'discount');  
259 - $('.first-li-more').removeClass('new default sale discount').addClass('discount');  
260 - return false;  
261 - });  
262 - } else if ($this.hasClass('filter')) { // 筛选面板  
263 - $dropList.css('display', 'none');  
264 -  
265 - // 筛选面板切换状态  
266 - if ($this.hasClass('active')) {  
267 - filter.hideFilter();  
268 -  
269 - // 点击筛选前的 active 项恢复 active  
270 - $pre.addClass('active');  
271 - $this.removeClass('active');  
272 - } else {  
273 - $pre = $this.siblings('.active');  
274 -  
275 - $pre.removeClass('active');  
276 - $this.addClass('active');  
277 -  
278 - filter.showFilter();  
279 - }  
280 - } else { // 排序改变  
281 - $dropList.css('display', 'none');  
282 - if ($this.hasClass('new')) {  
283 - navType = 'new';  
284 - } else if ($this.hasClass('price')) {  
285 - navType = 'price';  
286 - } else if ($this.hasClass('discount')) {  
287 - navType = 'discount';  
288 - } else if ($this.hasClass('default')) {  
289 - navType = 'default';  
290 - } else if ($this.hasClass('sale')) {  
291 - navType = 'sale';  
292 - } else if ($this.hasClass('popularity')) {  
293 - navType = 'popularity';  
294 - }  
295 -  
296 - // 更新当前排序:默认、最新、价格、折扣  
297 - nav = navInfo[navType];  
298 -  
299 - if ($this.hasClass('active')) {  
300 -  
301 - // 默认、最新无排序切换  
302 - if ($this.hasClass('default') || $this.hasClass('new') || $this.hasClass('popularity')) {  
303 - return false;  
304 - }  
305 -  
306 - if ($this.hasClass('price') || $this.hasClass('discount')) {  
307 -  
308 - // 价格或折扣切换排序状态  
309 - $this.find('.icon > .iconfont').toggleClass('cur');  
310 - $pre = $this; // 更新 pre 为当前项  
311 - nav.order = nav.order === 0 ? 1 : 0; // 切换排序  
312 - }  
313 - } else {  
314 - $active = $this.siblings('.active');  
315 -  
316 - $pre = $this; // $pre为除筛选导航的其他导航项,若当前active的为筛选,则把$pre置为当前点击项  
317 -  
318 - if ($active.hasClass('filter')) {  
319 -  
320 - // 若之前active项为筛选,则隐藏筛选面板  
321 - filter.hideFilter();  
322 - }  
323 -  
324 - $active.removeClass('active');  
325 - $this.addClass('active');  
326 - }  
327 -  
328 - /* 排序条件更新 */  
329 - defaultOpt.type = navType;  
330 - Object.assign(defaultOpt, nav);  
331 -  
332 - getGoodsList();  
333 - }  
334 -});  
335 -  
336 -  
337 -/**  
338 - * 当scroll到1/2$goodsContainer高度后继续请求下一页数据  
339 - */  
340 -const scrollHandler = function() {  
341 - if ($allGoodsTabPage.hasClass('active') && $(window).scrollTop() > $goodsContainer.height() * 0.6) {  
342 - isScrollLoad = true;  
343 - getGoodsList();  
344 - }  
345 -};  
346 -  
347 -/**  
348 - * 滚动加载  
349 - */  
350 -$(window).scroll(function() {  
351 - setTimeout(function() {  
352 - let afterScroll = document.body.scrollTop;  
353 -  
354 - if (afterScroll - beforeScroll > 0) {  
355 - window.requestAnimationFrame(scrollHandler);  
356 - beforeScroll = afterScroll;  
357 - } else {  
358 - return false;  
359 - }  
360 - }, 5);  
361 -});  
362 -  
363 -module.exports = {  
364 - getGoodsList,  
365 - getFilter  
366 -};