vue二级标题超出时出现滚动,接近隐藏的标题时,自动滚动到可视化界面

本文介绍了一种在APP中优化横向滑动二级标题栏用户体验的方法,通过自定义组件实现点击时自动滚动到相应位置,提升交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做app的伙伴都会知道,好多二级标题横向排列,数量多的话有时候就要做出滑动的处理,但是点击后边超出隐藏的标题时,就需要自己滑动到最后,非常不利于用户体验,所以自己就写了一个小的案例,仅仅师为例以后方便,

<template>
	<div class="content">
		<div class="title" id="title" @scroll="getscroll($event)">
			<span v-for="(item,index) in title" @click="getleft($event,index)" :class="{blue:num==index}">{{item}}</span>
		</div>
	</div>
</template>

<script>
	export default{
		name:"TwoTitle",
		data(){
			return{
				title:["新闻","体育","游戏","娱乐","财经","军事","媒体"],
				num:0,
				scrollLeft:"0"
			}
		},
		mounted() {
			
		},
		methods:{
			getleft(e,num){
				this.num = num // 数组下标 根据数组下标添加class名称
				var width = e.target.clientWidth //获取点击元素本身的宽度
				console.log(this.scrollLeft) 
				var title = document.getElementById("title")
				if (num==3 && this.scrollLeft ==0) { //判断当前位置 当超过一定界限时,滚动条滚动到底部,同时也可以自己设置滚动的距离
					$(title).scrollLeft(width*2) //滚动条执行
				} else if(num <= 2 && this.scrollLeft==150){ 
					$(title).scrollLeft(-width*2)
				}
			},
			getscroll(e){ //给标题的父元素添加滚动事件
				this.scrollLeft = e.target.scrollLeft //记录滚动条滚动的距离
			},
		}
	}
</script>

<style>
	
	.title{  /*超出父元素时出现滚动条*/
		width: 100%;
		height: 45px;
		overflow-x: auto;
		overflow-y:hidden;
		white-space: nowrap;  /*超出父级元素是不换行*/
	}
	
	
	.title span{
		display: inline-block;
		width: 20%;
		font-size: 40px;
		text-align: center;
		line-height: 45px;
	}
	.title::-webkit-scrollbar{
		display: none;
	}
	
	.blue{
		font-size: 45px;
		color: blue;
		border-bottom: 2px solid blue; /* px */
	}
</style>

以上就是整个页面的代码,如果有朋友有更好的案例,看到之后,可以分享,谢谢!!!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值