uniapp-自定义导航栏(渐变色+可封装组件通用)

由于普通的导航栏已经满足不了产品的定位,可采用自定义导航栏,封装成通用组件,在其他页面进行使用。

 第一步设置pages.json中pages中的style字段

 "navigationStyle": "custom",

核心代码:
 

获取胶囊的top设置上方导航距离,获取height设置导航的高度。 

uni.getMenuButtonBoundingClientRect()//获取胶囊

原生写法 

<template>
  <view class="tab_set">
    <!-- 设置胶囊顶部 -->
    <view class="MenuButton_top"></view>
    <!-- 实际导航栏 可封装自定义组件  -->
    <view class="back_Tag" v-if="top">{{ str }} </view>
  </view>
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import { ref } from "vue";
// 箭头 由于直接写箭头程序报错 只能赋值变量
const str = ref('<')
// 获取胶囊的距离
const top = ref('')//上方距离
const pageheight = ref('')//整个导航栏高度
const height = ref('')//实际导航栏高度
onLoad(() => {
  const res = uni.getMenuButtonBoundingClientRect()//获取胶囊
  top.value = res.top + 'px'
  pageheight.value = (res.height + 10 + res.top) + 'px'
  height.value = res.height + 'px'
  console.log('获取胶囊位置', height.value);
})
</script>

<style lang="scss" scoped>
// 整个导航栏样式
.tab_set {
  background: linear-gradient(rgb(82, 82, 247), rgb(218, 218, 248));
  height: v-bind('pageheight');

  // 上方距离空隙
  .MenuButton_top {
    height: v-bind('top');
  }

  // 内部导航栏
  .back_Tag {
    height: v-bind('height');
    line-height: v-bind('height');
    font-size: 35px;
    font-weight: 300;
    margin-left: 20px;
  }
}
</style>

加uniapp箭头图标,需要导入ubiapp-icon图

<template>
  <view class="tab_set">
    <!-- 设置胶囊顶部 -->
    <view class="MenuButton_top"></view>
    <!-- 实际导航栏 可封装自定义组件  -->
    <view class="back_Tag" v-if="top">  <uni-icons type="left" size="25"></uni-icons></view>
  </view>
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import { ref } from "vue";
// 箭头 由于直接写箭头程序报错 只能赋值变量
const str = ref('<')
// 获取胶囊的距离
const top = ref('')//上方距离
const pageheight = ref('')//整个导航栏高度
const height = ref('')//实际导航栏高度
onLoad(() => {
  const res = uni.getMenuButtonBoundingClientRect()//获取胶囊
  top.value = res.top + 'px'
  pageheight.value = (res.height + 10 + res.top) + 'px'
  height.value = res.height + 'px'
  console.log('获取胶囊位置', height.value);
})
</script>

<style lang="scss" scoped>
// 整个导航栏样式
.tab_set {
  background: linear-gradient(rgb(82, 82, 247), rgb(218, 218, 248));
  height: v-bind('pageheight');

  // 上方距离空隙
  .MenuButton_top {
    height: v-bind('top');
  }

  // 内部导航栏
  .back_Tag {
    height: v-bind('height');
    line-height: v-bind('height');
    // font-size: 20px;
    font-weight: 700;
    margin-left: 10rpx;
  }
}
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值