需要阅读上文 点我跳转 , 如果你已经理解如何实现点击文字变色原理,可以直接阅读本文

scroll-view

这是一个可滚动视图区域,我们需要先设置可滑动导航栏

wxml

  1. <scroll-view scroll-x="true" >
  2. <view bindtap="add" class="{{ index === current ? 'active' : '' }}" data-index="{{index}}" wx:for="{{title}}" wx:key="{{index}}">{{item}}</view>
  3. </scroll-view>

wxss

  1. scroll-view{
  2. background: black;
  3. width: 100%;
  4. height: 50rpx;
  5. white-space: nowrap;
  6. }
  7. view{
  8. color: white;
  9. display: inline-block;
  10. width: 30%;
  11. }
  12. .active{
  13. color: red;
  14. }
  15. .active:after{
  16. content: '';
  17. width: 30%;
  18. height: 2rpx;
  19. background-color: pink;
  20. display: block;
  21. }

图片.png

  • scroll-x 向右滑动许可,默认为fasle
  • white-space :nowrap 合并连续的空白与换行,否则无法实现右滑

    swiper

    这是一个滑块视图容器(也可以实现轮播图)

    wxml

    1. <swiper current="{{current}}">
    2. <swiper-item wx:for="{{[0,1,3,4,5] }}">
    3. {{item}}
    4. </swiper-item>
    5. </swiper>
    // 设置它的 下标 current 在swiper-item中实现翻页

js

  1. const app = getApp()
  2. Page({
  3. data: {
  4. title:['数据1','数据2','数据3','数据4','数据5','数据6','数据7'],
  5. current:null
  6. },
  7. onLoad: function () {
  8. },
  9. add(e){
  10. let index = e.currentTarget.dataset.index
  11. this.setData({current : index})
  12. }
  13. })