小程序自带的关闭按钮距离顶部的距离(设置按钮与其对齐)

21 篇文章 0 订阅
1 篇文章 0 订阅

1.情景

很多时候,需要使轮播在顶部,并且占有更多的视图区,就需要去掉导航栏,在轮播上加一个浮动的返回按钮,为了好看,就需要使得自定义按钮跟自带的关闭按钮对齐

2.官方api

Object wx.getMenuButtonBoundingClientRect()
基础库 2.1.0 开始支持,低版本需做兼容处理。

获取菜单按钮(右上角胶囊按钮)的布局位置信息。坐标信息以屏幕左上角为原点。

返回值
Object
菜单按钮的布局位置信息

属性 类型 说明
width number 宽度,单位:px
height number 高度,单位:px
top number 上边界坐标,单位:px
right number 右边界坐标,单位:px
bottom number 下边界坐标,单位:px
left number 左边界坐标,单位:px

3.方案

给按钮设置一个动态的top值,值和获取的关闭按钮的top一致,注意在onShow钩子中获取和设置。

<template>
  <view class="listAwards-areas">
    <view>
      <u-image mode="widthFix" :src="srcImg" :border-radius="10"></u-image>
      <img
        @click="routerTohome" :style="{top:iconTop}"
        class="search-icon"
        src="../../static/home/right-btn2.png"
      />
    </view>
  </view>
</template>

<script>
export default {
  name: "listAwards",
  data() {
    return {
       iconTop: "",// 搜索icon距离顶部的距离
      current: null,
      awardsList: [],
      srcImg: "",
    };
  },
  onLoad(option) {
    this.current = option.current;
  },
  onShow() {
    this.getMyInfo();
    this.iconTop = wx.getMenuButtonBoundingClientRect().top+"px"   //获取关闭按钮的高度
  },

  methods: {
    routerTohome() {
      uni.navigateTo({
        url: `/pages/index/index`,
      });
    },
    getMyInfo() {
  },
};
</script>

<style lang="scss">
.listAwards-areas {
  .nav-top {
    text-align: center;
  }
  .search-icon {
    position: absolute;
    top: 100rpx;
    left: 28rpx;
    width: 65rpx;
    height: 65rpx;
  }
}
</style>

4.效果

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp框架开发中,要实现顶部对齐胶囊的效果,可以通过以下步骤来实现: 1. 在index.vue文件中,使用自定义导航栏布局,并设置胶囊对齐的样式。可以参考以下代码: ```html <template> <view class="bg"> <view class="title" :style="'padding-top:' + titleHeight + 'px'">新华的湖</view> </view> </template> <script> export default { data() { return { titleHeight: 0 } }, onLoad() { // 获取微信右上角胶囊高度 this.getHeight(); }, methods: { getHeight() { let res = wx.getMenuButtonBoundingClientRect(); this.titleHeight = res.top; } } }; </script> <style lang="scss" scoped> .title { font-weight: 600; line-height: 30px; margin-left: 40rpx; font-size: 17px; } </style> ``` 2. 在pages.json文件中,设置页面的导航栏样式为custom,并禁用标题视图。可以参考以下代码: ```json "pages": \[ { "path": "pages/index/index", "style": { "navigationStyle": "custom", "app-plus": { "titleView": false } } }, ... \] ``` 通过以上步骤,你可以实现uniapp小程序顶部对齐胶囊的效果。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [uniapp搜索框适配小程序对齐胶囊](https://blog.csdn.net/weixin_45788691/article/details/121679738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [uniapp小程序获取胶囊按钮高度自定义顶部标题栏](https://blog.csdn.net/DDDHL_/article/details/124046528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值