<template>
	<view class="scroll1-box">
		<scroll-view scroll-x="true" @scroll="scroll" class="scroll-view" :bounces="false" enhanced="true" scroll-anchoring="true">
			<view class="tabs-scroll-box u-flex u-flex-wrap" :style="{width:scrollWidth * 25 + '%'}">
				<view class="tabs-item u-flex-col u-col-center" :style="{width:100 / scrollWidth + '%'}" v-for="(item,index) in list" :key="index" @click="tourl(item)">
					<image :src="item.logo" mode=""></image>
					<text>{{item.title}}</text>
				</view>
			</view>
		</scroll-view>
		<view class="hidescroll"></view>
		<view class="scroll-box">
			<view class="scroll" :style="{transform: `translateX(${sX})`,width:s_b_W}"></view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "rt-scroll",
		props: {
			list: {
				type: Array,
				default: () => {
					return []
				}
			},
			userinfo: {
				type: Object,
				default: () => {
					return {}
				}
			}
		},
		data() {
			return {
				sX: '0%', //等比偏移量
				c_W: 0, //最外围容器宽度
				c_b_W: 0, //最外围容器内容宽度
				s_b_W: '0px', //自定义滚动条移动盒子宽度
				tablist: [],
				scrollWidth: 0
			};
		},
		watch: {
			list: {
				handler() {
					this.getwidth()
				},
				deep: true
			}
		},
		created() {
			this.getwidth()
		},
		methods: {
			getwidth() {
				this.scrollWidth = Math.ceil(this.list.length / 2)
				this.$nextTick(() => {
					this.getdocument()
				})
				// console.log(this.scrollWidth);
			},
			tourl(item) {
				this.$emit('click', item)
			},
			scroll(e) {
				if ((this.c_b_W - this.c_W) < 0) {
					return
				}
				let nX = e.detail.scrollLeft // 得到滑动的偏移量
				var zong = this.c_b_W - this.c_W //总的偏移量
				this.sX = nX / zong * 100 + '%'
			},
			getdocument() {
				const query = uni.createSelectorQuery().in(this);
				query.selectAll('.scroll-view,.tabs-scroll-box').boundingClientRect((data) => {
					// dome元素从外到内依次获取数据，所以写法按顺序来才不会混
					this.c_W = data[0].width
					this.c_b_W = data[1].width
					this.s_b_W = (this.c_b_W - this.c_W) <= 0 ? "100%" : "15px"
					console.log(this.c_W, this.c_b_W);
				}).exec();
			}
		},
	}
</script>

<style lang="scss">
	.scroll1-box {
		padding-top: 2rpx;
		width: 702rpx;
		// height: 404rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding-bottom: 28rpx;
		position: relative;

		.hidescroll {
			position: absolute;
			width: 702rpx;
			height: 20rpx;
			background-color: #fff;
			left: 0;
			bottom: 38rpx;
			z-index: 10;
		}

		.scroll-view {
			padding-top: 12rpx;


			.tabs-scroll-box {
				padding: 0 4rpx;

				.tabs-item {
					width: 25%;
					margin: 20rpx 0 12rpx 0;

					image {
						width: 84rpx;
						height: 84rpx;
						margin-bottom: 20rpx;
						border-radius: 20rpx;
					}

					text {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #131415;
					}
				}
			}
		}

		.scroll-box {
			width: 30px;
			height: 10rpx;
			background: rgba(30, 125, 255, 0.2);
			border-radius: 5rpx;
			margin: 10rpx auto 0 auto;

			.scroll {
				height: 100%;
				background: #1E7DFF;
				border-radius: 5rpx;
			}
		}
	}
</style>