馒头和小包子 發表於 2019-9-17 01:19:00

uni-app自定义导航栏按钮|uniapp仿微信顶部导航条

<p><span style="font-size: 14px; font-family: &quot;comic sans ms&quot;, sans-serif">最近一直在学习uni-app开发,由于uniapp是基于vue.js技术开发的,只要你熟悉vue,基本上很快就能上手了。</span></p>
<p><span style="background-color: rgba(204, 255, 204, 1); font-size: 14px"><span style="font-family: &quot;comic sans ms&quot;, sans-serif">在开发中发现uni-app原生导航栏也能实现一些顶部自定义按钮+搜索框,只需在page.json里面做一些配置即可。设置app-plus,配置编译到App平台的特定样式。</span><span style="font-family: &quot;comic sans ms&quot;, sans-serif">dcloud平台对app-plus做了详细说明:<span style="color: rgba(0, 0, 255, 1)"><span style="background-color: rgba(204, 255, 204, 1); color: rgba(0, 0, 255, 1)">app-plus配置</span></span>,需注意 目前暂支持H5、App端,不支持小程序。</span></span></p>
<h3><span style="background-color: rgba(255, 255, 0, 1)">最新版uniapp+vue3跨端ai流式输出模板</span></h3>
<p><span style="font-size: 12px">Uniapp-DeepSeek跨三端AI助手|uniapp+vue3+deepseek-v3流式ai聊天模板</span></p>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917001617625-1630644873.png"></p>
<p><span style="font-size: 14px; font-family: &quot;comic sans ms&quot;, sans-serif">在page.json里配置app-plus即可</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
    </span>"path": "pages/ucenter/index"<span style="color: rgba(0, 0, 0, 1)">,
    </span>"style"<span style="color: rgba(0, 0, 0, 1)">: {
      </span>"navigationBarTitleText": "我的"<span style="color: rgba(0, 0, 0, 1)">,
      </span>"app-plus"<span style="color: rgba(0, 0, 0, 1)">: {
            </span>"titleNView"<span style="color: rgba(0, 0, 0, 1)">: {
                </span>"buttons"<span style="color: rgba(0, 0, 0, 1)">: [
                  {
                        </span>"text": "\ue670"<span style="color: rgba(0, 0, 0, 1)">,
                        </span>"fontSrc": "/static/iconfont.ttf"<span style="color: rgba(0, 0, 0, 1)">,
                        </span>"fontSize": "22px"<span style="color: rgba(0, 0, 0, 1)">,
                        </span>"float": "left"<span style="color: rgba(0, 0, 0, 1)">
                  },
                  {
                        </span>"text": "\ue62c"<span style="color: rgba(0, 0, 0, 1)">,
                        </span>"fontSrc": "/static/iconfont.ttf"<span style="color: rgba(0, 0, 0, 1)">,
                        </span>"fontSize": "22px"<span style="color: rgba(0, 0, 0, 1)">

                  }
                ],
                </span>"searchInput"<span style="color: rgba(0, 0, 0, 1)">:{
                  ...
                }
            }
      }
    }
},</span></pre>
</div>
<p><span style="font-size: 14px; font-family: &quot;comic sans ms&quot;, sans-serif">对于如何监听按钮、输入框事件,uni-app给出了相应API,只需把<span style="background-color: rgba(216, 216, 216, 1)">onNavigationBarButtonTap</span>和<span style="background-color: rgba(216, 216, 216, 1)">onNavigationBarSearchInputChanged</span>,写在响应的页面中即可。</span></p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif; font-size: 14px">那如何可以实现像京东、淘宝、微信顶部导航栏,如加入城市定位、搜索、自定图片/图标、圆点提示。。。</span></p>
<p><span style="font-size: 14px; font-family: &quot;comic sans ms&quot;, sans-serif">上面的方法是可以满足一般项目需求,但是在小程序里则失效了,而且一些复杂的导航栏就不能很好兼顾,这时只能寻求其它替代方法了</span></p>
<p><span style="font-size: 14px; font-family: &quot;comic sans ms&quot;, sans-serif">将navigationStyle设为custom或titleNView设为false时,原生导航栏不显示,这时就能自定义导航栏</span></p>
<p><span style="font-size: 12px; font-family: &quot;comic sans ms&quot;, sans-serif; background-color: rgba(216, 216, 216, 1)">"globalStyle": {&nbsp;"navigationStyle": "custom"&nbsp;}</span></p>
<p><span style="font-size: 14px"><strong>下面是简单测试实例:</strong></span></p>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917004112730-1765270245.png"></p>
<p><span style="font-size: 14px"><span style="font-family: &quot;comic sans ms&quot;, sans-serif">这里要注意的是,H5、小程序、App端状态栏都不一样,需要重新计算处理,</span><span style="font-family: &quot;comic sans ms&quot;, sans-serif">我这里已经处理好了,可直接使用,在App.vue里面设置即可</span></span></p>
<div class="cnblogs_code">
<pre>onLaunch: <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
    uni.getSystemInfo({
      success:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e){
            Vue.prototype.statusBar </span>=<span style="color: rgba(0, 0, 0, 1)"> e.statusBarHeight
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifndef MP</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span>(e.platform == 'android'<span style="color: rgba(0, 0, 0, 1)">) {
                Vue.prototype.customBar </span>= e.statusBarHeight + 50<span style="color: rgba(0, 0, 0, 1)">
            }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                Vue.prototype.customBar </span>= e.statusBarHeight + 45<span style="color: rgba(0, 0, 0, 1)">
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span>
            
            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef MP-WEIXIN</span>
            let custom =<span style="color: rgba(0, 0, 0, 1)"> wx.getMenuButtonBoundingClientRect()
            Vue.prototype.customBar </span>= custom.bottom + custom.top -<span style="color: rgba(0, 0, 0, 1)"> e.statusBarHeight
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span>
            
            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef MP-ALIPAY</span>
            Vue.prototype.customBar = e.statusBarHeight +<span style="color: rgba(0, 0, 0, 1)"> e.titleBarHeight
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span>
<span style="color: rgba(0, 0, 0, 1)">      }
    })
},</span></pre>
</div>
<p><span style="font-size: 14px; font-family: &quot;comic sans ms&quot;, sans-serif">啧啧啧,看下面的效果,是不是觉得很眼熟,没错,就是基于uni-app简单的实现了一个仿微信顶部导航条</span></p>
<p><span style="font-size: 14px; font-family: &quot;comic sans ms&quot;, sans-serif">顶部的图标使用iconfont字体图标、另外还可自定传入图片</span></p>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917005434450-548953044.png"></p>
<div class="cnblogs_code">
<pre>&lt;header-bar :isBack="false" title="标题信息" titleTintColor="#fff"&gt;
    &lt;text slot="back" class="uni_btnIco iconfont icon-arrL"&gt;&lt;/text&gt;
    &lt;text slot="iconfont" class="uni_btnIco iconfont icon-search" @tap="aaa"&gt;&lt;/text&gt;
    &lt;text slot="iconfont" class="uni_btnIco iconfont icon-tianjia" @tap="bbb"&gt;&lt;/text&gt;
    &lt;!-- &lt;text slot="string" class="uni_btnString" @tap="ccc"&gt;添加好友&lt;/text&gt; --&gt;
    &lt;image slot="image" class="uni_btnImage" src="../../static/logo.png" mode="widthFix" @tap="ddd"&gt;&lt;/image&gt;
