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官网现成的!加以利用。

 

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app中,底部导航栏切换时刷新页面可以通过以下几种方式实现: 1. 使用`onTabItemTap`事件监听导航栏切换。在App.vue文件中,可以通过监听`onTabItemTap`事件来处理导航栏切换的操作,然后在对应的页面中执行刷新页面的操作。 ```vue // App.vue <template> <div> <tab-bar></tab-bar> <router-view></router-view> </div> </template> <script> export default { methods: { // 监听底部导航栏切换 onTabItemTap(item) { // item.index 表示点击的是第几个导航栏 // 根据自己的需求执行相应的操作 if (item.index === 0) { // 执行页面刷新的操作 this.$refs.home.refresh(); } else if (item.index === 1) { // 执行页面刷新的操作 this.$refs.news.refresh(); } else if (item.index === 2) { // 执行页面刷新的操作 this.$refs.profile.refresh(); } } } } </script> ``` 2. 使用页面的生命周期钩子函数。在对应的页面中,可以使用`onShow`生命周期钩子函数来处理页面切换时的刷新操作。 ```vue // Home.vue <script> export default { onShow() { // 执行页面刷新的操作 this.refresh(); }, methods: { refresh() { // 页面刷新的逻辑 console.log('Home页面刷新'); } } } </script> ``` 3. 使用`uni.$on`和`uni.$emit`进行事件通信。在对应的页面中,可以使用`uni.$on`监听自定义事件,在底部导航栏切换时使用`uni.$emit`触发自定义事件,从而执行页面刷新的操作。 ```vue // Home.vue <script> export default { mounted() { // 监听自定义事件 uni.$on('refreshHome', () => { // 执行页面刷新的操作 this.refresh(); }); }, methods: { refresh() { // 页面刷新的逻辑 console.log('Home页面刷新'); } } } </script> ``` ```vue // App.vue <script> export default { methods: { // 监听底部导航栏切换 onTabItemTap(item) { if (item.index === 0) { // 触发自定义事件,通知Home页面刷新 uni.$emit('refreshHome'); } else if (item.index === 1) { // 触发自定义事件,通知News页面刷新 uni.$emit('refreshNews'); } else if (item.index === 2) { // 触发自定义事件,通知Profile页面刷新 uni.$emit('refreshProfile'); } } } } </script> ``` 这些方法都可以实现底部导航栏切换时刷新页面的效果,选择适合自己目的方式进行实现即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值