Authored by 梁志锋

颜色尺码选择优化

@@ -28,9 +28,6 @@ var $chosePanel = $('#chose-panel'), @@ -28,9 +28,6 @@ var $chosePanel = $('#chose-panel'),
28 $sizeRowList, 28 $sizeRowList,
29 $curColorBlock, 29 $curColorBlock,
30 $colorRowList, 30 $colorRowList,
31 - $curSizeRow,  
32 - $curColorRow,  
33 - rowIndex,  
34 cbFn, 31 cbFn,
35 $allChoseItems, 32 $allChoseItems,
36 queryString, 33 queryString,
@@ -48,9 +45,6 @@ function init() { @@ -48,9 +45,6 @@ function init() {
48 $sizeRowList = $('.size-list ul'); 45 $sizeRowList = $('.size-list ul');
49 $colorRowList = $('.color-list ul'); 46 $colorRowList = $('.color-list ul');
50 $leftNum = $('#left-num'); 47 $leftNum = $('#left-num');
51 - $curSizeRow = $sizeRowList.eq(0);  
52 - $curColorRow = $colorRowList.eq(0);  
53 - rowIndex = 0;  
54 curColorIndex = 0; 48 curColorIndex = 0;
55 curSizeIndex = 0; 49 curSizeIndex = 0;
56 } 50 }
@@ -138,6 +132,28 @@ function displayGoodNum(curGoodNum) { @@ -138,6 +132,28 @@ function displayGoodNum(curGoodNum) {
138 } 132 }
139 } 133 }
140 134
  135 +//老的选中尺码去掉勾选,新的选中尺码加上勾选
  136 +function changeSizeChosed(oldSizeIndex) {
  137 + if (curColorIndex && $curSizeBlock && $curSizeBlock.length > 0) {
  138 + $curSizeBlock.removeClass('chosed');
  139 + $curSizeBlock = $($sizeRowList.eq(oldSizeIndex).children().get(curColorIndex - 1));
  140 + $curSizeBlock.addClass('chosed');
  141 + return $curSizeBlock.data('num');
  142 + }
  143 + return 0;
  144 +}
  145 +
  146 +//老的选中颜色去掉勾选,新的选中颜色加上勾选
  147 +function changeColorChosed(oldColorIndex) {
  148 + if (curSizeIndex && $curColorBlock && $curColorBlock.length > 0) {
  149 + $curColorBlock.removeClass('chosed');
  150 + $curColorBlock = $($colorRowList.eq(oldColorIndex).children().get(curSizeIndex - 1))
  151 + $curColorBlock.addClass('chosed');
  152 + return $curColorBlock.data('num');
  153 + }
  154 + return 0;
  155 +}
  156 +
