uni-app开发时自定义导航栏

1. 修改pages.json文件

增加navigationStyle

{
			"path": "pages/index/index",
			"style": {
				"navigationStyle": "custom",
				"navigationBarTitleText": "首页"
			}
		},

2. 下载uni-ui扩展组件库

npm install @dcloudio/uni-ui --save

3. 编写代码

components/CustomNavbar.vue文件

<script>
const { safeAreaInsets } = uni.getSystemInfoSync()
import uniIcons from '@dcloudio/uni-ui/lib/uni-icons/uni-icons.vue';

export default {
  components: {
    uniIcons
  },
  data() {
    return {
        safeAreaInsets,
        searchValue: ''
    }
  },
  methods: {
    handleSearch() {
        console.log(this.searchValue)
    },
    handleScan() {
        uni.scanCode({
            success: (res) => {
                console.log("res", res)
            }
        })
    }
  }
}
</script>

<template>
    <view class="navbar" :style="{paddingTop: safeAreaInsets?.top + 10 + 'px'}">
        <view class="logo">
            <image class="logo-image" src="/static/images/logo.png" />
            <text class="logo-text">新鲜 · 亲民 · 快捷</text>
        </view>
        <view class="search"> 
            <uni-icons color="#fff" type="search" size="16" @click="handleSearch"></uni-icons>
            <input class="search-value" v-model="searchValue" placeholder="搜索商品">
            <uni-icons color="#fff" type="scan" size="16" @click="handleScan"></uni-icons>
        </view>
    </view>

</template>

<style lang="scss">
.navbar {
    background-image: url(@/static/images/navigator_bg.png);
    background-size: cover;
    padding-bottom: 20rpx;

    .logo {
        display: flex;
        align-items: center;
        height: 64rpx;
        padding-left: 30rpx;

        .logo-image {
            width: 166rpx;
            height: 39rpx;
        }
        .logo-text {
            font-size: 26rpx;
            color: #fff;
            padding-left: 20rpx;
            margin-left: 20rpx;
            border-left: 1px solid #fff;
        }
    }

    .search {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 26rpx 0 26rpx;
        height: 64rpx;
        margin: 16rpx 20rpx;
        color: #fff;
        font-size: 28rpx;
        border-radius: 32rpx;
        background-color: rgba(255, 255, 255, 0.5);

        .uni-icons {
            width: 32rpx;
        }
        .search-value {
            flex: 1;
            padding: 0 20rpx;
        }
    }
}
</style>

index.vue文件

<script>
import CustomNavbar from '@/components/CustomNavbar.vue'

export default {
  components: {
    CustomNavbar
  },
  data() {
    return {

    }
  },
  methods: {

  }
}

</script>

<template>
  <view class="viewport">
      <CustomNavbar />
  </view>
</template>

<style lang="scss">
page {
  background: #f7f7f7;
}
</style>

相关推荐

  1. uni-app开发定义导航

    2024-07-16 21:08:04       23 阅读
  2. uni-app定义导航下拉刷新实现

    2024-07-16 21:08:04       33 阅读
  3. Uniapp定义导航

    2024-07-16 21:08:04       54 阅读
  4. uniapp——定义导航的封装

    2024-07-16 21:08:04       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 21:08:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 21:08:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 21:08:04       57 阅读
  4. Python语言-面向对象

    2024-07-16 21:08:04       68 阅读

热门阅读

  1. 新质生产力和新质战斗力如何深度耦合

    2024-07-16 21:08:04       19 阅读
  2. 【Python】Arcpy将excel点生成shp文件

    2024-07-16 21:08:04       20 阅读
  3. Linux批量更改文件后缀名

    2024-07-16 21:08:04       19 阅读
  4. android gradle 开发与应用(一) : Gradle基础

    2024-07-16 21:08:04       17 阅读
  5. Python学习4---迭代器和生成器的区别

    2024-07-16 21:08:04       24 阅读
  6. Linux基本命令(续)

    2024-07-16 21:08:04       21 阅读
  7. HTTPS

    HTTPS

    2024-07-16 21:08:04      19 阅读
  8. Vue3 基础

    2024-07-16 21:08:04       22 阅读
  9. redis-缓存三剑客

    2024-07-16 21:08:04       14 阅读
  10. python爬虫豆瓣电影TOP250

    2024-07-16 21:08:04       19 阅读