博主信息
博文 48
粉丝 3
评论 1
访问量 45910
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
uni-app 自定义导航以及定位
吴长清
原创
2333人浏览过
  • 两个前提:

    • pages.json中取消原生导航

    • manifest.json文件中配置定位服务

  • 代码

  1. <template>
  2. <view class="container">
  3. <view class="top" :style="{height:statusBarHeight+'px',width:windowWidth+'px'}"></view>
  4. <view class="box-bg" :style="{height:navbarHeight+'px'}" @click="getLocation">
  5. <image src="/static/dingwei.png"></image>
  6. <text style="color: white;">{{locationName}}</text>
  7. <span style="color: white;" class="iconfont icon-xiala"></span>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. // 设置状态栏默认值高度为20
  16. statusBarHeight: 20,
  17. // 设置导航栏的默认值为45
  18. navbarHeight: 45,
  19. // 设置手机宽度默认值为375
  20. windowWidth: 375,
  21. // topHeigth:0,
  22. locationName: '请选择'
  23. }
  24. },
  25. methods: {
  26. getLocation() {
  27. // console.log("点击事件");
  28. uni.chooseLocation({
  29. // 成功的回调函数
  30. success: (res) => {
  31. console.log(res);
  32. // 地理位置字符串大于10时截取前面的字符串即可
  33. res.name.length > 10 ? this.locationName = res.name.substring(0, 9) + '...' : this
  34. .locationName = res.name
  35. },
  36. // 失败回调函数
  37. fail: (res) => {
  38. console.log(res);
  39. }
  40. })
  41. }
  42. },
  43. created() {
  44. // 同步获取系统(手机)信息
  45. const info = uni.getSystemInfoSync();
  46. // 动态设置状态栏的高度
  47. this.statusBarHeight = info.statusBarHeight;
  48. // 动态设置手机宽度
  49. this.windowWidth = info.windowWidth;
  50. // 获取胶囊的位置
  51. const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  52. // console.log(info);
  53. // 动态设置导航栏的位置
  54. this.navbarHeight = (menuButtonInfo.bottom - this.statusBarHeight) + (menuButtonInfo.top - this
  55. .statusBarHeight);
  56. }
  57. }
  58. </script>
  59. <style lang="scss">
  60. .top {
  61. background: linear-gradient(100deg, #00d5ff, #00aaff);
  62. }
  63. .box-bg {
  64. background: linear-gradient(100deg, #00d5ff, #00aaff);
  65. padding: 5px 10rpx;
  66. display: flex;
  67. gap: 10rpx;
  68. place-items: center;
  69. image {
  70. width: 50rpx;
  71. height: 50rpx;
  72. background-color: antiquewhite;
  73. border-radius: 510rpx;
  74. }
  75. }
  76. </style>
  • 效果

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学