赞
踩
与(原):微信小程序——自定义底部tabBar_custom-tab-bar-CSDN博客 略有不同
解决微信小程序自定义tarbar - 点击两次图标才能选中
- "tabBar":{
- "custom": true,
- "list": [{
- "pagePath": "pages/index/index",
- "iconPath": "/images/ico_home_tab_juchang_nor 2@2x.png",
- "selectedIconPath": "/images/ico_home_tab_juchang_pre 2@2x.png",
- "text": "首页"
- }, {
- "pagePath": "pages/commdt/index",
- "iconPath": "/images/ico_home_tab_juchang_nor 3@2x.png",
- "selectedIconPath": "/images/ico_home_tab_juchang_pre 3@2x.png",
- "text": "视频"
- },
- {
- "pagePath": "pages/three/three",
- "iconPath": "/images/ico_home_tab_juchang_nor@2x.png",
- "selectedIconPath": "/images/ico_home_tab_juchang_pre@2x.png",
- "text": "充值"
- }]
- },
- "usingComponents": {
- "custom-tab-bar":"/custom-tab-bar/custom-tab-bar"
- },

- // custom-tab-bar/custom-tab-bar.js
- Component({
- // 1.不同部分定义 properties
- properties:{
- selected:{
- type:Number,
- value:null
- }
- },
- data: {
- color: "#7A7E83",
- selectedColor: "#3cc51f",
- list: [{
- pagePath: "/pages/index/index",
- iconPath: "/images/ico_home_tab_juchang_nor 2@2x.png",
- selectedIconPath: "/images/ico_home_tab_juchang_pre 2@2x.png",
- text: "首页"
- }, {
- pagePath: "/pages/commdt/index",
- iconPath: "/images/ico_home_tab_juchang_nor 3@2x.png",
- selectedIconPath: "/images/ico_home_tab_juchang_pre 3@2x.png",
- text: "视频"
- },
- {
- pagePath: "/pages/three/three",
- iconPath: "/images/ico_home_tab_juchang_nor@2x.png",
- selectedIconPath: "/images/ico_home_tab_juchang_pre@2x.png",
- text: "充值"
- }]
- },
- attached() {
- },
- methods: {
- switchTab(e) {
- console.log("执行跳转",e);
- const data = e.currentTarget.dataset
- const url = data.path
- wx.switchTab({url})
- this.setData({
- // 2.不同部分使用 properties 中定义数据
- selected: this.properties.selected
- })
- }
- }
- })

- <!--custom-tab-bar/custom-tab-bar.wxml-->
- <cover-view class="tab-bar">
- <cover-view class="tab-bar-border"></cover-view>
- <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
- <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
- <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
- </cover-view>
- </cover-view>
- /* custom-tab-bar/custom-tab-bar.wxss */
- .tab-bar {
- position: fixed;
- bottom: 0;
- left: 0;
- right: 0;
- height: 48px;
- background: white;
- display: flex;
- padding-bottom: env(safe-area-inset-bottom);
- }
-
- .tab-bar-border {
- background-color: rgba(0, 0, 0, 0.33);
- position: absolute;
- left: 0;
- top: 0;
- width: 100%;
- height: 1px;
- transform: scaleY(0.5);
- }
-
- .tab-bar-item {
- flex: 1;
- text-align: center;
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- }
-
- .tab-bar-item cover-image {
- width: 27px;
- height: 27px;
- }
-
- .tab-bar-item cover-view {
- font-size: 10px;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。