【uniapp】小程序滚动tab
745 字
4 分钟
【uniapp】小程序滚动tab
文字版
效果图
源码
<template> <view class="content"> <view class="scrollBox"> <scroll-view class="scroll" scroll-x='true' scroll-with-animation :scroll-left='scrollLeft'> <view class="tabBox" v-for="(item,index) in tabsList" :key="index" :class="[{ active: currentIndex === index }, 'scroll-item']" @click="changeTab(index)"> <view class="nanum" :class="currentIndex === index?'active':''"> {{index+1}} </view> </view> </scroll-view> </view> </view></template>
<script> export default { data() { return { scrollWidth: 0, tabsList: [{ id: 1, name: 'tab1', }, { id: 2, name: 'tab2', }, { id: 3, name: 'tab3', }, { id: 4, name: 'tab4', }, { id: 5, name: 'tab5', }, { id: 6, name: 'tab6', }, { id: 7, name: 'tab7', }, { id: 8, name: 'tab8', }, { id: 9, name: 'tab9', } ], currentIndex: 0, scrollLeft: 0, } }, onLoad() { // 获取元素宽度 const query = uni.createSelectorQuery().select('.scroll'); query.boundingClientRect(res => { this.scrollWidth = res.width; }).exec() },
methods: { // 切换Tab changeTab(index) { console.log(index, "文字tab"); this.currentIndex = index; this.moveTo(index) }, // 当前点击的元素移动到屏幕中间 moveTo(index) { const query = uni.createSelectorQuery().in(this) query.selectAll('.scroll-item').boundingClientRect(rect => { const windowWidth = this.scrollWidth; // 获取到的屏幕宽度 let width = 0 // 计算当前点击的标签项距离左侧的距离 for (let i = 0; i < index; i++) { width += rect[i].width } // 当大于屏幕一半的宽度则滚动,否则就设置左侧位置为0 if (width > windowWidth / 2) { this.scrollLeft = width + rect[index].width / 2 - windowWidth / 2 } else { this.scrollLeft = 0 } }).exec() },
} }</script>
<style scoped> .scrollBox .tabBox { width: 150rpx; height: 100%; margin-right: 10rpx; padding: 4rpx; box-sizing: border-box; background-color: #fff; margin-top: 40rpx; }
.scrollBox .tabBox .nanum { width: 100%; text-align: center; font-size: 24rpx; position: absolute; bottom: 0; left: 0; height: 60rpx; line-height: 60rpx; background: #fff; }
.scroll { width: 100%; white-space: nowrap; height: 100%; background-color: #eee; }
.scroll view { display: inline-block; font-size: 30rpx; color: #000; position: relative; }
.scroll view.active { background-color: #000000 !important; color: #fff !important; }</style>图片版
效果图
源码
<template> <view class="content"> <view class="scrollBox"> <scroll-view class="scroll" scroll-x='true' scroll-with-animation :scroll-left='scrollLeft'> <view class="imagbx" v-for="(item,index) in tabsList" :key="index" :class="[{ active: currentIndex === index }, 'scroll-item']" @click="changeTab(index)"> <image :src="item.img" mode="widthFix"></image> <view class="nanum" :class="currentIndex === index?'active':''"> {{index+1}} </view> </view> </scroll-view> </view> </view></template>
<script> export default { data() { return { scrollWidth: 0, tabsList: [{ id: 1, name: 'img1', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' }, { id: 2, name: 'img2', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' }, { id: 3, name: 'img3', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' }, { id: 4, name: 'img4', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' }, { id: 5, name: 'img5', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' }, { id: 6, name: 'img6', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' }, { id: 7, name: 'img7', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' }, { id: 8, name: 'img8', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' }, { id: 9, name: 'img9', img: 'https://img.pconline.com.cn/images/product/1144409/201812/10/15444358345118610_800.png' } ], currentIndex: 0, scrollLeft: 0, } }, onLoad() { // 获取元素宽度 const query = uni.createSelectorQuery().select('.scroll'); query.boundingClientRect(res => { this.scrollWidth = res.width; }).exec() },
methods: { // 点击切换Tab changeTab(index) { console.log(index, "图片tab"); this.currentIndex = index; this.moveTo(index) }, // 点击元素移动到中间 moveTo(index) { const query = uni.createSelectorQuery().in(this) query.selectAll(`.scroll-item`).boundingClientRect(rect => { const windowWidth = this.scrollWidth; // 屏幕宽度 let width = 0 // 计算当前点击的标签项距离左侧的距离 for (let i = 0; i < index; i++) { width += rect[i].width } // 当大于屏幕一半的宽度则滚动,否则就设置左侧位置为0 if (width > windowWidth / 2) { this.scrollLeft = width + rect[index].width / 2 - windowWidth / 2 } else { this.scrollLeft = 0 } }).exec() },
} }</script>
<style scoped> .scrollBox .imagbx { width: 150rpx; height: 100%; margin-right: 10rpx; padding: 4rpx; box-sizing: border-box; background-color: #fff; }
.scrollBox .imagbx image { height: 160rpx; width: 100%; margin-bottom: 44rpx; }
.scrollBox .imagbx .nanum { width: 100%; text-align: center; font-size: 24rpx; position: absolute; bottom: 0; left: 0; height: 40rpx; background: #fff; }
.scroll { width: 100%; white-space: nowrap; height: 100%; background-color: #eee; }
.scroll view { display: inline-block; font-size: 30upx; color: #000; position: relative; }
.scroll view.active { background-color: #000000 !important; color: #fff !important; }</style>支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
相关文章智能推荐
1
【uniapp】个人中心页面
前端本文档详细介绍了如何使用 UniApp 开发一个个人中心页面。个人中心页面是移动应用中常见的功能模块,通常包含用户的头像、昵称、简介以及各种功能列表。本文将通过三个不同风格的设计示例,展示如何实现这些功能。
2
分享一个非常好用的loading效果网站
前端本文介绍了一个非常实用的加载效果网站,适合前端开发者寻找和使用各种加载动画。以下是文章的详细内容
3
PC端登录页源码(一键复制)
前端登录页面是每一个项目不可缺少的页面,是一个应用的门面,首次进入看见的可能就是登录界面,做了这么久的前端,发现登录界面其实都大同小异,有时候没必要从头写,我写了几个自认为比较好看的uniapp登录界面记录一下,下次用到的话,可以直接复制过来,修改下就行了.只有静态页面,并未做逻辑上的东西,这样便于在此基础上做调整。
4
【前端】高颜值登录页面(一键复制)
前端登录页面是每一个项目不可缺少的页面,是一个应用的门面,首次进入看见的可能就是登录界面,做了这么久的前端,发现登录界面其实都大同小异,有时候没必要从头写,我写了几个自认为比较好看的uniapp登录界面记录一下,下次用到的话,可以直接复制过来,修改下就行了.只有静态页面,并未做逻辑上的东西,这样便于在此基础上做调整。
5
Linux 磁盘空间检查与新硬盘挂载完整指南
Linux本文详细介绍了在 Linux(银河麒麟 V10)环境下挂载新硬盘前的磁盘空间检查方法,以及完整的硬盘分区、格式化、挂载、开机自动挂载配置流程,同时解决了普通用户无 root 权限操作挂载目录的常见问题。
随机文章随机推荐
小王的博客














