HBuilderX打包web网站之wap2app设置底部菜单tabBar

 上面是真实案例,首页、在看、我的就是我设置的菜单,还可以设置图标,填写图片网络地址就行。

下面是代码,可以直接用:

第一步,先下载2个文件或者复制也行,那就新建吧:

分别新建一个css文件,命名为   __wap2apptabbar.css

 .tab {
     position: absolute;
     left: 0;
     right: 0;
     bottom: 0;
     height: 50px;
 }

 .tab-inner {
     display: table;
     table-layout: fixed;
     background-color: #f7f7f7;
     -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, .85);
     box-shadow: 0 0 1px rgba(0, 0, 0, .85);
     -webkit-backface-visibility: hidden;
     backface-visibility: hidden;
 }

 .tab-item {
     display: table-cell;
     width: 1%;
     height: 50px;
     overflow: hidden;
     color: #666;
     text-align: center;
     text-overflow: ellipsis;
     white-space: nowrap;
     vertical-align: middle;
 }

 .tab-item-icon img {
     width: 100%;
     height: 100%;
 }

 .tab-item .tab-item-icon img:nth-child(1) {
     display: block;
 }

 .tab-item .tab-item-icon img:nth-child(2) {
     display: none;
 }

 .tab-item.active .tab-item-icon img:nth-child(1) {
     display: none;
 }

 .tab-item.active .tab-item-icon img:nth-child(2) {
     display: block;
 }

 .tab-item-label {
     display: block;
     overflow: hidden;
     font-size: 12px;
 }

 .tab-item-icon {
     margin: 0 auto;
     width: 24px;
     height: 24px;
 }

 .tab-item-icon-bars{

 }

 .tab.no-label .tab-item-icon {
     margin: 0 auto;
     width: 40px;
     height: 40px;
 }

 .tab.no-icon .tab-item-label {
     font-size: 16px;
 }

 .tab-item.active {
     color: #FF5777;
 }

第个文件命名为:__wap2apptabbar.js

(function(window, document) {
    var TabBar = function(options) {
        options = options || {};

        this.tabClass = options.tabClass || 'tab'; //容器元素
        this.itemClass = options.itemClass || 'tab-item'; //选项样式名称
        this.selectedClass = options.selectedClass || 'active'; //选项激活样式名称

        this.itemIconClass = options.itemIconClass || 'tab-item-icon'; //选项图标样式名称
        this.itemLabelClass = options.itemLabelClass || 'tab-item-label'; //选项标题样式名称

        this.list = options.list || []; //选项列表

        this.selected = 0;

        if (this.list.length) {
            this.render();
        }

        this.tabElem = document.querySelector('.' + this.tabClass);
        this.itemElems = [].slice.call(document.querySelectorAll('.' + this.itemClass));

        this.handleOldVersion();

        this.initEvent();

    };
    var proto = TabBar.prototype;

    proto.refresh = function() {
        this.itemElems = [].slice.call(document.querySelectorAll('.' + this.itemClass));
    };

    proto.handleOldVersion = function() {
        var list = this.list;
        if (!list.length) {
            for (var i = 0; i < this.itemElems.length; i++) {
                list.push({
                    url: this.itemElems[i].getAttribute('href')
                });
            }
        }
        window.__wap2app_old_tab_item_urls = [];
        for (var i = 0; i < list.length; i++) {
            __wap2app_old_tab_item_urls.push(list[i].url);
        }
    };
    proto.render = function() {
        var tabbarElem = document.createElement('nav');
        tabbarElem.className = this.tabClass;
        if (!this.list[0].iconPath) {
            tabbarElem.className = tabbarElem.className + ' no-icon';
        }
        if (!this.list[0].text) {
            tabbarElem.className = tabbarElem.className + ' no-label';
        }
        var html = [];
        for (var i = 0; i < this.list.length; i++) {
            html.push(this.renderItem(this.list[i], i));
        }
        tabbarElem.innerHTML = '<div class="' + this.tabClass + '-inner">' + html.join('') + '</div>';
        document.body.appendChild(tabbarElem);
    };
    proto.renderItem = function(item, index) {
        var isSelected = this.selected === index;
        var html = ['<div class="' + this.itemClass + (isSelected ? (' ' + this.selectedClass) : '') + '" href="' + item.url + '">'];
        if (item.iconPath) {
            html.push('<div class="' + this.itemIconClass + '"><img src="' + item.iconPath + '"/><img src="' + item.selectedIconPath + '"/></div>');
        }
        if (item.text) {
            html.push('<div class="' + this.itemLabelClass + '">' + item.text + '</div>');
        }
        html.push('</div>');
        return html.join('');
    };
    proto.initEvent = function() {
        if (!this.tabElem) {
            throw new Error('未找到TabBar容器');
        }
        if (!this.itemElems || !this.itemElems.length) {
            throw new Error('TabBar容器内无选项');
        }
        var self = this;
        //全局回调
        window.__wap2app_change_tab_callback = function(e) {
            self.highlight(e.index);
        };

        this.tabElem.addEventListener('click', function(e) {
            var target = e.target;
            for (; target && target !== self.tabElem; target = target.parentNode) {
                var index = self.itemElems.indexOf(target);
                if (~index) {
                    if (index === self.selected) {
                        return;
                    }
                    e.preventDefault();
                    e.stopPropagation();
                    var url = target.getAttribute('href');
                    if (!url) {
                        throw new Error('未指定选项打开的链接地址');
                    }
                    self.highlight(index);
                    var id = plus.runtime.appid;
                    wap2app.switchTab(id, id + '_' + index, url);
                }
            }
        });
    };
    proto.highlight = function(index) {
        this.itemElems[this.selected].classList.remove(this.selectedClass);
        var currentItemElem = this.itemElems[index]
        currentItemElem.classList.add(this.selectedClass);
        if (currentItemElem.scrollIntoView) {
            currentItemElem.scrollIntoView();
        }
        this.selected = index;
    };
    window.TabBar = TabBar;
})(window, document);

