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
      },
scroll() {
				        // 编写你的滚动逻辑代码
				        console.log('滚动事件被触发');
				      },

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

耀南.

你的鼓励将是我最最最最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值