手写 轮播效果

此处只做了手动点击的效果,未处理自动轮播,基于vue2书写 ,

逻辑:

  • 点击左边的图标,进行'上一个'处理,若此时在第一项,则return,否则将当前所在数据索引-1;
  • 点击右边的图标,进行'下一个'处理,若此时在最后一项,则return,否则将所在数据索引+1;
  • 当单独点击某数据时,若当前就是点击项,则return,否则将点击项数据索引赋值给当前项;

仅供参考

页面部分:
<!--      楼层选择-->
      <div class="building_select">
        <div class="icon_bg" @click="buildingSelect('previous')">
          <img src="@/assets/left_icon.png">
        </div>
        <div class="carousel">
          <div :style="{ 'transform': `translateX(${translateX})`}" class="building_box">
            <template v-for="(item,index) in floorList">
              <div :key="index" :class="item===floorList[curBuilding]?'active':''" 
                    class="detail"
                   @click="buildingSelect('other',index)">
                {{ item }}F
              </div>
            </template>
          </div>
        </div>
        <div class="icon_bg" @click="buildingSelect('next')">
          <img src="@/assets/right_icon.png">
        </div>
      </div>
js部分:
export default {
  name: '',
  props: {
    floorList: {//楼层信息
      type: Array,
      default: () => ([1,2,3,4,5,6,7,8,9,10,11,12,13])
    }
  },
  computed: {
    translateX() {
      if (this.floorList.length > 6) {
        //当页面呈现的超过7个时,点击则会滚动
        if (this.floorList.length - this.curBuilding <= 5) {
          //当剩下的不超过6个时,不滚动
          return `${(this.floorList.length - 6) * -72}px`
        } else {
          return `${this.curBuilding * -72}px`
        }
      } else {
        return 0
      }
    }
  },
  data() {
    return {
      curBuilding: 0,//默认0索引0
    }
  },
  methods: {
    //楼栋选择
    buildingSelect(type, index) {
      switch (type) {
        case 'previous'://上一个
          if (this.curBuilding === 0) return
          --this.curBuilding
          break;
        case 'next'://下一个
          if (this.curBuilding === this.floorList.length - 1) return
          ++this.curBuilding
          break;
        case 'other'://
          if (index === this.curBuilding) return
          this.curBuilding = index
          break;
      }
    },
  }
}
</script>
css部分:
 .building_select {
      display: flex;
      align-items: center;
      gap: 16px;
      color: #fff;
      padding: 24px 24px 16px;
      width: 100%;

      .icon_bg {
        width: 24px;
        height: 24px;
        cursor: pointer;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .carousel {
        flex: 1;
        overflow: hidden;
        flex-basis: 0;
      }

      .building_box {
        display: flex;
        gap: 16px;
        transition: transform 0.5s ease;

        .detail {
          min-width: 56px;
          height: 32px;
          line-height: 32px;
          background: rgba(51, 51, 51, 0.48);
          border-radius: 6px;
          font-weight: 500;
          font-size: 16px;
          cursor: pointer;
        }

        .active {
          background: #3070F9;
        }
      }
    }

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值