经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
uni-app自定义导航栏按钮|uniapp仿微信顶部导航条
来源:cnblogs  作者:xiaoyan2017  时间:2019/9/17 10:33:12  对本文有异议

最近一直在学习uni-app开发,由于uniapp是基于vue.js技术开发的,只要你熟悉vue,基本上很快就能上手了。

在开发中发现uni-app原生导航栏也能实现一些顶部自定义按钮+搜索框,只需在page.json里面做一些配置即可。设置app-plus,配置编译到App平台的特定样式。dcloud平台对app-plus做了详细说明:app-plus配置,需注意 目前暂支持H5、App端,不支持小程序。

在page.json里配置app-plus即可

  1. {
  2. "path": "pages/ucenter/index",
  3. "style": {
  4. "navigationBarTitleText": "我的",
  5. "app-plus": {
  6. "titleNView": {
  7. "buttons": [
  8. {
  9. "text": "\ue670",
  10. "fontSrc": "/static/iconfont.ttf",
  11. "fontSize": "22px",
  12. "float": "left"
  13. },
  14. {
  15. "text": "\ue62c",
  16. "fontSrc": "/static/iconfont.ttf",
  17. "fontSize": "22px"
  18. }
  19. ],
  20. "searchInput":{
  21. ...
  22. }
  23. }
  24. }
  25. }
  26. },

对于如何监听按钮、输入框事件,uni-app给出了相应API,只需把onNavigationBarButtonTaponNavigationBarSearchInputChanged,写在响应的页面中即可。

 

那如何可以实现像京东、淘宝、微信顶部导航栏,如加入城市定位、搜索、自定图片/图标、圆点提示。。。

上面的方法是可以满足一般项目需求,但是在小程序里则失效了,而且一些复杂的导航栏就不能很好兼顾,这时只能寻求其它替代方法了

将navigationStyle设为custom或titleNView设为false时,原生导航栏不显示,这时就能自定义导航栏

"globalStyle": { "navigationStyle": "custom" }

下面是简单测试实例:

这里要注意的是,H5、小程序、App端状态栏都不一样,需要重新计算处理,我这里已经处理好了,可直接使用,在App.vue里面设置即可

  1. onLaunch: function() {
  2. uni.getSystemInfo({
  3. success:function(e){
  4. Vue.prototype.statusBar = e.statusBarHeight
  5. // #ifndef MP
  6. if(e.platform == 'android') {
  7. Vue.prototype.customBar = e.statusBarHeight + 50
  8. }else {
  9. Vue.prototype.customBar = e.statusBarHeight + 45
  10. }
  11. // #endif
  12. // #ifdef MP-WEIXIN
  13. let custom = wx.getMenuButtonBoundingClientRect()
  14. Vue.prototype.customBar = custom.bottom + custom.top - e.statusBarHeight
  15. // #endif
  16. // #ifdef MP-ALIPAY
  17. Vue.prototype.customBar = e.statusBarHeight + e.titleBarHeight
  18. // #endif
  19. }
  20. })
  21. },

啧啧啧,看下面的效果,是不是觉得很眼熟,没错,就是基于uni-app简单的实现了一个仿微信顶部导航条