第三步,取消注释:

 第四步,在client_index.html设置tabBar代码:

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="viewport-fit=cover,width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<!--使用本地选项卡时,将如下两行代码注释取消-->
		<link rel="stylesheet" type="text/css" href="__wap2apptabbar.css" />
		<script src="__wap2apptabbar.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">  
		    /*自定义选项卡文字颜色示例*/  
		    .tab-item {  
		        color: black;//选项卡文字默认为黑色  
		    }  
		    .tab-item.active {  
		        color: blue;//选项卡文字高亮时为蓝色  
		    }  
		</style>
	</head>
	
	<body>
		
		<script>  
		    new TabBar({  
		        list: [{  
		            url: "https://huamaotianxing.com/",  
		            text: "首页",  
		            iconPath: 'home.png',  
		            selectedIconPath: 'home-selected.png'  
		        }, {  
		            url: "https://huamaotianxing.com/open/course/explore",  
		            text: "在看",  
		            iconPath: 'tab1.png', //本地图标  
		            selectedIconPath: 'tab1-selected.png'  
		        }, {  
		            url: "https://huamaotianxing.com/my/courses/learning",  
		            text: "我的",  
		            iconPath: 'http://m.exampple.com/imgs/about.png',//网络图标  
		            selectedIconPath: 'http://m.exampple.com/imgs/about-selected.png'  
		        }]  
		    });  
		</script>
		
	</body>

</html>

第6步,在sitemap.json文件配置一下:

 

{
    "global": {
        "webviewParameter": {
            "titleNView": {
                "autoBackButton": true,
                "backgroundColor": "#f7f7f7",//导航栏背景色
                "titleColor": "#000000",//标题颜色
                "titleSize": "17px"
            },
            "statusbar": {
                //系统状态栏样式(前景色)
                "style": "dark"
            },
            "appendCss": "",
            "appendJs": ""
        },
        "easyConfig": {}
    },
    "pages": [
        {
            "webviewId": "__W2A__huamaotianxing.com",//首页
            "matchUrls": [
                {
                    "href": "https://huamaotianxing.com"
                }, {
                    "href": "https://huamaotianxing.com/"
                }
            ],
            "webviewParameter": {
                "titleNView": false,
                "statusbar": {
                    //状态条背景色,
                    //首页不使用原生导航条,颜色值建议和global->webviewParameter->titleNView->backgroundColor颜色值保持一致
                    //若首页启用了原生导航条,则建议将首页的statusbar配置为false,这样状态条可以和原生导航条背景色保持一致;
                    "background": "#f7f7f7"
                },
				"tabBar": {//选项卡配置,仅首页支持  
				            "height": "50px",//选项卡高度,默认为50px  
				            "list": [  
				                {  
				                    "url": "https://huamaotianxing.com/" //tab1页面地址  
				                }, {  
				                    "url": "https://huamaotianxing.com/open/course/explore" //tab2页面地址  
				                }, {  
				                    "url": "https://huamaotianxing.com/my/courses/learning"  //tab3页面地址  
				                }  
				            ]  
				        }  
            }
        }
    ]
}

第7步,运行到真机上测试了,ok了。

官方文档说明:

选项卡切换优化 - wap2app教程 - DCloud问答选项卡切换优化 - wap2app教程 - 体验差距相比原生App,M站选项卡切换体验较差,主要体现在:原生App切换选项卡时,选项卡区域不变,仅内容区view变化;但M站选项卡切换时,会将整个页面重新加载,经常出现白屏现象。 优化思路wap2app的优化方案是拆分选项卡区域和内容区,变成两...https://ask.dcloud.net.cn/article/12878

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在uni-app中,你可以通过以下步骤来实现自定义底部tabbar: 1. 在main.js文件中添加以下代码段,以解决点击两次才能选择icon的问题: ``` Vue.mixin({ methods: { setTabBarIndex(index) { if (typeof this.$mp.page.getTabBar === 'function' && this.$mp.page.getTabBar()) { this.$mp.page.getTabBar().setData({ selected: index }) } } } }) ``` 2. 在index.wxss文件中重新定义tabbar的样式,包括背景、高度、字体大小等。以下是一个例子: ```css .tab-bar { position: fixed; bottom: 0; left: 0; right: 0; display: flex; box-shadow: 0px -2px 10px 0px rgba(0,0,0,0.05); box-sizing: content-box; } .tab-bar-item { flex: auto; text-align: center; display: flex; justify-content: center; align-items: center; flex-direction: column; background: #fff; height: 120rpx; } /* 自定义样式 */ .tab-bar-item.diy { margin-top: 0!important; background: transparent; position: relative; flex: inherit; width: 134rpx; } .tab-bar-item image { width: 48rpx; height: 48rpx; overflow: initial; } .tab-bar-item view { font-size: 24rpx; } .tab-bar-item image.diy { position: absolute; width: 134rpx; height: 140rpx; bottom: 25.6%; z-index: 100; } .tab-bar-item view.diy { margin-top: 90rpx; background: #fff; width: 100%; height: 100%; padding-top: 58rpx; z-index: 99; } ``` 通过以上步骤,你可以在uni-app中实现自定义底部tabbar。使用以上的代码段和样式定义,你可以根据需求自定义底部tabbar的样式和交互效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uni-app-tabbar:uni-app底部初步实现(不支持小程序)](https://download.csdn.net/download/weixin_42172572/15923240)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uni-app自定义底部tabbar](https://blog.csdn.net/Janent168/article/details/129809136)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值