位置: 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模型能做什么)

  • 退个税手续费如何申报
  • 登记会计账簿的内容包括
  • 企业购进材料入什么账户
  • 单位全额承担社保相当于领生活费了吗
  • 应付账款现金流量表指定
  • 成本费用总额占营业收入比重
  • 主营业务成本包括职工薪酬吗
  • 展示费是业务宣传费吗
  • 汇算清缴忘记申报了怎么办
  • 印花税 企业
  • 小规模免征增值税政策
  • 应收账款融资的会计如何核算
  • 公司账户转账给个人账户有限额吗?
  • 承兑汇票背书用什么方法?
  • 非居民企业提供劳务企业所得税
  • 应收账款 转让
  • 教培机构房租占比
  • 没有发票的怎么报账
  • 财政拨款结转的借贷方向
  • 应付职工薪酬核算内容不包括
  • 出租房电费怎么结算
  • 如何激活windows11专业版?
  • 企业所得税申报错误怎么更改
  • 增值税发票的作用
  • 一次性开票分期确认收入如何纳税申报
  • 怎么把浏览器屏幕缩小
  • 借别人资质投标如何签合同
  • 收到预付款计入什么科目
  • PHP:pg_send_query_params()的用法_PostgreSQL函数
  • 成本法转为权益法追溯的理解
  • 银行卡账户年费是什么意思 为什么扣我银行卡的钱
  • 华沙的教堂
  • 非城市公交企业管理办法
  • 实收资本明细账模板
  • 递延所得税资产和负债怎么计算
  • 权利许可证照印花税优惠政策
  • 国内旅客运输服务普票可以抵扣吗
  • 华为od测试岗机试需要怎么准备
  • 人类记忆存储在哪
  • mongodb 查询条件
  • php与服务器的关系
  • 职工食堂资金管理办法
  • mysql中join的用法
  • 个体户开发票超过定额是如何交税?
  • 印花税减免优惠政策2019
  • 员工自己全额承担社保费用要计提吗
  • 怎么打印银行对账单
  • 销售应税消费品缴纳的消费税计入
  • 共管账户取钱需要多久到账
  • 房地产储备土地排名
  • 交通运输行业指什么
  • 补贴算报酬吗
  • 存在弃置费用的固定资产例题
  • 事业单位不允许办企业是哪个文件要求的
  • 最新mysql数据库安装步骤
  • win7浏览器升级到ie11
  • windowsxp教程
  • linux的lvm扩容
  • win7电脑磁盘空间不足清理步骤
  • U盘写保护怎么处理
  • linux 使用技巧
  • Windows Server 2012实用技巧集锦
  • win7电脑711
  • 如何找?
  • 下列有关javascript中call和apply
  • java 迭代器 remove
  • JavaScript中创建文件
  • 网页跳转的实现方法
  • linux 批量修改
  • 安卓回调接口怎么写
  • android 5.1 WIFI图标上的感叹号及其解决办法
  • python中lxml模块
  • jQuery插件使用
  • 浙江税务客服咨询电话
  • 江苏省社科院院士名单
  • 贵州省增值税普通发票图片
  • 中行网银查询
  • 公租房要钱吗
  • 北京市东城分局刑侦大队电话
  • 武汉洪山实验中学是初中还是高中
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设