位置: IT常识 - 正文

微信小程序实现滑动/点击切换Tab(微信小程序实现文件上传)

编辑:rootadmin
微信小程序实现滑动/点击切换Tab 背景

推荐整理分享微信小程序实现滑动/点击切换Tab(微信小程序实现文件上传),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:微信小程序实现文件上传,微信小程序实现轮播图,微信小程序实现页面跳转,微信小程序实现轮播效果的组件,微信小程序实现懒加载,微信小程序实现轮播效果的组件,微信小程序实现懒加载,微信小程序实现懒加载,内容如对您有帮助,希望把文章链接给更多的朋友!

👏 swiper+scroll-view实现滑动/点击切换Tab,以及scroll-left的使用~

🥇文末分享源代码。记得点赞+关注+收藏!

1.实现效果微信小程序实现滑动/点击切换Tab(微信小程序实现文件上传)

2.实现步骤2.1 scroll-view实现tab列表

scroll-view: 可滚动视图区域。使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 height。组件属性的长度单位默认为px。 scroll-x(boolean):允许横向滚动 scroll-y(boolean):允许纵向滚动 scroll-left(number/string):设置横向滚动条位置 scroll-with-animation(boolean):在设置滚动条位置时使用动画过渡

定义一个tab列表,scroll-view包裹,允许横向滚动,设置scroll-left默认为0每个tab设置为display: inline-block,scroll-view设置 white-space: nowrap不换行

