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


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

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

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

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

    1. .tab {
    2. position: absolute;
    3. left: 0;
    4. right: 0;
    5. bottom: 0;
    6. height: 50px;
    7. }
    8. .tab-inner {
    9. display: table;
    10. table-layout: fixed;
    11. background-color: #f7f7f7;
    12. -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, .85);
    13. box-shadow: 0 0 1px rgba(0, 0, 0, .85);
    14. -webkit-backface-visibility: hidden;
    15. backface-visibility: hidden;
    16. }
    17. .tab-item {
    18. display: table-cell;
    19. width: 1%;
    20. height: 50px;
    21. overflow: hidden;
    22. color: #666;
    23. text-align: center;
    24. text-overflow: ellipsis;
    25. white-space: nowrap;
    26. vertical-align: middle;
    27. }
    28. .tab-item-icon img {
    29. width: 100%;
    30. height: 100%;
    31. }
    32. .tab-item .tab-item-icon img:nth-child(1) {
    33. display: block;
    34. }
    35. .tab-item .tab-item-icon img:nth-child(2) {
    36. display: none;
    37. }
    38. .tab-item.active .tab-item-icon img:nth-child(1) {
    39. display: none;
    40. }
    41. .tab-item.active .tab-item-icon img:nth-child(2) {
    42. display: block;
    43. }
    44. .tab-item-label {
    45. display: block;
    46. overflow: hidden;
    47. font-size: 12px;
    48. }
    49. .tab-item-icon {
    50. margin: 0 auto;
    51. width: 24px;
    52. height: 24px;
    53. }
    54. .tab-item-icon-bars{
    55. }
    56. .tab.no-label .tab-item-icon {
    57. margin: 0 auto;
    58. width: 40px;
    59. height: 40px;
    60. }
    61. .tab.no-icon .tab-item-label {
    62. font-size: 16px;
    63. }
    64. .tab-item.active {
    65. color: #FF5777;
    66. }

    第个文件命名为:__wap2apptabbar.js

    1. (function(window, document) {
    2. var TabBar = function(options) {
    3. options = options || {};
    4. this.tabClass = options.tabClass || 'tab'; //容器元素
    5. this.itemClass = options.itemClass || 'tab-item'; //选项样式名称
    6. this.selectedClass = options.selectedClass || 'active'; //选项激活样式名称
    7. this.itemIconClass = options.itemIconClass || 'tab-item-icon'; //选项图标样式名称
    8. this.itemLabelClass = options.itemLabelClass || 'tab-item-label'; //选项标题样式名称
    9. this.list = options.list || []; //选项列表
    10. this.selected = 0;
    11. if (this.list.length) {
    12. this.render();
    13. }
    14. this.tabElem = document.querySelector('.' + this.tabClass);
    15. this.itemElems = [].slice.call(document.querySelectorAll('.' + this.itemClass));
    16. this.handleOldVersion();
    17. this.initEvent();
    18. };
    19. var proto = TabBar.prototype;
    20. proto.refresh = function() {
    21. this.itemElems = [].slice.call(document.querySelectorAll('.' + this.itemClass));
    22. };
    23. proto.handleOldVersion = function() {
    24. var list = this.list;
    25. if (!list.length) {
    26. for (var i = 0; i < this.itemElems.length; i++) {
    27. list.push({
    28. url: this.itemElems[i].getAttribute('href')
    29. });
    30. }
    31. }
    32. window.__wap2app_old_tab_item_urls = [];
    33. for (var i = 0; i < list.length; i++) {
    34. __wap2app_old_tab_item_urls.push(list[i].url);
    35. }
    36. };
    37. proto.render = function() {
    38. var tabbarElem = document.createElement('nav');
    39. tabbarElem.className = this.tabClass;
    40. if (!this.list[0].iconPath) {
    41. tabbarElem.className = tabbarElem.className + ' no-icon';
    42. }
    43. if (!this.list[0].text) {
    44. tabbarElem.className = tabbarElem.className + ' no-label';
    45. }
    46. var html = [];
    47. for (var i = 0; i < this.list.length; i++) {
    48. html.push(this.renderItem(this.list[i], i));
    49. }
    50. tabbarElem.innerHTML = '<div class="' + this.tabClass + '-inner">' + html.join('') + '</div>';
    51. document.body.appendChild(tabbarElem);
    52. };
    53. proto.renderItem = function(item, index) {
    54. var isSelected = this.selected === index;
    55. var html = ['<div class="' + this.itemClass + (isSelected ? (' ' + this.selectedClass) : '') + '" href="' + item.url + '">'];
    56. if (item.iconPath) {
    57. html.push('<div class="' + this.itemIconClass + '"><img src="' + item.iconPath + '"/><img src="' + item.selectedIconPath + '"/></div>');
    58. }
    59. if (item.text) {
    60. html.push('<div class="' + this.itemLabelClass + '">' + item.text + '</div>');
    61. }
    62. html.push('</div>');
    63. return html.join('');
    64. };
    65. proto.initEvent = function() {
    66. if (!this.tabElem) {
    67. throw new Error('未找到TabBar容器');
    68. }
    69. if (!this.itemElems || !this.itemElems.length) {
    70. throw new Error('TabBar容器内无选项');
    71. }
    72. var self = this;
    73. //全局回调
    74. window.__wap2app_change_tab_callback = function(e) {
    75. self.highlight(e.index);
    76. };
    77. this.tabElem.addEventListener('click', function(e) {
    78. var target = e.target;
    79. for (; target && target !== self.tabElem; target = target.parentNode) {
    80. var index = self.itemElems.indexOf(target);
    81. if (~index) {
    82. if (index === self.selected) {
    83. return;
    84. }
    85. e.preventDefault();
    86. e.stopPropagation();
    87. var url = target.getAttribute('href');
    88. if (!url) {
    89. throw new Error('未指定选项打开的链接地址');
    90. }
    91. self.highlight(index);
    92. var id = plus.runtime.appid;
    93. wap2app.switchTab(id, id + '_' + index, url);
    94. }
    95. }
    96. });
    97. };
    98. proto.highlight = function(index) {
    99. this.itemElems[this.selected].classList.remove(this.selectedClass);
    100. var currentItemElem = this.itemElems[index]
    101. currentItemElem.classList.add(this.selectedClass);
    102. if (currentItemElem.scrollIntoView) {
    103. currentItemElem.scrollIntoView();
    104. }
    105. this.selected = index;
    106. };
    107. window.TabBar = TabBar;
    108. })(window, document);

    第三步,取消注释:

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

     

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="viewport-fit=cover,width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    6. <title></title>
    7. <!--使用本地选项卡时,将如下两行代码注释取消-->
    8. <link rel="stylesheet" type="text/css" href="__wap2apptabbar.css" />
    9. <script src="__wap2apptabbar.js" type="text/javascript" charset="utf-8"></script>
    10. <style type="text/css">
    11. /*自定义选项卡文字颜色示例*/
    12. .tab-item {
    13. color: black;//选项卡文字默认为黑色
    14. }
    15. .tab-item.active {
    16. color: blue;//选项卡文字高亮时为蓝色
    17. }
    18. </style>
    19. </head>
    20. <body>
    21. <script>
    22. new TabBar({
    23. list: [{
    24. url: "https://huamaotianxing.com/",
    25. text: "首页",
    26. iconPath: 'home.png',
    27. selectedIconPath: 'home-selected.png'
    28. }, {
    29. url: "https://huamaotianxing.com/open/course/explore",
    30. text: "在看",
    31. iconPath: 'tab1.png', //本地图标
    32. selectedIconPath: 'tab1-selected.png'
    33. }, {
    34. url: "https://huamaotianxing.com/my/courses/learning",
    35. text: "我的",
    36. iconPath: 'http://m.exampple.com/imgs/about.png',//网络图标
    37. selectedIconPath: 'http://m.exampple.com/imgs/about-selected.png'
    38. }]
    39. });
    40. </script>
    41. </body>
    42. </html>

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

     

    1. {
    2. "global": {
    3. "webviewParameter": {
    4. "titleNView": {
    5. "autoBackButton": true,
    6. "backgroundColor": "#f7f7f7",//导航栏背景色
    7. "titleColor": "#000000",//标题颜色
    8. "titleSize": "17px"
    9. },
    10. "statusbar": {
    11. //系统状态栏样式(前景色)
    12. "style": "dark"
    13. },
    14. "appendCss": "",
    15. "appendJs": ""
    16. },
    17. "easyConfig": {}
    18. },
    19. "pages": [
    20. {
    21. "webviewId": "__W2A__huamaotianxing.com",//首页
    22. "matchUrls": [
    23. {
    24. "href": "https://huamaotianxing.com"
    25. }, {
    26. "href": "https://huamaotianxing.com/"
    27. }
    28. ],
    29. "webviewParameter": {
    30. "titleNView": false,
    31. "statusbar": {
    32. //状态条背景色,
    33. //首页不使用原生导航条,颜色值建议和global->webviewParameter->titleNView->backgroundColor颜色值保持一致
    34. //若首页启用了原生导航条,则建议将首页的statusbar配置为false,这样状态条可以和原生导航条背景色保持一致;
    35. "background": "#f7f7f7"
    36. },
    37. "tabBar": {//选项卡配置,仅首页支持
    38. "height": "50px",//选项卡高度,默认为50px
    39. "list": [
    40. {
    41. "url": "https://huamaotianxing.com/" //tab1页面地址
    42. }, {
    43. "url": "https://huamaotianxing.com/open/course/explore" //tab2页面地址
    44. }, {
    45. "url": "https://huamaotianxing.com/my/courses/learning" //tab3页面地址
    46. }
    47. ]
    48. }
    49. }
    50. }
    51. ]
    52. }

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

    官方文档说明:

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

  • 相关阅读:
    使用acme.sh配置https证书
    Java中使用 MD5 工具进行对密码进行加密
    【FPGA】Verilog:升降计数器 | 波纹计数器 | 约翰逊计数器 | 实现 4-bit 升降计数器的 UP/DOWN
    设计模式之观察者模式
    微软新服务,允许企业扩大对其威胁情报库的访问权限
    算法~利用zset实现滑动窗口限流
    利用Timer实现窗体淡入淡出的效果
    找不到d3dx9_37.dll,无法继续执行代码
    Ubuntu系统设置
    K3s离线部署
  • 原文地址:https://blog.csdn.net/sinat_25884075/article/details/125634221