微信小程序使用scroll-view做导航栏

在这里插入图片描述

<view class="banner">
  <scroll-view scroll-x="true" scroll-with-animation="true" scroll-into-view="nav-{{select > 0 ? select -1 : select}}">
    <view wx:for="{{sortList}}" wx:for-index="index" wx:key="index" id="nav-{{index+1}}"
      class="sort {{index == select ? 'choose' : ''}}" data-index='{{index}}' data-id="{{item.id}}" bindtap="getSelect">
      {{item.name}}
      <view class="line" wx:if="{{index == select}}"></view>
    </view>
  </scroll-view>
</view>
.banner {
  position: sticky;
  top: 0;
  width: 100%;
  height: 80rpx;
  background: #fff;
  white-space: nowrap;
  display: flex;
  align-items: center;
  padding: 0 30rpx;
  box-sizing: border-box;
  border-top: 1rpx solid #f5f5f5;
  border-bottom: 1rpx solid #f5f5f5;
}

.banner scroll-view {
  height: 100%;
}

.sort {
  position: relative;
  display: inline-block;
  font-size: 32rpx;
  margin-right: 70rpx;
  line-height: 80rpx;
  height: 100%;
}

scroll-view .sort:last-child {
  margin-right: 0rpx;
}

.choose {
  color: #16BA98;
}

.line {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 5rpx;
  border-radius: 10rpx;
  background: #16BA98;
  animation: spread .3s;
}

@keyframes spread {
  0% {
    width: 0;
    left: 50%;
  }

  100% {
    width: 100%;
    left: 0;
  }
}
const app = getApp()
Page({
  data: {
    select: 0,
    sortList: [{
        name: '电视剧'
      },
      {
        name: '电影'
      },
      {
        name: '综艺'
      },
      {
        name: '少儿'
      },
      {
        name: '动漫'
      },
      {
        name: 'NBA'
      },
      {
        name: '体育'
      },
      {
        name: '财经'
      },
      {
        name: '历史'
      },
    ]
  },

  getSelect(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      select: index
    })
  },

})
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值