评论

如何使用scroll-view制作左右滚动导航条效果

简单的导航条+swiper切换效果

最新:2020/06/13。修改为scroll-view与swiper联动效果,新增下拉刷新以及上拉加载效果。。具体效果查看代码片段,以下文章内容和就不改了
刚刚在社区里看到 有老哥在问如何做滚动的导航栏。这里简单给他写了个代码片段,需要的大哥拿去随便改改,先看效果图:

代码如下:

wxml
<scroll-view class="scroll-wrapper" scroll-x scroll-with-animation="true" scroll-into-view="item{{currentTab < 4 ? 0 : currentTab - 3}}" >
  <view class="navigate-item" id="item{{index}}" wx:for="{{taskList}}" wx:key="{{index}}" data-index="{{index}}" bindtap="handleClick">
    <view class="names {{currentTab === index ? 'active' : ''}}">{{item.name}}</view>
    <view class="currtline {{currentTab === index ? 'active' : ''}}"></view>
  </view>
</scroll-view>
wxss
.scroll-wrapper {
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
  background: #FFF;
  height: 90rpx;
  padding: 0 32rpx;
  box-sizing: border-box;
}

::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}

.navigate-item {
  display: inline-block;
  text-align: center;
  height: 90rpx;
  line-height: 90rpx;
  margin: 0 16rpx;
}

.names {
  font-size: 28rpx;
  color: #3c3c3c;
}

.names.active {
  color: #00cc88;
  font-weight: bold;
  font-size: 34rpx;
}

.currtline {
  margin: -8rpx auto 0 auto;
  width: 100rpx;
  height: 8rpx;
  border-radius: 4rpx;
}

.currtline.active {
  background: #47CD88;
  transition: all .3s;
}

JS
const app = getApp()

Page({
  data: {
    currentTab: 0,
    taskList: [{
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, {
      name: '有趣好玩'
    }, ]
  },
  onLoad() {

  },
  handleClick(e) {
    let currentTab = e.currentTarget.dataset.index
    this.setData({
      currentTab
    })
  },
})
最后奉上代码片段:

https://developers.weixin.qq.com/s/nkyp64mN7fim

最后一次编辑于  2020-06-13  
点赞 52
收藏
评论

19 个评论

  • 阿巴阿巴
    阿巴阿巴
    2019-07-26

    收藏了

    2019-07-26
    赞同 1
    回复 8
    查看更多(3)
  • 雪芽蓝域
    雪芽蓝域
    2023-09-26

    你好,有tab局部刷新的代码吗

    2023-09-26
    赞同
    回复 2
    • 牲产队-朝酒晚舞
      牲产队-朝酒晚舞
      2023-09-26
      你的局部刷新是指?有示例的图吗?
      2023-09-26
      回复
    • 波子
      波子
      03-07
      说的应该就是,下拉刷新以及上拉加载效果
      03-07
      回复
  • 神呢
    神呢
    2023-09-18

    想问一下如果我想去掉右边的那个滑动条的话应该怎么做?

    2023-09-18
    赞同
    回复 1
    • 牲产队-朝酒晚舞
      牲产队-朝酒晚舞
      2023-09-19
      右边的滑动条?是下面swiper的上下滑动?可以截个图圈一下吗?
      2023-09-19
      回复
  • 破序
    破序
    2022-10-22

    请问下面的内容怎么写啊,不同index下的内容不同

    2022-10-22
    赞同
    回复 1
    • 牲产队-朝酒晚舞
      牲产队-朝酒晚舞
      2022-10-24
      可以描述清楚点么?下面每个swiper里面你可以放不同的内容吖,我现在放的index,你可以每个swiper-item内容都不同的
      2022-10-24
      回复
  • 法医
    法医
    2022-04-20

    您好!当不需要左右滑动的时候,为什么swiper还会左右有弹一下拉伸效果?

    2022-04-20
    赞同
    回复 1
  • 申
    2022-03-04

    刚来小程序 ~请问如何改成楼层滚动样式呢

    2022-03-04
    赞同
    回复
  • 啦啦啦
    啦啦啦
    2021-07-31

    很厉害,社区就缺这类完整分享。哈哈 特意登录上来赞美,比官方扩展好用

    2021-07-31
    赞同
    回复 1
  • 。。。
    。。。
    2021-06-17

    scroll-view中间内容一多头部滑动块就会卡顿,有没有解决方法

    2021-06-17
    赞同
    回复 11
    查看更多(6)
  • ATIP
    ATIP
    2021-04-09

    好牛逼啊,我今天整体百度了一下没找能这么牛逼的案例,大哥太感谢你了

    2021-04-09
    赞同
    回复
  • 哈哈哈
    哈哈哈
    2021-04-08

    进收藏夹吃灰去吧!

    2021-04-08
    赞同
    回复 1

正在加载...

登录 后发表内容