封装scroll,可以设置样式

组件:

<template>
	<view class="content">
		<view>
			<view class="tab" :style="{ 
				padding: completeTabStyle.padding,
				margin: completeTabStyle.margin,
				backgroundColor: completeTabStyle.bgColor,
				border: completeTabStyle.border,
				borderRadius: completeTabStyle.borderRadius,
				position: completeTabStyle.sticky ? 'sticky' : 'static',
				top: `${completeTabStyle.stickyTop}px`,
				}
				
				">
				<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="120">
					<view id="demo1" class="scroll-view-item_H uni-bg-red" v-for="(item, index) in tabList" :key="index"
						:style="{
							minWidth:`${completeTabStyle.itemMW}rpx` ,
							height:`${completeTabStyle.itemH}rpx`,
							fontSize:completeTabStyle.fontSize,
							fontWeight:completeTabStyle.fontWeight,
							color:completeTabStyle.color,
								padding: completeTabStyle.padding,
					}" @click="scroll(index)">

						<view class="" style="display: flex;justify-content: space-between;align-items: center;">
							<view class="">
								<view v-if="tabIndex == index" :style="{
								fontSize:completeTabStyle.activeFontSize,
								fontWeight:completeTabStyle.activeFontWeight,
								color:completeTabStyle.activeColor}">
									{{ item }}
								</view>
								<view v-else>
									{{ item }}
								</view>
							</view>
							<view v-if="index < tabList.length - 1 && completeTabStyle.itemSplitBar" class="split-line"
								style="position: relative;" :style="{
							          backgroundColor:completeTabStyle.itemSplitBarColor,
							             height:`${completeTabStyle.itemSplitBarH}rpx`,
										 right:`${completeTabStyle.itemSplitBarR}rpx`
							          }">
								<view>
								</view>
							</view>
						
						</view>
						<view v-if="index == tabIndex && completeTabStyle.at"  class="attendant"
						:style="{
						   backgroundColor:completeTabStyle.atBgColor,
						      height:`${completeTabStyle.atH}rpx`,
							  width:`${completeTabStyle.atW}rpx`,
							  borderRadius:completeTabStyle.atBorderRadius,
							  bottom:`${completeTabStyle.atB}rpx` }" >
							<view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		<!-- 	<swiper @change="handle_swiperChange" :current="tabIndex" next-margin="100rpx">
				<swiper-item class="swiper" v-for="(item, index) in tabList" :key="index">
					{{ item }}
				</swiper-item>
			</swiper> -->
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			tabList: {
				type: Array,
				default: () => {
					return []
				}
			},
			tabStyle: {
				type: Object,
				default: () => {
					return {}
				}
			},
		},
		data() {
			return {
				tabIndex: 0,
				tab: ["热搜榜", "视频榜", "博客榜"],
			};
		},
		computed: {
			completeTabStyle() {
				return Object.assign({
						margin: "0px 0px 0px 0px", // tab的外边距
						padding: "0px 0px 0px 0px", // tab的内边距
						bgColor: "#fffff", // tab的背景颜色

						borderRadius: "0px", // tab的外圆角
						border: "none", // tab的外边框
						sticky: false, // 是否吸顶
						stickyTop: 0, // 吸顶距离

						itemMW: 100, // tab块最小宽度 滑动模式 生效
						itemH: 50, //  tab块高度
						fontSize: "16px", // tab块字体大小
						fontWeight: "normal", // tab块字体粗细
						color: "#333333", // tab块字体颜色
						activeColor: "#007aff", // tab块激活颜色
						activeFontSize: "16px", // tab块激活字体大小
						activeFontWeight: "normal", // tab块激活字体粗细
						itemSplitBar: true, // tab块的分割条
						itemSplitBarColor: "#999999", // tab块分割条的颜色
						itemSplitBarH: "auto", // tab块分割条的高度 支持写法 auto|50%|40px
						dot: false, // tab块提示点
						att: false,
						liveLineColor: '#fff',
						at: true, // 使用提示条
						atMode: "center bottom", // 提示条位置模式 组合模式: center [bottom|top]  提供上中 中 下中 三种模式
						atW: "auto", // 提示条的宽度 支持写法 auto|80%|100
						atH: 5, // 提示条的高度
						atBgColor: "#007aff", // 提示条背景颜色
						atBorderRadius: "5px", // 提示条的外圆角
					},
					this.tabStyle
				);
			},

		},
		methods: {
			scroll(i) {
				this.tabIndex = i;
			},
			handle_swiperChange(e) {
				console.log(e);
				this.tabIndex = e.detail.current
			},
		},
	};
</script>

