Authored by lore-w

商品列表鼠标移入效果增加边界判断

@@ -60,7 +60,8 @@ module.exports = function($o, rowWidth) { @@ -60,7 +60,8 @@ module.exports = function($o, rowWidth) {
60 var pMouseHover = new ProductEvent(); 60 var pMouseHover = new ProductEvent();
61 61
62 var targetWidth = $o.eq(0).width(), 62 var targetWidth = $o.eq(0).width(),
63 - targetHeight = $o.eq(0).height(); 63 + targetHeight = $o.eq(0).height(),
  64 + winW = $(window).width();
64 65
65 function handleEvent(event) { 66 function handleEvent(event) {
66 var $target, 67 var $target,
@@ -68,7 +69,9 @@ module.exports = function($o, rowWidth) { @@ -68,7 +69,9 @@ module.exports = function($o, rowWidth) {
68 targetY = 0, 69 targetY = 0,
69 rowW = rowWidth, 70 rowW = rowWidth,
70 activeIndex = 0, 71 activeIndex = 0,
71 - $targetDuplicate = ''; 72 + $targetDuplicate = '',
  73 + offsetL = 0,
  74 + offsetR = 0;
72 75
73 76
74 switch (event.type) { 77 switch (event.type) {
@@ -79,6 +82,11 @@ module.exports = function($o, rowWidth) { @@ -79,6 +82,11 @@ module.exports = function($o, rowWidth) {
79 activeIndex = $target.index() + 1; 82 activeIndex = $target.index() + 1;
80 targetX = (activeIndex % rowW) === 0 ? rowW : activeIndex % rowW; 83 targetX = (activeIndex % rowW) === 0 ? rowW : activeIndex % rowW;
81 targetY = Math.ceil(activeIndex / rowW); 84 targetY = Math.ceil(activeIndex / rowW);
  85 + offsetL = $target.offset().left;
  86 + offsetR = winW - (offsetL + targetWidth);
  87 +
  88 +
  89 + console.log($target.offset().left);
82 90
83 pMouseHover.fire({ 91 pMouseHover.fire({
84 type: 'MouseEnter', 92 type: 'MouseEnter',
@@ -89,7 +97,9 @@ module.exports = function($o, rowWidth) { @@ -89,7 +97,9 @@ module.exports = function($o, rowWidth) {
89 targetY: targetY, 97 targetY: targetY,
90 rowWidth: rowW, 98 rowWidth: rowW,
91 activeIndex: activeIndex, 99 activeIndex: activeIndex,
92 - targetDuplicate: $targetDuplicate 100 + targetDuplicate: $targetDuplicate,
  101 + offsetL: offsetL,
  102 + offsetR: offsetR
93 }); 103 });
94 break; 104 break;
95 case 'mouseleave': 105 case 'mouseleave':
@@ -15,7 +15,11 @@ var $goodsContainer = $('.goods-container'), @@ -15,7 +15,11 @@ var $goodsContainer = $('.goods-container'),
15 15
16 var MouseOver = productEvent($goodItem, 5); 16 var MouseOver = productEvent($goodItem, 5);
17 17
18 -// 构造html 18 +/**
  19 + * @description 构造商品颜色列表的html结构
  20 + * @param data 商品颜色的数组,[url: '',src: '']
  21 + * @return json {'colorListStr': '', 'ulNum': ''}
  22 + * */
19 function createColorList(data) { 23 function createColorList(data) {
20 var colorListStr = '', 24 var colorListStr = '',
21 len = data.length, 25 len = data.length,
@@ -49,13 +53,20 @@ function createColorList(data) { @@ -49,13 +53,20 @@ function createColorList(data) {
49 if (ulNum < col) { 53 if (ulNum < col) {
50 colorListStr += '</ul>'; 54 colorListStr += '</ul>';
51 } 55 }
52 - return colorListStr; 56 + return {
  57 + colorListStr: colorListStr,
  58 + ulNum: col
  59 + };
53 } 60 }
54 61
55 MouseOver.addHandler('MouseEnter', function(event) { 62 MouseOver.addHandler('MouseEnter', function(event) {
56 63
57 var itemMr = 10, //list的右边距 64 var itemMr = 10, //list的右边距
58 itemMb = 35, //list的下边距 65 itemMb = 35, //list的下边距
  66 + ulStr = '',
  67 + ulNum,
  68 + wrapperWidth,
  69 + diffWidth,
59 wrapperX = (event.targetX - 1) * (event.targetWidth + itemMr) - 21, 70 wrapperX = (event.targetX - 1) * (event.targetWidth + itemMr) - 21,
60 wrapperY = (event.targetY - 1) * (event.targetHeight + itemMb) + 25 - 19; 71 wrapperY = (event.targetY - 1) * (event.targetHeight + itemMb) + 25 - 19;
61 72
@@ -70,14 +81,25 @@ MouseOver.addHandler('MouseEnter', function(event) { @@ -70,14 +81,25 @@ MouseOver.addHandler('MouseEnter', function(event) {
70 dataType: 'json' 81 dataType: 'json'
71 }).then(function(data) { 82 }).then(function(data) {
72 83
  84 + ulStr = createColorList(data).colorListStr;
  85 + ulNum = createColorList(data).ulNum;
  86 +
73 $goodInfoMain.append(event.targetDuplicate); 87 $goodInfoMain.append(event.targetDuplicate);
74 - $goodSelectColor.append($(createColorList(data)));  
75 - }); 88 + $goodSelectColor.append($(ulStr));
  89 + wrapperWidth = 10 + (15 + 50) * ulNum + event.targetWidth;
  90 +
  91 + //
  92 + diffWidth = event.offsetR - ((15 + 50) * ulNum + 25);
  93 + if (diffWidth <= 0) {
  94 + wrapperX = wrapperX + diffWidth;
  95 + }
  96 + $goodItemWrapper.css({
  97 + width: wrapperWidth,
  98 + left: wrapperX,
  99 + top: wrapperY,
  100 + display: 'inline-block'
  101 + });
76 102
77 - $goodItemWrapper.css({  
78 - left: wrapperX,  
79 - top: wrapperY,  
80 - display: 'inline-block'  
81 }); 103 });
82 }); 104 });
83 105