uniapp,导航栏(切换项)有多项,溢出采取左滑右滑的形式展示

一、实现效果

当有多项的导航,或者说切换项,超出页面的宽度,我们采取可滑动的方式比较好一些!并且在页面右边加个遮罩,模拟最右边有渐变效果!

二、实现代码

html代码:

<!-- 头部导航栏 -->
    <view class="scroll-con">
      <scroll-view class="scroll-box" scroll-x="true" @scroll="scroll">
        <view class="scroll-item" :class="current == index? 'move':''" v-for="(item,index) in routeList" :key="item.id"
          @click="onchange(index)">
          {{item.text}}
        </view>
      </scroll-view>
      <view class="mask"></view>
    </view>

选中的样式:

.move {
    background: url("../../static/image/index_pages/item-bg.png") no-repeat;
    background-position: center;
    background-size: contain;
  }

所有样式:

  .scroll-box {
    white-space: nowrap;
    width: 100%;
  }

  .scroll-item {
    position: relative;
    display: inline-block;
    background-color: #F4F4F4;
    height: 58rpx;
    text-align: center;
    width: 128rpx;
    border-radius: 8rpx;
    font-size: 24rpx;
    line-height: 58rpx;
    font-weight: 400;
    color: #000;
    margin-right: 16rpx;
  }

  .scroll-con {
    margin-bottom: 32rpx;
    position: relative;
  }

  .mask {
    position: absolute;
    right: 0;
    top: 0;
    width: 100rpx;
    height: 58rpx;
    background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, #FFFFFF 100%);
  }

  .move {
    background: url("../../static/image/index_pages/item-bg.png") no-repeat;
    background-position: center;
    background-size: contain;
  }

js里面定义的数据:

routeList: [{
            id: 1,
            text: '路线1'
          },
          {
            id: 2,
            text: '路线2'
          },
          {
            id: 3,
            text: '路线3'
          },
          {
            id: 4,
            text: '路线4'
          },
          {
            id: 5,
            text: '路线5'
          },
          {
            id: 6,
            text: '路线6'
          }
        ],
        current: 0,

methods里面的切换方法:

onchange(index) {
        this.current = index
      },

 不是很难对吧,其实也是使用了uniapp官网现成的!加以利用。

 

最近更新

  1. TCP协议是安全的吗?

    2024-03-18 12:32:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-18 12:32:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-18 12:32:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-18 12:32:04       20 阅读

热门阅读

  1. 软件测试行业的现状和前景?

    2024-03-18 12:32:04       24 阅读
  2. 比特币,区块链及相关概念简介(四)

    2024-03-18 12:32:04       26 阅读
  3. 开源项目_搭建对象存储服务MinIO

    2024-03-18 12:32:04       18 阅读
  4. 设计模式之建造者模式

    2024-03-18 12:32:04       21 阅读
  5. ubuntu下修改硬盘名字和开机挂载硬盘

    2024-03-18 12:32:04       17 阅读
  6. 3月17日ACwing每日一题

    2024-03-18 12:32:04       18 阅读