141 init(); 157 init();
142 158
143 $yohoPage.on('touchstart', '.chose-panel', function(e) { 159 $yohoPage.on('touchstart', '.chose-panel', function(e) {
@@ -153,13 +169,9 @@ $yohoPage.on('touchstart', '.chose-panel', function(e) { @@ -153,13 +169,9 @@ $yohoPage.on('touchstart', '.chose-panel', function(e) {
153 169
154 $yohoPage.on('touchstart', '.color-list .block', function() { 170 $yohoPage.on('touchstart', '.color-list .block', function() {
155 var $this = $(this), 171 var $this = $(this),
156 - index,  
157 - curSizeBlock, 172 + index = $this.index(),
158 curGoodNum; 173 curGoodNum;
159 174
160 - $this.siblings('.chosed').removeClass('chosed');  
161 - index = $this.index();  
162 -  
163 // 当前颜色已经是选中状态,再点击时 175 // 当前颜色已经是选中状态,再点击时
164 if ($this.hasClass('chosed')) { 176 if ($this.hasClass('chosed')) {
165 177
@@ -176,38 +188,15 @@ $yohoPage.on('touchstart', '.color-list .block', function() { @@ -176,38 +188,15 @@ $yohoPage.on('touchstart', '.color-list .block', function() {
176 188
177 curSizeIndex = 0; 189 curSizeIndex = 0;
178 190
179 - //如果尺码已经是选择状态  
180 - if (curColorIndex) {  
181 -  
182 - // 之前选中的尺码去掉勾选样式,新尺码块勾选  
183 - if ($curSizeBlock && $curSizeBlock.length > 0) {  
184 - $curSizeBlock.removeClass('chosed');  
185 - curSizeBlock = $sizeRowList.eq(0).children().get(curColorIndex - 1);  
186 - $curSizeBlock = $(curSizeBlock);  
187 - curGoodNum = $curSizeBlock.data('num');  
188 - $curSizeBlock.addClass('chosed');  
189 - }  
190 - } 191 + //老的选中尺码去掉勾选,新的选中尺码加上勾选
  192 + changeSizeChosed(0);
191 193
192 // 当前颜色不是选中状态,选中时 194 // 当前颜色不是选中状态,选中时
193 } else { 195 } else {
194 hasChooseColor = true; 196 hasChooseColor = true;
195 197
196 - //如果尺码已经是选择状态  
197 - if (curColorIndex) {  
198 -  
199 - // 之前选中的尺码去掉勾选样式,新尺码块勾选  
200 - if ($curSizeBlock && $curSizeBlock.length > 0) {  
201 - $curSizeBlock.removeClass('chosed');  
202 -  
203 - //curSizeBlock = $sizeRowList.eq(curSizeIndex + 1).children().get(curColorIndex - 1);  
204 - curSizeBlock = $sizeRowList.eq(index + 1).children().get(curColorIndex - 1);  
205 - $curSizeBlock = $(curSizeBlock);  
206 - curGoodNum = $curSizeBlock.data('num');  
207 - $curSizeBlock.addClass('chosed');  
208 - }  
209 -  
210 - } 198 + //老的选中尺码去掉勾选,新的选中尺码加上勾选
  199 + curGoodNum = changeSizeChosed(index + 1);
211 200
212 // 显示剩余数量 201 // 显示剩余数量
213 displayGoodNum(curGoodNum); 202 displayGoodNum(curGoodNum);
@@ -225,7 +214,8 @@ $yohoPage.on('touchstart', '.color-list .block', function() { @@ -225,7 +214,8 @@ $yohoPage.on('touchstart', '.color-list .block', function() {
225 $imgsThumb.addClass('hide').eq(index).removeClass('hide'); 214 $imgsThumb.addClass('hide').eq(index).removeClass('hide');
226 } 215 }
227 216
228 - // 当前颜色块 切换勾选样式 217 + // 颜色块切换勾选样式
  218 + $this.siblings('.chosed').removeClass('chosed');
229 $this.toggleClass('chosed'); 219 $this.toggleClass('chosed');
230 $('#good-num').val(1); 220 $('#good-num').val(1);
231 221
@@ -233,13 +223,9 @@ $yohoPage.on('touchstart', '.color-list .block', function() { @@ -233,13 +223,9 @@ $yohoPage.on('touchstart', '.color-list .block', function() {
233 updateConformButtonClassAndText(); 223 updateConformButtonClassAndText();
234 }).on('touchstart', '.size-list .block', function() { 224 }).on('touchstart', '.size-list .block', function() {
235 var $this = $(this), 225 var $this = $(this),
236 - index,  
237 - curColorBlock, 226 + index= $this.index(),
238 curGoodNum; 227 curGoodNum;
239 228
240 - $this.siblings('.chosed').removeClass('chosed');  
241 - index = $this.index();  
242 -  
243 // 当前尺码已经是选中状态,再点击时 229 // 当前尺码已经是选中状态,再点击时
244 if ($this.hasClass('chosed')) { 230 if ($this.hasClass('chosed')) {
245 231
@@ -256,35 +242,15 @@ $yohoPage.on('touchstart', '.color-list .block', function() { @@ -256,35 +242,15 @@ $yohoPage.on('touchstart', '.color-list .block', function() {
256 242
257 curColorIndex = 0; 243 curColorIndex = 0;
258 244
259 - //如果颜色已经是选择状态  
260 - if (curSizeIndex) {  
261 -  
262 - // 之前选中的颜色去掉勾选样式,新颜色块勾选  
263 - if ($curColorBlock && $curColorBlock.length > 0) {  
264 - $curColorBlock.removeClass('chosed');  
265 - curColorBlock = $colorRowList.eq(0).children().get(curSizeIndex - 1);  
266 - $curColorBlock = $(curColorBlock);  
267 - curGoodNum = $curColorBlock.data('num');  
268 - $curColorBlock.addClass('chosed');  
269 - }  
270 - } 245 + //老的选中颜色去掉勾选,新的选中颜色加上勾选
  246 + changeColorChosed(0);
271 247
272 // 当前尺码不是选中状态,选中时 248 // 当前尺码不是选中状态,选中时
273 } else { 249 } else {
274 hasChooseSize = true; 250 hasChooseSize = true;
275 251
276 - //如果颜色已经是选择状态  
277 - if (curSizeIndex) {  
278 -  
279 - // 之前选中的颜色去掉勾选样式,新颜色块勾选  
280 - if ($curColorBlock && $curColorBlock.length > 0) {  
281 - $curColorBlock.removeClass('chosed');  
282 - curColorBlock = $colorRowList.eq(index + 1).children().get(curSizeIndex - 1);  
283 - $curColorBlock = $(curColorBlock);  
284 - curGoodNum = $curColorBlock.data('num');  
285 - $curColorBlock.addClass('chosed');  
286 - }  
287 - } 252 + //老的选中颜色去掉勾选,新的选中颜色加上勾选
  253 + curGoodNum = changeColorChosed(index + 1);
288 254
289 // 显示剩余数量 255 // 显示剩余数量
290 displayGoodNum(curGoodNum); 256 displayGoodNum(curGoodNum);
@@ -299,6 +265,8 @@ $yohoPage.on('touchstart', '.color-list .block', function() { @@ -299,6 +265,8 @@ $yohoPage.on('touchstart', '.color-list .block', function() {
299 $curSizeBlock = $this; 265 $curSizeBlock = $this;
300 } 266 }
301 267
  268 + // 颜色块切换勾选样式
  269 + $this.siblings('.chosed').removeClass('chosed');
302 $this.toggleClass('chosed'); 270 $this.toggleClass('chosed');
303 $('#good-num').val(1); 271 $('#good-num').val(1);
304 272