resource-tfgoods-list.vue
8.56 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
<template>
<resource class="no-padding-right product-list-more" ref="resource">
<ul class="resource-tf-goods"
@touchstart="touchStart"
@touchmove="touchMove"
@touchend="touchEnd"
ref="goodsList"
:style="moreSlider"
:class="{'less-slider': value.data.list.length <= 3}"
>
<li class="product-item" v-for="(item, pi) in value.data.list" :key="pi">
<product-link :value="item" class="link" :yas="value" :yas-f="index" :yas-i="pi"></product-link>
<img-format :lazy="lazy" :src="item.default_images" :w="188" :h="250"></img-format>
<div class="ellipsis">
<p class="title" v-if="item.product_name">{{item.product_name}}</p>
<p class="price" v-if="item.sales_price">¥{{item.sales_price}}</p>
</div>
</li>
</ul>
<div class="more" :class="{'show-more': value.data.list.length <= 3}" :style="moreMove" ref="more" v-show="isShowMore" @click="moreAction"></div>
<a-link ref="linkA" :href="value.data.url"></a-link>
</resource>
</template>
<script>
import _ from 'lodash';
import Resource from './resource';
export default {
name: 'ResourceTfGoodsList',
data() {
return {
startX: 0,
endX: 0,
moveX: 0,
disX: 0,
moreSlider: '',
moreMove: '',
itemVisibleStatus: {},
checkTimer: ''
};
},
props: {
value: Object,
lazy: Boolean,
index: Number
},
computed: {
isShowMore() {
if (!this.value.data.url || this.value.data.url === 'http://www.yohobuy.com') {
return false;
} else {
return true;
}
}
},
methods: {
touchStart(ev) {
ev = ev || event;
// tounches类数组,等于1时表示此时有只有一只手指在触摸屏幕
if (ev.touches.length === 1) {
// 记录开始位置
this.startX = ev.touches[0].clientX;
}
},
touchMove(ev) {
let wd = this.$refs.goodsList.scrollWidth - this.$refs.goodsList.offsetWidth;
let moreWd = this.$refs.more.offsetWidth;
let scrollLeft = this.$refs.goodsList.scrollLeft;
if (this.value.data.list.length <= 3 || !this.isShowMore) {
return;
}
if (ev.touches.length === 1 && scrollLeft >= wd) {
// 滑动时距离浏览器左侧实时距离
this.moveX = ev.touches[0].clientX;
// 起始位置减去 实时的滑动的距离,得到手指实时偏移距离
this.disX = this.startX - this.moveX;
// 如果是向右滑动或者不滑动,不改变滑块的位置
if (this.disX < 0 || this.disX === 0) {
this.moreSlider = 'transform:translateX(0px)';
this.moreMove = 'transform:translateX(' + moreWd + 'px)';
// 大于0,表示左滑了,此时滑块开始滑动
} else if (this.disX > 0) {
// 具体滑动距离我取的是 手指偏移距离*5。
this.moreSlider = 'transform:translateX(-' + this.disX * 5 + 'px)';
this.moreMove = 'transform:translateX(' + moreWd - this.disX + 'px)';
// 最大也只能等于按钮宽度
if (this.disX * 5 >= moreWd) {
this.moreSlider = 'transform:translateX(-' + moreWd + 'px)';
this.moreMove = 'transform:translateX(0px)';
}
}
} else {
this.moreSlider = 'transform:translateX(0px)';
this.moreMove = 'transform:translateX(' + moreWd + 'px)';
}
},
touchEnd(ev) {
// 手动触发父组件曝光检查, touchEnd事件触发后延迟500ms检查
clearTimeout(this.checkTimer);
this.checkTimer = setTimeout(() => {
this.$parent.checkReqFromChild(this.index);
}, 500);
let wd = this.$refs.goodsList.scrollWidth - this.$refs.goodsList.offsetWidth;
let moreWd = this.$refs.more.offsetWidth;
let scrollLeft = this.$refs.goodsList.scrollLeft;
if (this.value.data.list.length <= 3 || !this.isShowMore) {
return;
}
if (ev.changedTouches.length === 1 && scrollLeft >= wd) {
let endX = ev.changedTouches[0].clientX;
this.disX = this.startX - endX;
// 如果距离小于按钮一半,强行回到起点
if ((this.disX * 5) < (moreWd / 2)) {
this.moreSlider = 'transform:translateX(0px)';
this.moreMove = 'transform:translateX(' + moreWd + 'px)';
} else {
// 大于一半 滑动到最大值
this.$refs.linkA.click();
this.moreSlider = 'transform:translateX(0px)';
this.moreMove = 'transform:translateX(' + moreWd + 'px)';
}
}
},
moreAction() {
if (this.value.data.list.length > 3) {
return;
}
this.$refs.linkA.click();
this.moreSlider = 'transform:translateX(0px)';
this.moreMove = 'transform:translateX(' + this.$refs.more.offsetWidth + 'px)';
},
// 用于检测楼层内部item的曝光状态, 暴露给父组件调用
checkReqFromParent(isInit) {
// 重置item的显示状态重新检测
_.each(this.itemVisibleStatus, (v, k) => {
this.itemVisibleStatus[k] = false
});
let rect;
let isVisible;
let visible = [];
let items = this.$el.querySelectorAll('.product-item');
_.each(items, (item, idx) => {
rect = item.getBoundingClientRect();
isVisible = (rect.left > 0 && rect.left < window.screen.width) ||
((rect.left + rect.width) > 0 && (rect.left + rect.width) < window.screen.width);
if (isVisible && !this.itemVisibleStatus[idx]) {
visible.push({
I_INDEX: idx + 1,
PRD_SKN: this.value.data && this.value.data.list[idx].product_skn
});
}
this.itemVisibleStatus[idx] = isVisible;
});
return visible;
}
},
components: {Resource}
};
</script>
<style lang="scss">
.resource-tf-goods {
width: 100%;
overflow-x: scroll;
-webkit-overflow-scrolling: touch;
white-space: nowrap;
overflow-y: hidden;
display: inline-block;
transition: 0.5s;
position: absolute;
left: 0;
right: 0;
z-index: 200;
background: #fff;
padding-left: 20px;
li.product-item {
position: relative;
display: inline-block;
padding-right: 20px;
text-align: center;
line-height: 40px;
.link {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
img {
width: 188px;
height: 250px;
display: block;
}
.ellipsis > p {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
font-size: 24px;
max-width: 188px;
line-height: 28px;
margin-top: 24px;
font-family: "SanFranciscoText-Regular";
&.price {
font-size: 28px;
margin-top: 20px;
}
}
}
}
.product-list-more {
position: relative;
height: 318px;
.more {
width: 113px;
height: 250px;
background: url("~statics/img/channel/more@2x.png") no-repeat;
background-size: contain;
position: absolute;
right: 0;
top: 20px;
transform: translateX(113px);
transition: 0.5s;
}
.show-more {
width: 98px;
position: relative;
vertical-align: top;
display: inline-block;
top: 0;
right: 0;
transform: translateX(0);
}
.less-slider {
width: auto;
position: relative;
right: auto;
padding-left: 0;
}
}
.scroller-box {
overflow-x: hidden;
}
</style>