horizList.vue 4.33 KB
<template>
    <div class="horiz-list-box" v-if="lists && lists.length">
		<slot name="title-box"></slot>
		<div class="box" :class="'com-' + com + ' pid-' + pid">
			<div class="h-box">
				<div class="scroll-box" ref="scrollBox">
					<div class="box" :style="'width: ' + scrollItemBox + 'vw'">
						<div class="item-box" v-for="(item, index) in lists" :key="index">

							<!--
								com=1:goodsItem_1组件,pid:1、3,固定不滚动;2、4,滚动
                                    事件:
                                        @jump:整个点击跳转事件
                                        @addCart:右下角加入购物车按钮事件,已阻止事件冒泡

								com=2:goodsItem_2组件,pid: 1,带标题,不显示附加信息;2、不带标题,显示附加信息
                                    事件:
                                        @jump:整个点击跳转事件

								com=3:goodsItem_3组件,无pid参数
                                    事件:
                                        @jump:整个点击跳转事件
                                        @primary:右下角红色主按钮点击事件,已阻止事件冒泡
							 -->

							<!-- 商品展示样式 1 -->
							<goods-item-1 v-if="com == '1'" @jump="jump" @addCart="addCart"
								:item="item"
								:pid="pid">
							</goods-item-1>

							<!-- 商品展示样式 2 -->
							<goods-item-2 v-if="com == '2'" @jump="jump"
								:item="item"
								:pid="pid">
							</goods-item-2>

							<!-- 商品展示样式 3 -->
							<goods-item-3 v-if="com == '3'" @jump="jump" @primary="primary"
								:item="item">
							</goods-item-3>
						</div>
					</div>
				</div>
			</div>
		</div>
    </div>
</template>

<script>
	// 引入组件
	import goodsItem1 from "@/components/goodsItem_1/goodsItem_1";
	import goodsItem2 from "@/components/goodsItem_2/goodsItem_2";
	import goodsItem3 from "@/components/goodsItem_3/goodsItem_3";

    export default {
        data: function () {
            return {
                basicWidth: 100,
				scrollItemBox: 100
            }
        },
		props: ["lists", "com", "pid"],
		components: {
			goodsItem1,
			goodsItem2,
			goodsItem3
        },
		methods: {

			/**
			 * 点击跳转
			 * @param {object} item [数据对象]
			 */
			jump(item) {
				this.$emit("jump", item);
			},

			/**
			 * 加入购物车
			 * @param {object} item [数据对象]
			 */
			addCart(item) {
				this.$emit("addCart", item);
			},

			/**
			 * 主按钮点击事件
			 * @param {object} item [数据对象]
			 */
			primary(item) {
				this.$emit("primary", item);
			},

			/**
			 * 设置横向滚动
			 * @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 + (0 * multiple)) / clientWidth * this.basicWidth);

				// 判断scrollItemBox值,更新this.scrollItemBox宽度
				if (scrollItemBox > this.basicWidth) {
					this.$set(this, "scrollItemBox", scrollItemBox);
				} else {
					this.$set(this, "scrollItemBox", this.basicWidth);
				}
			}
		},
		watch: {

			/**
			 * 监听横向滚动数据加载
			 */
			 lists(newVal, oldVal) {

				// 判断是否有数据
				if (typeof newVal === "object" && newVal.length > 0) {

					// 下一周期处理
					this.$nextTick(function() {

						// 设置横向滚动
						this.setScrollBox(this.$refs.scrollBox);
					});
				}
			 }
		},
		mounted() {

			// 判断如果lists有值,设置横向滚动数据
			if (this.lists && this.lists.length) {

				// 下一周期处理
				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">
    @import './horizList';
</style>