Blogger Information
Blog 48
fans 3
comment 1
visits 30343
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
uni-app 自定义导航以及定位
吴长清
Original
1416 people have browsed it
  • 两个前提:

    • 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>
  • 效果

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post