goodsBox.vue
2.84 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
<template>
<div class="goods-box" v-if="goodsBoxs">
<div class="box">
<div class="bg">
<img :src="goodsBoxs.img" class="img" />
</div>
</div>
<div class="list-box">
<div class="scroll-box" ref="scrollBox">
<div class="box" :style="'width: ' + scrollItemBox + 'vw'">
<div class="item" v-for="(item, index) in goodsBoxs.list" :key="index">
<div class="a-link" @click="toDetail(item)">
<div class="img-box">
<img v-lazy="item.simg" class="img" />
</div>
<div class="title">
{{item.title}}
</div>
<div class="price">
¥{{item.price}}
</div>
<div class="prices" v-if="parseFloat(item.prices) > parseFloat(item.price)">
¥{{item.prices}}
</div>
<div class="prices" v-else>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
basicWidth: 100,
scrollItemBox: 100
}
},
props: ["goodsBoxs"],
methods: {
/**
* 跳转详情页
* @param {object} goods [商品详情对象]
*/
toDetail(goods) {
if (typeof this.goodsBoxs.funcs === "object" && typeof this.goodsBoxs.funcs.toDetail === "function") {
this.goodsBoxs.funcs.toDetail(goods);
}
},
/**
* 设置横向滚动
* @param {object} wrapper [需要设置滚动的对象]
*/
setScrollBox(wrapper) {
// box滚动元素宽度,item对象
let scrollItemBox = 0;
let clientWidth = wrapper.clientWidth;
let multiple = clientWidth / 750;
let childrens = wrapper.children[0].children;
// 遍历子元素,获取box宽度
for (let i = 0; i < childrens.length; i++) {
scrollItemBox += childrens[i].clientWidth;
}
// 根据屏幕比例计算实际设置vw值
// scrollItemBox = (scrollItemBox + ((30 * multiple * (childrens.length - 1)))) / clientWidth * this.basicWidth;
scrollItemBox = (scrollItemBox / clientWidth * this.basicWidth);
console.log(scrollItemBox);
// 判断scrollItemBox值,更新this.scrollItemBox宽度
if (scrollItemBox > this.basicWidth) {
this.$set(this, "scrollItemBox", scrollItemBox);
} else {
this.$set(this, "scrollItemBox", this.basicWidth);
}
}
},
mounted(){
this.setScrollBox(this.$refs.scrollBox);
},
watch: {
/**
* 监听横向滚动数据加载
*
*/
"goodsBoxs.list"(newVal, oldVal) {
// 判断是否有数据
if (typeof newVal === "object" && newVal.length > 0) {
// 下一周期处理
this.$nextTick(function() {
// 设置横向滚动
this.setScrollBox(this.$refs.scrollBox);
});
}
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style rel="stylesheet/less" lang="less" scoped>
@import './goods_box';
</style>