<style scoped lang='less'>
	.tab {
		position: sticky;
		top: 0;
		z-index: 2;
	}

	.swiper {
		position: relative;
		z-index: 1;
		transition: height ease 300ms;
	}

	.swiper-slider {
		padding: 0 0 30rpx;
	}

	.content {
		height: 100vw;

		.scroll-view_H {
			white-space: nowrap;
		}

		.uni-bg-red {
			/* height: 50rpx; */
			display: inline-block;
			/* margin-right: 30rpx; */

		}

		.uni-bg-green {
			height: 50rpx;
			display: inline-block;
			margin-right: 30rpx;
		}

		.uni-bg-blue {
			height: 50rpx;
			display: inline-block;
		}
	}

	.swiper {
		width: 90rpx;
	}

	.active {
		color: red;
	}

	.split-line {
		position: absolute;
		right: 0;
		width: 1px;
	}
	 .attendant {
	
	    transition: top 300ms, left 300ms, width 300ms;
		margin: 0 auto
	  }
</style>

使用:

<template>
	<view class="tab">
		<switchTab :tabList="tabData.tabs" :tabStyle="tabData.style" ref="swiperTabPro"></switchTab>
	</view>
</template>

<script>
	import switchTab from './01.vue'
	export default {
		components: {
			switchTab
		},
		data() {
			return {
				tabIndex: 0,
				tabData: {
					tabs: ["店铺", "产品", "话题", "展会活动"],
					style: {
						margin: "10rpx 50rpx 100rpx 50rpx", // tab的外边距
						padding: "10rpx 10rpx 10rpx 10rpx", // tab的内边距
						bgColor: "#55ffff", // tab的背景颜色
						borderRadius: "20rpx", // tab的外圆角
						border: "none", // tab的外边框,(5px solid #ccc)
						sticky: false, // 是否吸顶true或false
						stickyTop: 0, // 吸顶距离


						itemMW: 20, // tab块最小宽度 滑动模式 生效
						itemH: 50, //  tab块高度
						
						fontSize: "36rpx", // tab块字体大小
						fontWeight: "normal", // tab块字体粗细
						color: "#ff00ff", // tab块字体颜色
						
						activeColor: "#007aff", // tab块激活颜色
						activeFontSize: "46rpx", // tab块激活字体大小
						activeFontWeight: "normal", // tab块激活字体粗细
						
						itemSplitBar: true, // tab块的分割条
						itemSplitBarColor: "#999999", // tab块分割条的颜色
						itemSplitBarH: "40", // tab块分割条的高度 支持写法 40
						itemSplitBarR:'-10',//对应padding

						at: true, // 使用提示条
						atW: 80, // 提示条的宽度 支持写法 100
						atH: 5, // 提示条的高度
						atBgColor: "#ffff7f", // 提示条背景颜色
						atBorderRadius: "5px", // 提示条的外圆角
						
					},
				},
			};
		},

		methods: {

		},
	};
</script>

<style>
</style>

效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
封装一个 Scroll-view 组件,你可以创建一个新的组件文件,例如 "ScrollViewWrapper.vue"。然后在这个文件中,你可以使用 Vue 的单文件组件语法来定义该组件。 在 "ScrollViewWrapper.vue" 文件中,你需要引入 Vue 和 scroll-view 的依赖,并创建一个名为 "ScrollViewWrapper" 的组件。在组件内部,你可以使用 scroll-view 组件来实现滚动功能,并提供一些可调整的属性和事件。 以下是一个简单的例子,演示如何封装一个基本的 scroll-view 组件: ```html <template> <scroll-view :style="scrollStyle" @scroll="handleScroll"> <slot></slot> </scroll-view> </template> <script> import ScrollView from 'scroll-view' export default { name: 'ScrollViewWrapper', components: { ScrollView }, props: { scrollStyle: { type: Object, default: () => ({}) } }, methods: { handleScroll(event) { // 处理滚动事件 console.log(event) this.$emit('scroll', event) } } } </script> <style scoped> /* 样式可以根据需要进行自定义 */ </style> ``` 在上面的例子中,我们引入了 `scroll-view` 组件,并在模板中使用了 `<scroll-view>` 标签。通过 `:style` 属性,我们可以传递一个对象给 `scroll-view` 组件,用于设置滚动视图的样式。我们还通过 `@scroll` 事件监听滚动事件,并将其通过 `$emit` 发送给父组件。 你可以根据实际需求,在示例代码中添加其他属性和方法,来满足你的具体需求。最后,在需要使用 scroll-view 的地方,你可以引入封装好的 `ScrollViewWrapper` 组件,并在其中放置需要滚动的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值