&lt;/header-bar&gt;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917005444239-1496544395.png"></p>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917005452366-1077530407.png"></p>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917005513931-1302339406.png"></p>
<div class="cnblogs_code">
<pre>&lt;header-bar :isBack="true" titleTintColor="#fff" :bgColor="{'background-image': 'linear-gradient(45deg, #007AFF 10%, #005cbf)'}" search&gt;
    &lt;text slot="back" class="uni_btnIco iconfont icon-arrL"&gt;&lt;/text&gt;
    &lt;text slot="iconfont" class="uni_btnIco iconfont icon-choose03" @tap="aaa"&gt;&lt;/text&gt;
    &lt;image slot="image" class="uni_btnImage" src="../../static/logo.png" mode="widthFix" @tap="ddd"&gt;&lt;/image&gt;
&lt;/header-bar&gt;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917005525520-1224259212.png"></p>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917005534739-1407039119.png"></p>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917005542901-1696079199.png"></p>
<div class="cnblogs_code">
<pre>&lt;header-bar :isBack="true" title="我的" titleTintColor="#fff" :bgColor="{background: '#353535'}"&gt;
    &lt;text slot="back" class="uni_btnIco iconfont icon-close"&gt;&lt;/text&gt;
    &lt;text slot="iconfont" class="uni_btnIco iconfont icon-search"&gt;&lt;/text&gt;
    &lt;text slot="string" class="uni_btnString" style="color: #2B9939;"&gt;添加好友&lt;/text&gt;
&lt;/header-bar&gt;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917005552633-251369223.png"></p>
<p>支持传入的属性,另外还用到了vue插槽slot</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">/***<br>  isBack            是否返回按钮
  title               标题
  titleTintColor      标题颜色
  bgColor             背景
  center            标题居中
  search            搜索条
  searchRadius      圆形搜索条
  fixed               是否固定<br>*/</span></pre>
