網站首頁 編程語言 正文
可以使用 scroll-view 實現類似 tabBar 的效果
? ? 可以使用?scroll-view
?組件來自定義一個可以滾動的 TabBar,將每個 Tab 子頁面放在 scroll-view 內,然后通過?scrollIntoView()
?方法來滾動到對應的 Tab。
具體操作步驟如下:
- 在頁面中添加 scroll-view 組件:
<view class="tab-bar">
<scroll-view class="tab-bar-scrollview" scroll-x="{{scrollX}}" scroll-with-animation="{{true}}">
<view class="tab-bar-item {{currentIndex === index ? 'active' : ''}}" wx:for="{{tabBarList}}" wx:key="{{index}}" data-index="{{index}}" bindtap="onTabBarItemTap">{{item.text}}</view>
</scroll-view>
</view>
其中,tabBarList
?是一個包含所有 TabBar 子頁面信息的數組,scrollX
?控制滾動方向,currentIndex
?記錄當前選中的 Tab 索引。TabBar 的樣式可以根據需求進行自定義。
- 在頁面中定義?
onTabBarItemTap()
?方法,該方法會在 TabBar 子頁面被點擊時被觸發,在方法中實現?scrollIntoView()
?方法來滾動到相應的 Tab 子頁面:
onTabBarItemTap(e) {
const { index } = e.currentTarget.dataset;
this.setData({
currentIndex: index,
});
wx.createSelectorQuery()
.in(this)
.select(`#tab-bar-item-${index}`)
.boundingClientRect((rect) => {
wx.pageScrollTo({
scrollTop: 0,
scrollLeft: rect.left - 50,
duration: 300,
});
})
.exec();
},
其中,wx.createSelectorQuery()
?方法可以創建一個 SelectorQuery 對象,然后通過該對象的?select()
?方法獲取指定的節點,再調用?boundingClientRect()
?方法獲取該節點的位置信息。最后,調用?wx.pageScrollTo()
?方法實現頁面的滾動。在?wx.pageScrollTo()
?方法中,scrollTop
、scrollLeft
?控制滾動位置,duration
?控制滾動時長,可以根據需要進行調整。
原文鏈接:https://blog.csdn.net/smallmww/article/details/132227575
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-04-07 C++11實現字符串分割的示例_C 語言
- 2022-07-23 詳解Nginx的超時keeplive_timeout配置步驟_nginx
- 2023-01-23 redis實現多級緩存同步方案詳解_Redis
- 2023-05-30 docker如何查看容器啟動命令(已運行的容器)_docker
- 2023-01-06 Flutter?Recovering?Stream?Errors小技巧_Android
- 2022-09-18 聊聊C++中右值引用和移動構造函數的使用_C 語言
- 2022-06-22 SQL實現篩選出連續3天登錄用戶與窗口函數的示例代碼_MsSql
- 2023-02-17 react生命周期(類組件/函數組件)操作代碼_React
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支