<scroll-view scroll-x class="container-head-sc" scroll-left="{{sleft}}" scroll-with-animation="true"> <view class="item" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view> </scroll-view>.container-head-sc { height: 50rpx; border-radius: 25rpx; background: #eeece4; color: #333; white-space: nowrap;}.container-head-sc .item { padding: 0 20rpx; min-width: 90rpx; text-align: center; line-height: 50rpx; font-size: 26rpx; display: inline-block; height: 50rpx;}给每个tab设置vertical-align: top;防止高度塌陷.container-head-sc .item{ /* 防止高度塌陷 */ + vertical-align: top;}添加当前激活tab样式,定义当前选中项索引currentTab默认为0(即选中第一个),当currentTab==列表的某一项索引表示选中 <view class="item {{currentTab == index ?'active':''}}" data-current="{{index}}" catchtap="handleTabChange" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view>.container-head-sc .active { color: #ffffff; font-weight: bold; background: orange; border-radius: 25rpx;}添加切换事件 handleTabChange(e) {let { current } = e.target.dataset;if (this.data.currentTab == current || current === undefined) return;this.setData({ currentTab: current,});},2.2 swiper+scroll-iew 实现内容列表

swiper: 滑块视图容器。默认高度为150px; current(number):当前所在滑块的 index,默认为0 autoplay(boolean):是否自动切换 bindchange(eventhandle):current 改变时会触发 change 事件,event.detail = {current, source}

swiper包裹内容列表,需要为swiper指定高度,这里我们设置为撑满一屏

/* swiper默认高度为150px */.container-swiper { height: calc(100% - 110rpx);}设置swiper的current为当前选中的tab标签索引,即currentTab<swiper current="{{currentTab}}" class="container-swiper"> <swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'> </swiper-item></swiper>swiper-item展示内容列表,用scroll-view包裹内容,设置竖向滚动,使用竖向滚动时,需要给scroll-view一个固定高度,这里将scroll-view高度设置为100%,与swiper同高,铺满一屏<swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'> <scroll-view scroll-y class="container-swiper-sc"> <view class="flex-wrap flex-row items"> ....//内容 </view> </scroll-view> </swiper-item>.container-swiper-sc { height: 100%;}swiper添加bindchange事件,当滑动时候,动态的设置currentTab,实现tab列表的同步更新 <swiper current="{{currentTab}}" bindchange="handleSwiperChange" class="container-swiper">....//内容</swiper> handleSwiperChange(e) { this.setData({ currentTab: e.detail.current, }); },可以发现,当swiper所在滑块的 index超出tab列表的可视范围,我们得手动滑动tab列表才能看见当前所选中的tab找到2.1节 scroll-left=“{{sleft}}”,scroll-left用来设置横向滚动条位置,也就是说,我们可以监听swiper的滚动,在滑块所在的index改变的时候,去动态的设置scroll-left的位置scroll-left的计算

wx.createSelectorQuery(): 返回一个 SelectorQuery 对象实例 SelectorQuery.selectAll(string selector): 在当前页面下选择匹配选择器 selector 的所有节点。

getScrollLeft() {const query = wx.createSelectorQuery();query.selectAll(".item").boundingClientRect();//这里将会返回页面中所有class为item的节点,个数为tab列表的长度query.exec((res) => { let num = 0; for (let i = 0; i < this.data.currentTab; i++) { num += res[0][i].width; } // 计算当前currentTab之前的宽度总和 this.setData({ sleft: Math.ceil(num), });});},修改swiper的bindchange事件,每次滑块的变化,都重新计算scroll-left的大小 handleSwiperChange(e) { + this.getScrollLeft(); },3.实现代码<view class="head flex-row"> <view class="head-title">scroll-left</view></view><scroll-view scroll-y class="container"> <view class="container-head flex-row"> <scroll-view scroll-x class="container-head-sc" scroll-left="{{sleft}}" scroll-with-animation="true"> <view class="item {{currentTab == index ?'active':''}}" data-current="{{index}}" catchtap="handleTabChange" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view> </scroll-view> </view> <swiper current="{{currentTab}}" bindchange="handleSwiperChange" class="container-swiper"> <swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'> <scroll-view scroll-y class="container-swiper-sc"> <view class="flex-wrap flex-row items"> <block wx:for="{{item}}" wx:key="index"> <image src="https://www.yuucn.com/wp-content/uploads/2023/04/1682149599-47bec120528c89e.jpg" mode="aspectFill" class="item-img" /> </block> </view> </scroll-view> </swiper-item> </swiper></scroll-view>page { background-color: #ffa500; height: 100%;}.head { height: 90rpx; color: #333; font-size: 30rpx; padding-left: 30rpx; font-weight: bold; padding-bottom: 10rpx; box-sizing: border-box;}.head-title { position: relative; display: inline-block; height: 100%;}.head-title::after { content: ''; position: absolute; z-index: 99; width: 15px; height: 15px; margin-left: -15rpx; border-top: 3px solid #333; border-right: 3px solid #333; border-top-right-radius: 100%; transform: rotate(-225deg); left: 50%; bottom: 3px;}.container { width: 100%; height: calc(100% - 90rpx); background-color: #fff; overflow: hidden; border-radius: 30rpx 30rpx 0 0;}.container-head { width: 100%; height: 110rpx; box-sizing: border-box; padding: 10rpx 20rpx;}.container-head-sc { height: 50rpx; border-radius: 25rpx; background: #eeece4; color: #333; white-space: nowrap;}.container-head-sc .item { padding: 0 20rpx; min-width: 90rpx; text-align: center; line-height: 50rpx; font-size: 26rpx; display: inline-block; /* 引起高度塌陷 */ vertical-align: top; height: 50rpx;}.container-head-sc .active { color: #ffffff; font-weight: bold; background: orange; border-radius: 25rpx;}/* swiper默认高度为150px */.container-swiper { height: calc(100% - 110rpx);}.container-swiper-sc { height: 100%;}.container-swiper-sc .items { padding: 0 2%; width: 100%; box-sizing: border-box;}.container-swiper-sc .items .item-img { width: 30vw; height: 30vw; margin-right: 2.8%; margin-bottom: 10rpx; flex-shrink: 0;}.container-swiper-sc .items .item-img:nth-child(3n+3) { margin-right: 0;}/* 隐藏scroll-view的滚动条 */::-webkit-scrollbar { width: 0; height: 0; color: transparent;}Page({ data: { currentTab: 0, sleft: "", //横向滚动条位置 list: [1, 2, 3, 4, 5, 6, 7, 22, 32],//测试列表 }, handleTabChange(e) { let { current } = e.target.dataset; if (this.data.currentTab == current || current === undefined) return; this.setData({ currentTab: current, }); }, handleSwiperChange(e) { this.setData({ currentTab: e.detail.current, }); this.getScrollLeft(); }, getScrollLeft() { const query = wx.createSelectorQuery(); query.selectAll(".item").boundingClientRect(); query.exec((res) => { let num = 0; for (let i = 0; i < this.data.currentTab; i++) { num += res[0][i].width; } this.setData({ sleft: Math.ceil(num), }); }); },});4.写在最后🍒看完本文如果觉得有用,记得点赞+关注+收藏鸭 🍕更多小程序相关,关注🍥苏苏的bug,🍡苏苏的github,🍪苏苏的码云~
本文链接地址:https://www.jiuchutong.com/zhishi/298790.html 转载请保留说明!

上一篇:用chatgpt写insar地质灾害的论文,重复率只有1.8%,chatgpt4.0写论文不是梦

下一篇:BERT模型基本理念、工作原理、配置讲解(图文解释)(bert模型能做什么)

  • 解说微博营销六大价值(微博营销博主)

    解说微博营销六大价值(微博营销博主)

  • 团圆家乡年圆(团圆家乡年活动)(团圆家乡年什么意思)

    团圆家乡年圆(团圆家乡年活动)(团圆家乡年什么意思)

  • 支付宝店员怎么查看收款记录(支付宝店员怎么查账)

    支付宝店员怎么查看收款记录(支付宝店员怎么查账)

  • oppoa8怎么关闭运行程序(oppo怎么关闭运行程序)

    oppoa8怎么关闭运行程序(oppo怎么关闭运行程序)

  • iphone电池柱状图怎么看(iphone电池柱状图绿色低于最低线)

    iphone电池柱状图怎么看(iphone电池柱状图绿色低于最低线)

  • 拼多多为什么不显示猫(拼多多为什么不能用微信支付)

    拼多多为什么不显示猫(拼多多为什么不能用微信支付)

  • excel 表格如何把筛选后的行进行复制粘贴(excel表格如何把行高调整一致)

    excel 表格如何把筛选后的行进行复制粘贴(excel表格如何把行高调整一致)

  • 电脑QQ群课堂怎么进不了(电脑QQ群课堂怎么改备注)

    电脑QQ群课堂怎么进不了(电脑QQ群课堂怎么改备注)

  • Word如何把字体竖着(word如何把字体变大)

    Word如何把字体竖着(word如何把字体变大)

  • 怎么把视频剪辑到一起(怎么把视频剪辑短一些)

    怎么把视频剪辑到一起(怎么把视频剪辑短一些)

  • 键盘正常但是不能输入(键盘正常但是不能打字,打字会按出快捷)

    键盘正常但是不能输入(键盘正常但是不能打字,打字会按出快捷)

  • 双十一退货没有极速退款了吗(双十一退货没有运费险)

    双十一退货没有极速退款了吗(双十一退货没有运费险)

  • 为什么微信无缘无故自助冻结(为什么微信无缘无故身份过期)

    为什么微信无缘无故自助冻结(为什么微信无缘无故身份过期)

  • phones接口是什么意思(pg接口是什么)

    phones接口是什么意思(pg接口是什么)

  • 快手打开不显示图像怎么办(快手打开不显示在线状态)

    快手打开不显示图像怎么办(快手打开不显示在线状态)

  • 华为p40几月份上市(华为p40多会儿上市的)

    华为p40几月份上市(华为p40多会儿上市的)

  • 怎么发定位给微信好友(怎么发定位给微信好友视频)

    怎么发定位给微信好友(怎么发定位给微信好友视频)

  • opporeno3防水吗(reno3防水吗?)

    opporeno3防水吗(reno3防水吗?)

  • w10怎么切换登录账号(windows10怎么切换用户)

    w10怎么切换登录账号(windows10怎么切换用户)

  • 如何用支付宝坐地铁(如何用支付宝坐北京地铁)

    如何用支付宝坐地铁(如何用支付宝坐北京地铁)

  • 安卓微信聊天文件怎么恢复(安卓微信聊天文件目录)

    安卓微信聊天文件怎么恢复(安卓微信聊天文件目录)

  • 无店铺怎么上美团外卖(没有店铺可以入驻美团吗)

    无店铺怎么上美团外卖(没有店铺可以入驻美团吗)

  • 魅族16sPro怎么隐藏照片(魅族16怎么隐藏应用)

    魅族16sPro怎么隐藏照片(魅族16怎么隐藏应用)

  • iphone11Pro怎么隐藏单个软件(苹果11por手机怎么隐藏应用图标)

    iphone11Pro怎么隐藏单个软件(苹果11por手机怎么隐藏应用图标)

  • 喜马拉雅总是自动停止(喜马拉雅总是自动暂停怎么回事)

    喜马拉雅总是自动停止(喜马拉雅总是自动暂停怎么回事)

  • 出口退税无纸化备案
  • 一般纳税人委托其他单位加工材料收回后直接对外销售的
  • 美国个税计算器2021计算器
  • 当期所得税计入什么科目
  • 计提房产税要含税吗
  • 加工费怎么做账
  • 医院财政资金免企业所得税吗
  • 小微企业增值税起征点是多少
  • 货到发票没到
  • 外商企业所得税可享受什么优惠政策
  • 资质招投标
  • 个人去税务局开劳务票 税点是多少
  • 服务外包合同印花税
  • 发票最大限额999元,超过了怎么办
  • 深圳税种核定需要申报吗
  • 可转债税前收益和税后收益
  • 小规模纳税人需要做账吗
  • 业务宣传费企业所得税扣除标准是多少
  • 哪些税计入原材料费用
  • 发票冲红后多交多少税
  • 公司房产税怎么做分录
  • tp link无线路由器设置
  • 贴现利息会计处理
  • 有奖发票奖金支付
  • 反结账是什么意思怎么取消
  • 将u盘格式化
  • neotrace.exe - neotrace是什么进程 有什么用
  • 小程序码生成器在线制作
  • 企业补充养老保险
  • 代发工资如何合理避税
  • mongodb jpa
  • web转义字符
  • 企业实行股权激励的作用
  • 已抵扣增值税进什么科目
  • 小程序开发公司十大排名
  • 20221年的手抄报
  • verilog hdl中任务可以调用
  • 员工出差过程中意外死亡算工伤吗
  • 全年一次性奖金税率表2023
  • 客户少给了钱怎么要
  • 手写发票可以报维修费吗
  • 金税四期对建筑企业的影响
  • 贴现模式怎么计算利息
  • 小微企业城建税及附加减免优惠
  • 汇票没到期如何兑现
  • 评估费用由谁承担
  • access2016备注型
  • 其他应付款科目贷方登记发生的各种应付
  • 哪种发票可以报税
  • 交易性金融资产的账务处理
  • 存货价值的计算公式
  • 施工企业项目部管理人员对外行为的法律后果由谁承担
  • 建筑业外包工程包括哪些
  • 银行打对公回单
  • 工资的计算方法有几种
  • 会计账簿按外表形式分
  • sqlserver字符串切割
  • windows2008关闭ie增强
  • windows service 2008 r2
  • linux系统的
  • 本地磁盘安装
  • 怎么开aero透明模式
  • win7怎样关闭ie浏览器
  • win102020h2版本
  • windows xp快捷键设置在哪
  • win10系统玩英雄联盟可以吗
  • win10取消uac
  • jquery table加载数据
  • js实现简单的画图功能
  • node用什么写的
  • node的express
  • unity保存项目
  • android中适配器
  • 安卓登录界面布局设计
  • 河北税务怎么查询交了没有
  • 税务局 笔录
  • 车船税多少钱一辆
  • 火药概念股有哪些
  • 苏州市国家税务局稽查局李加云副局长
  • 代扣app有哪些
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设