顶部的图标使用iconfont字体图标、另外还可自定传入图片

  1. <header-bar :isBack="false" title="标题信息" titleTintColor="#fff">
  2. <text slot="back" class="uni_btnIco iconfont icon-arrL"></text>
  3. <text slot="iconfont" class="uni_btnIco iconfont icon-search" @tap="aaa"></text>
  4. <text slot="iconfont" class="uni_btnIco iconfont icon-tianjia" @tap="bbb"></text>
  5. <!-- <text slot="string" class="uni_btnString" @tap="ccc">添加好友</text> -->
  6. <image slot="image" class="uni_btnImage" src="../../static/logo.png" mode="widthFix" @tap="ddd"></image>
  7. </header-bar>

  1. <header-bar :isBack="true" titleTintColor="#fff" :bgColor="{'background-image': 'linear-gradient(45deg, #007AFF 10%, #005cbf)'}" search>
  2. <text slot="back" class="uni_btnIco iconfont icon-arrL"></text>
  3. <text slot="iconfont" class="uni_btnIco iconfont icon-choose03" @tap="aaa"></text>
  4. <image slot="image" class="uni_btnImage" src="../../static/logo.png" mode="widthFix" @tap="ddd"></image>
  5. </header-bar>

  1. <header-bar :isBack="true" title="我的" titleTintColor="#fff" :bgColor="{background: '#353535'}">
  2. <text slot="back" class="uni_btnIco iconfont icon-close"></text>
  3. <text slot="iconfont" class="uni_btnIco iconfont icon-search"></text>
  4. <text slot="string" class="uni_btnString" style="color: #2B9939;">添加好友</text>
  5. </header-bar>

支持传入的属性,另外还用到了vue插槽slot

  1. /***
      isBack 是否返回按钮
  2.   title 标题
  3.   titleTintColor 标题颜色
  4.   bgColor 背景
  5.   center 标题居中
  6.   search 搜索条
  7.   searchRadius 圆形搜索条
  8.   fixed 是否固定
    */
  1. <template>
  2. <view class="uni_topbar" :style="style">
  3. <view class="inner flexbox flex_alignc" :class="[fixed ? 'fixed' : '']" :style="[{'height': customBarH + 'px', 'padding-top': statusBarH + 'px', 'color': titleTintColor}, bgColor]">
  4. <!-- 返回 -->
  5. <!-- <text class="uni_icoBack iconfont icon-arrL" v-if="isBack" @tap="goBack"></text> -->
  6. <view v-if="isBack" @tap="goBack">
  7. <slot name="back"></slot>
  8. </view>
  9. <slot name="headerL"></slot>
  10. <!-- 标题 -->
  11. <!-- #ifndef MP -->
  12. <view class="flex1" v-if="!search && center"></view>
  13. <!-- #endif -->
  14. <view class="uni_title flex1" :class="[center ? 'uni_titleCenter' : '']" :style="[isBack ? {'font-size': '32upx', 'padding-left': '0'} : '']" v-if="!search && title">
  15. {{title}}
  16. </view>
  17. <view class="uni_search flex1" :class="[searchRadius ? 'uni_searchRadius' : '']" v-if="search"> />
  18. <input class="uni_searchIpt flex1" type="text" placeholder="搜索" placeholder-style="color: rgba(255,255,255,.5);" />
  19. </view>
  20. <!-- 右侧 -->
  21. <view class="uni_headerRight flexbox flex_row flex_alignc">
  22. <slot name="iconfont"></slot>
  23. <slot name="string"></slot>
  24. <slot name="image"></slot>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29.  
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. statusBarH: this.statusBar,
  35. customBarH: this.customBar
  36. }
  37. },
  38. props: {
  39. isBack: { type: [Boolean, String], default: true },
  40. title: { type: String, default: '' },
  41. titleTintColor: { type: String, default: '#fff' },
  42. bgColor: Object,
  43. center: { type: [Boolean, String], default: false },
  44. search: { type: [Boolean, String], default: false },
  45. searchRadius: { type: [Boolean, String], default: false },
  46. fixed: { type: [Boolean, String], default: false },
  47. },
  48. computed: {
  49. style() {
  50. let _style = `height: ${this.customBarH}px;`
  51. return _style
  52. }
  53. },
  54. methods: {
  55. goBack() {
  56. uni.navigateBack()
  57. }
  58. }
  59. }
  60. </script>

最后附上一个基于ReactNative实现的自定义导航条的聊天室项目

https://www.cnblogs.com/xiaoyan2017/p/11441285.html

另外后续准备使用uni-app技术也做一个实战聊天项目,届时也会分享出来。????

 

原文链接:http://www.cnblogs.com/xiaoyan2017/p/11531238.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号