当前位置:   article > 正文

uniapp: 实现一个优美的带底部标识的横向滚动导航栏_uniapp横向滚动导航条

uniapp横向滚动导航条

效果如图:

 图为一个列表由后台自定义的导航栏,当数量超过8个时进行分页,横向滚动预览其它导航栏,滑动后底部条也会随之变化

代码:

1.首先是nav数组的处理

  1. // menu为接口返回的菜单数组
  2. let menuArr = [];
  3. // 处理nav为每8个一组
  4. if (menu.length) {
  5. let [start, end, result] = [null, null, []]
  6. for (let i = 0; i < Math.ceil(menu.length / 8); i++) {
  7. start = i * 8
  8. end = start + 8
  9. result.push(menu.slice(start, end))
  10. }
  11. menuArr = result;
  12. this.menus=menuArr;
  13. }

2.HTML

  1. <!-- nav列表 START -->
  2. <scroll-view class="home-index__nav" v-if="menus.length" scroll-x="true" @scroll="scrollNav">
  3. <view :class="['home-index__nav-item']" v-for="(item,index) in menus">
  4. <view class="home-index__nav-list" v-for="(nav,key) in item" :key="key"
  5. @click="goMenuDetail(nav)">
  6. <img class="home-index__nav-logo" :src='nav.pic'>
  7. <view class="home-index__nav-title area-row">{{nav.name}}</view>
  8. </view>
  9. </view>
  10. </scroll-view>
  11. <view style="height:6px;margin-top:-14rpx;" class="mb">
  12. <view class="home-index__nav-scroll" :style="`width:${36*menus.length}rpx`"
  13. v-if="menus.length >= 2"><text :style="`left:${scrollNavIndex*36}rpx`"></text></view>
  14. </view>
  15. <!-- nav列表 END -->

 3.js事件

  1. // 导航栏滚动条
  2. scrollNav(event) {
  3. const {
  4. scrollWidth,
  5. scrollLeft
  6. } = event.detail;
  7. const oneWidth = scrollWidth / this.menu
  8. let index = 0;
  9. for (let i = 0; i < this.menus.length; i
  10. if (scrollLeft >= (oneWidth * i)) {
  11. index = i;
  12. }
  13. }
  14. if (index === this.scrollNavIndex) retur
  15. this.scrollNavIndex = index;
  16. },

 4.css样式

  1. .home-index__nav {
  2. white-space: nowrap;
  3. max-height: 336rpx;
  4. padding: 24rpx 0;
  5. margin: 0 24rpx;
  6. }
  7. .home-index__nav-item {
  8. display: inline-flex;
  9. width: 100%;
  10. height: 100%;
  11. flex-wrap: wrap;
  12. margin-top: 0;
  13. }
  14. .home-index__nav-title {
  15. font-size: 24rpx;
  16. color: #454545;
  17. margin-top: 6rpx;
  18. }
  19. .home-index__nav-list {
  20. display: flex;
  21. flex-direction: column;
  22. align-items: center;
  23. justify-content: center;
  24. width: calc(calc(100% - 48rpx)/4);
  25. margin-top: 30rpx;
  26. &:nth-child(-n+4) {
  27. margin-top: 0;
  28. }
  29. image {
  30. width: 82rpx;
  31. height: 82rpx;
  32. }
  33. }
  34. .home-index__nav-scroll {
  35. position: relative;
  36. // width: 100rpx;
  37. height: 12rpx;
  38. background: #E4E4E4;
  39. border-radius: 10px;
  40. margin: 0rpx auto 20rpx auto;
  41. }
  42. .home-index__nav-scroll text {
  43. position: absolute;
  44. width: 36rpx;
  45. height: 12rpx;
  46. background: #CBAA65;
  47. border-radius: 10px;
  48. display: inline-block;
  49. }

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