当前位置:   article > 正文

uniapp 自定义底部导航并适配+返回顶部效果_uni-nav-bar底部

uni-nav-bar底部

样式

 

可以自己自定义顶部导航所有样式,包括凸起的效果

先在app.vue,判断全屏的机型

  1. onLaunch: function() {
  2. //判断设备是否为 iPhone
  3. const self = this
  4. uni.getSystemInfo({
  5. success: function(res) {
  6. // 根据 model 进行判断
  7. let iphoneArr = ['iPhone X', 'iPhone XR','iPhone XS Max','iPhone 11','iPhone 11 Pro','iPhone 11 Pro Max']
  8. self.globalData.isIphone = iphoneArr.some(function(item){
  9. return item === res.model
  10. })
  11. }
  12. })
  13. },
  14. globalData: {
  15. isIphone:false
  16. },

首页代码

html段

  1. <template>
  2. <view class="content">
  3. <!-- uni-nav-bar 自带组件 -->
  4. <uni-nav-bar :fixed="true" color="#000000" :status-bar="true" left-icon="arrowleft"
  5. left-text="返回" title="uni-nav-bar 标题" @clickLeft="back" class="header-nav"/>
  6. <!-- 中间内容区域 -->
  7. <scroll-view
  8. scroll-y="true"
  9. :style="{'height': contentHeight + 'px'}"
  10. class="content-details"
  11. @scroll="scroll"
  12. :scroll-top="scrollTop"
  13. >
  14. <!-- 对应按钮的内容展示 -->
  15. <one v-show="isShow == 1"></one>
  16. <two v-show="isShow == 2"></two>
  17. <three v-show="isShow == 3"></three>
  18. <four v-show="isShow == 4"></four>
  19. <!-- 返回顶部 -->
  20. <view
  21. class="bottonTop"
  22. :style="{'top': contentHeight - 20 + 'px','display':(topState===true? 'block':'none')}"
  23. @click="goBackTop">
  24. T
  25. </view>
  26. </scroll-view>
  27. <!-- 底部导航 -->
  28. <view class="footer-bar" :style="{'padding-bottom': isTrue ? '30rpx' : '0' }">
  29. <view class="footer-content" @click="nav(1)">
  30. <image src="/static/tu1.png" mode=""></image>
  31. <text>模式一</text>
  32. </view>
  33. <view class="footer-content" @click="nav(2)">
  34. <image src="/static/tu2.png" mode=""></image>
  35. <text>模式二</text>
  36. </view>
  37. <view class="footer-content" @click="nav(3)">
  38. <image src="/static/tu3.png" mode=""></image>
  39. <text>模式三</text>
  40. </view>
  41. <view class="footer-content" @click="nav(4)">
  42. <image src="/static/tu4.png" mode=""></image>
  43. <text>模式四</text>
  44. </view>
  45. </view>
  46. </view>
  47. </template>

JS

  1. <script>
  2. // 自己写的对应底部导航的页面
  3. import one from "../index/index.vue";
  4. import two from '../modelOne/modelOne.vue';
  5. import three from '../modelTwo/modelTwo.vue';
  6. import four from '../modelThree/modelThree.vue';
  7. export default {
  8. components: {
  9. one,two,three,four
  10. },
  11. data() {
  12. return {
  13. isTrue: false,
  14. contentHeight: '',
  15. isShow: 1,
  16. topState: false,
  17. scrollTop: 0,
  18. }
  19. },
  20. onLoad() {
  21. // 判断是否是全面屏
  22. this.isTrue = getApp().globalData.isIphone;
  23. },
  24. onReady() {
  25. // 计算屏幕剩余高度 填补剩余高度
  26. let _this = this;
  27. uni.getSystemInfo({
  28. success: function(res) {
  29. let obj = uni.createSelectorQuery().select('.footer-bar');
  30. obj.boundingClientRect(function(data) { // data - 各种参数
  31. _this.contentHeight = res.windowHeight - data.height;
  32. }).exec()
  33. let obj1 = uni.createSelectorQuery().select('.header-nav');
  34. obj1.boundingClientRect(function(data) { // data - 各种参数
  35. if(_this.isTrue){
  36. _this.contentHeight = _this.contentHeight - data.height - 24;
  37. }else{
  38. _this.contentHeight = _this.contentHeight - data.height
  39. }
  40. }).exec()
  41. }
  42. });
  43. },
  44. methods: {
  45. // 底部导航切换
  46. nav(index) {
  47. this.isShow = index;
  48. this.topState = false;
  49. this.scrollTop = 0;
  50. },
  51. // 滚动事件
  52. scroll(e) {
  53. if(e.detail.scrollTop == 0) {
  54. this.topState = false;
  55. }else {
  56. this.topState = true;
  57. }
  58. this.scrollTop = e.detail.scrollTop;
  59. },
  60. // 返回顶部
  61. goBackTop() {
  62. this.scrollTop = 0;
  63. this.topState = false;
  64. }
  65. },
  66. }
  67. </script>

css

  1. <style>
  2. .content {
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. justify-content: center;
  7. }
  8. .content-details {
  9. width: 100%;
  10. position: relative;
  11. }
  12. .bottonTop{
  13. background: #FFFFFF;
  14. line-height: 48rpx;
  15. width: 48rpx;
  16. text-align: center;
  17. height: 48rpx;
  18. position: fixed;
  19. right: 20rpx;
  20. }
  21. .footer-bar {
  22. position: fixed;
  23. width: 100%;
  24. height: 112rpx;
  25. background: #fafafa;
  26. bottom: 0;
  27. left: 0;
  28. display: flex;
  29. align-items: center;
  30. }
  31. .footer-content {
  32. flex: 1;
  33. display: flex;
  34. flex-direction: column;
  35. justify-content: center;
  36. align-items: center;
  37. }
  38. .footer-content image {
  39. width: 48rpx;
  40. height: 48rpx;
  41. }
  42. .footer-content text {
  43. margin-top: 10rpx;
  44. font-size: 28rpx;
  45. color: #999;
  46. }
  47. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/131574
推荐阅读