</div>
<div class="cnblogs_code">
<pre>&lt;template&gt;
    &lt;view class="uni_topbar" :style="style"&gt;
      &lt;view class="inner flexbox flex_alignc" :class="" :style="[{'height': customBarH + 'px', 'padding-top': statusBarH + 'px', 'color': titleTintColor}, bgColor]"&gt;
            &lt;!-- 返回 --&gt;
            &lt;!-- &lt;text class="uni_icoBack iconfont icon-arrL" v-<span style="color: rgba(0, 0, 255, 1)">if</span>="isBack" @tap="goBack"&gt;&lt;/text&gt; --&gt;
            &lt;view v-<span style="color: rgba(0, 0, 255, 1)">if</span>="isBack" @tap="goBack"&gt;
                &lt;slot name="back"&gt;&lt;/slot&gt;
            &lt;/view&gt;
            &lt;slot name="headerL"&gt;&lt;/slot&gt;
            &lt;!-- 标题 --&gt;
            &lt;!-- #ifndef MP --&gt;
            &lt;view class="flex1" v-<span style="color: rgba(0, 0, 255, 1)">if</span>="!search &amp;&amp; center"&gt;&lt;/view&gt;
            &lt;!-- #endif --&gt;
            &lt;view class="uni_title flex1" :class="" :style="" v-<span style="color: rgba(0, 0, 255, 1)">if</span>="!search &amp;&amp; title"&gt;<span style="color: rgba(0, 0, 0, 1)">
                {{title}}
            </span>&lt;/view&gt;
            &lt;view class="uni_search flex1" :class="" v-<span style="color: rgba(0, 0, 255, 1)">if</span>="search"&gt; /&gt;
                &lt;input class="uni_searchIpt flex1" type="text" placeholder="搜索" placeholder-style="color: rgba(255,255,255,.5);" /&gt;
            &lt;/view&gt;
            &lt;!-- 右侧 --&gt;
            &lt;view class="uni_headerRight flexbox flex_row flex_alignc"&gt;
                &lt;slot name="iconfont"&gt;&lt;/slot&gt;
                &lt;slot name="string"&gt;&lt;/slot&gt;
                &lt;slot name="image"&gt;&lt;/slot&gt;
            &lt;/view&gt;
      &lt;/view&gt;
    &lt;/view&gt;
&lt;/template&gt;

&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
      data() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
                statusBarH: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.statusBar,
                customBarH: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.customBar
            }
      },
      props: {
            isBack: { type: , </span><span style="color: rgba(0, 0, 255, 1)">default</span>: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)"> },
            title: { type: String, </span><span style="color: rgba(0, 0, 255, 1)">default</span>: ''<span style="color: rgba(0, 0, 0, 1)"> },
            titleTintColor: { type: String, </span><span style="color: rgba(0, 0, 255, 1)">default</span>: '#fff'<span style="color: rgba(0, 0, 0, 1)"> },
            bgColor: Object,
            center: { type: , </span><span style="color: rgba(0, 0, 255, 1)">default</span>: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)"> },
            search: { type: , </span><span style="color: rgba(0, 0, 255, 1)">default</span>: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)"> },
            searchRadius: { type: , </span><span style="color: rgba(0, 0, 255, 1)">default</span>: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)"> },
            fixed: { type: , </span><span style="color: rgba(0, 0, 255, 1)">default</span>: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)"> },
      },
      computed: {
            style() {
                let _style </span>= `height: ${<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.customBarH}px;`
                </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> _style
            }
      },
      methods: {
            goBack() {
                uni.navigateBack()
            }
      }
    }
</span>&lt;/script&gt;</pre>
</div>
<blockquote>
<h4><em>最后附上几个最新uniapp+vue3原创实战项目案例<span style="font-family: &quot;Courier New&quot;; font-size: 12px; white-space-collapse: preserve">😏</span><span style="font-family: &quot;Courier New&quot;; font-size: 12px; white-space-collapse: preserve">😏</span></em></h4>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 12px; white-space: pre-wrap">Uniapp-DeepSeek跨三端AI助手|uniapp+vue3+deepseek-v3流式ai聊天模板</span></p>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 12px; white-space: pre-wrap">uniapp+vue3酒店预订|vite5+uniapp预约订房系统模板(h5+小程序+App端)</span></p>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 12px; white-space: pre-wrap">uniapp-welive仿微信/抖音直播带货|uni-app+vue3+pinia短视频直播商城</span></p>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 12px; white-space: pre-wrap">uniapp+vue3聊天室|uni-app+vite4+uv-ui跨端仿微信app聊天语音/朋友圈</span></p>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 12px; white-space: pre-wrap">uniapp-vue3-oadmin手机后台实例|vite5.x+uniapp多端仿ios管理系统</span></p>
</blockquote>
<p><span style="background-color: rgba(255, 255, 0, 1)"><em><span style="font-family: 宋体, &quot;Songti SC&quot;; white-space: pre-wrap; font-size: 18px">目前自定义弹框+导航条+底部Tabbar组件已经发布到了插件市场,免费下载使用。</span></em></span></p>
<p><span style="font-size: 12px">https://ext.dcloud.net.cn/plugin?id=13799</span></p>
<p><span style="font-size: 12px">https://ext.dcloud.net.cn/plugin?id=5592</span><br><span style="font-size: 12px">https://ext.dcloud.net.cn/plugin?id=5593</span></p>
<p><img src="https://img2018.cnblogs.com/blog/1289798/201909/1289798-20190917011211099-200184283.jpg"></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    本文为博主原创文章,未经博主允许不得转载,欢迎大家一起交流 QQ(282310962) wx(xy190310)<br><br>
来源:https://www.cnblogs.com/xiaoyan2017/p/11531238.html
頁: [1]
查看完整版本: uni-app自定义导航栏按钮|uniapp仿微信顶部导航条