点击左右移动列表

默认显示五张卡片

<template>

  <div class="cardDrawer">

    <div class="cardDrawer__container">

      <el-button v-if="isScroll" type="text" class="cardDrawer__container__left" @click="scrollList('left')"><i class="el-icon-arrow-left" /></el-button>

      <div class="cardListsContainer">

        <div ref="cardLists" class="cardDrawer__container__list">

          <!-- 这里面放卡片列表div, v-for巴拉巴拉 -->

          <cardList :list="leftList" />

          <cardList :list="rightList" />

        </div>

      </div>

      <el-button v-if="isScroll" type="text" class="cardDrawer__container__right" @click="scrollList('right')"><i class="el-icon-arrow-right" /></el-button>

    </div>

  </div>

</template>

<script>

import cardList from './components/cardList'

export default {

  components: { cardList },

  props: {

    leftList: {

      type: Array,

      default: () => []

    },

    rightList: {

      type: Array,

      default: () => []

    }

  },

  data() {

    return {

      cardWidth: 240 // 点击一次按钮移动的距离

    }

  },

  computed: {

    // 是否显示左右移动按钮

    isScroll() {

      const leftLength = this.leftList.length

      const rightLength = this.rightList.length

      let length = leftLength + rightLength

      // 如果两个卡片列表都为空,显示一个empty图片,占一张卡片的长度

      if (leftLength === 0 && rightLength === 0) length = 1

      if (length >= 6) return true

      else return false

    }

  },

  methods: {

    // 列表移动

    scrollList(type) {

      const { cardWidth } = this

      const marginLeft = this.$refs.cardLists.style.marginLeft

      const currentWidth = marginLeft ? marginLeft.match(/(\S*)px/)[1] : 0 // 当前margin-left

      const minNum = 5 // 默认时展示的完整卡片数量

      const maxWidth = (this.leftList.length + this.rightList.length - minNum) * cardWidth

      if (type === 'left') {

        if (Number(currentWidth) >= 0) return

        // console.log('左移列表')

        const marginLeft = Number(currentWidth) + cardWidth

        this.$refs.cardLists.style.marginLeft = marginLeft + 'px'

      }

      if (type === 'right') {

        // console.log('右移列表')

        if (Number(currentWidth) <= -maxWidth) return

        const marginLeft = Number(currentWidth) - cardWidth

        this.$refs.cardLists.style.marginLeft = marginLeft + 'px'

      }

    }

  }

}

</script>

<style lang="scss" scoped>

.cardDrawer {

  height: 100%;

  background-color: #f7f9fd;

  padding: 10px 0;

  &__container {

    width: 1460px;

    margin: auto;

    color: #999999;

    display: flex;

    justify-content: space-between;

    align-items: center;

    &__list {

      width: 100%;

      display: flex;

      justify-content: flex-start;

      transition: margin-left 0.3s 0s;

    }

  }

}

.cardListsContainer {

  width: 1408px;

  overflow: hidden;

}

</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值