横向菜单滚动vue3

直接新建一个页面,然后根据自己的需求该样式就行

<template>
	<div class="navbar-list">
		<div class="flex-imd" id="flexImd">
			<div
				v-for="(item, index) in courseTypeList"
				:key="index"
				class="navbar-item"
				:class="currNavbar === index ? 'act' : 'nol'"
				@click="chooseNav(index)"
			>
				<img :src="item.typeLogo" alt="" class="navbar-item-img" style="pointer-events: none" />
				{{ item.typeName }}
			</div>
		</div>
	</div>
</template>
 
<script setup>
const currNavbar = ref(0)
const courseTypeList = ref([
	{ typeName: '测试标题001', typeLogo: 'imgurl' },
	{ typeName: '测试标题001', typeLogo: 'imgurl' },
	{ typeName: '测试标题001', typeLogo: 'imgurl' },
	{ typeName: '测试标题001', typeLogo: 'imgurl' },
	{ typeName: '测试标题001', typeLogo: 'imgurl' },
	{ typeName: '测试标题001', typeLogo: 'imgurl' },
	{ typeName: '测试标题001', typeLogo: 'imgurl' }
])
const chooseNav = (index) => {
	currNavbar.value = index
	const item = document.querySelectorAll('.navbar-item')[index]
	const container = document.querySelector('#flexImd')
	const scrollLeft = item.offsetLeft - (container.offsetWidth - item.offsetWidth) / 2
	container.scrollTo({
		left: scrollLeft,
		behavior: 'smooth'
	})
}
</script>
 
<style>
.flex-imd {
	overflow: hidden;
	display: flex;
	overflow-x: scroll;
	padding: 0;
}
.flex-imd::-webkit-scrollbar {
	display: none;
}
.navbar-list {
	/* width: 50%; */
	padding: 10px;
}
.navbar-item {
	width: max-content;
	white-space: nowrap;
	padding: 0px 18px;
	margin: 0px 5px;
	box-sizing: border-box;
	height: 30px;
	display: flex;
	border-radius: 15px;
	color: white;
	font-weight: 900;
	font-size: 12px;
	flex-flow: column nowrap;
	justify-content: space-around;
	align-items: center;
	cursor: pointer;
	display: flex;
	flex-direction: row;
	align-items: center;
}
.navbar-item-img {
	width: 20px;
	height: 20px;
	margin-right: 5px;
}
.navbar-list::-webkit-scrollbar {
	display: none;
}
.navbar-item.act {
	/* background: #c30f20; */
}
.nol {
	background: #999999;
}
</style>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Vue 3中的横向无缝滚动,你可以使用CSS和Vue的动态绑定来实现。下面是一个简单的示例: 首先,在你的Vue组件中,添加一个包含要滚动的内容的div元素: ```html <template> <div class="scroll-container"> <div class="scroll-content"> <!-- 这里添加你要滚动的内容 --> </div> </div> </template> ``` 接下来,在CSS中设置滚动容器和内容的样式: ```css .scroll-container { overflow: hidden; /* 隐藏超出容器范围的内容 */ white-space: nowrap; /* 防止内容换行 */ } .scroll-content { display: inline-block; /* 内容横向排列 */ animation: scroll 10s linear infinite; /* 使用动画实现滚动效果,10s是滚动一次的时间,可以根据需要调整 */ } @keyframes scroll { 0% { transform: translateX(0); } /* 初始位置 */ 100% { transform: translateX(-100%); } /* 滚动到最右边时的位置 */ } ``` 最后,在Vue组件的`mounted`生命周期钩子中,通过JavaScript动态计算并设置滚动内容的宽度: ```javascript <template> <!-- ... --> </template> <script> export default { mounted() { const scrollContent = document.querySelector('.scroll-content'); const scrollContainer = document.querySelector('.scroll-container'); scrollContent.style.width = scrollContent.scrollWidth - scrollContainer.offsetWidth + 'px'; } } </script> ``` 这样就实现了一个简单的横向无缝滚动效果。你可以根据需要自定义样式和动画,以适应你的项目需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值