图文切换类似轮播

在这里插入图片描述

<template>
  <div class="home">
   <el-row :gutter="20" type="flex" justify="center">
      <el-col :span="20">
        <div id="father" class="grid-content bg-purple">
          <div id="swiper-box" class="swiper-box" :style="{transform:'translateX('+currentOffset+'px)'}">
           <div class="card-item" :style="{minWidth:width}" v-for="i in 10">
              <el-card :body-style="{ padding: '0px' }">
                <img src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1089874897,1268118658&fm=26&gp=0.jpg" class="image">
                <div style="padding: 14px;">
                  <span>青蛙呱呱</span>
                  <div class="bottom clearfix">
                    <time class="time">{{ currentDate }}</time>
                    <el-button type="text" class="button">操作按钮</el-button>
                  </div>
                </div>
              </el-card>
            </div>
          </div>
          <i class="el-icon-arrow-left" @click="left"></i>
          <i class="el-icon-arrow-right" @click="right"></i>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      currentOffset:0,
      width:"20%",
      fatherWith:0,
      box:'', // dom
      currentDate: new Date()
    }
  },
  mounted () {
    this.setCardWidth()
    this.box = document.querySelector('#swiper-box')
    window.addEventListener('resize',()=>{
      this.currentOffset = 0
      this.setCardWidth()
    })
  },
  methods:{
    setCardWidth() {
      const father = document.querySelector('#father');
      this.fatherWith = father.clientWidth
      console.log("fatherWidth",this.fatherWith);
      this.width = ((this.fatherWith-20) / 5)+'px'
    },

    right() {
      console.log(this.currentOffset);
      if(this.currentOffset <= (-(this.fatherWith-20) / 5)*(10-5)) return
      this.currentOffset-=((this.fatherWith-20) / 5)
    },
    left() {
      console.log(this.currentOffset);
      if(this.currentOffset >= -2) return
      this.currentOffset+=((this.fatherWith-20) / 5)
    }
  }
}
</script>


<style lang="less" scoped>
.bg-purple {
  overflow: hidden;
  position:relative;
  padding-left: 10px;
}
.home {
  margin-top: 50px;
  .swiper-box {
    display: flex;
    width: auto;
    transition: all .2s;
  }
}

[class ^="el-icon-arrow-"] {
  position:absolute;
  top:50%;
  transform: translateY(-50%);
  font-size: 30px;
  padding: 10px 0;
  background-color: #999;
  cursor: pointer;
  
}
.el-icon-arrow-left {
  left:-7px;
}
.el-icon-arrow-right {
  right:-7px;
}

.card-item {
    padding:10px;
    box-sizing: border-box;
}
  .time {
    font-size: 13px;
    color: #999;
  }
  
  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 0;
    float: right;
  }

  .image {
    width: 100%;
    display: block;
  }

  .clearfix:before,
  .clearfix:after {
      display: table;
      content: "";
  }
  
  .clearfix:after {
      clear: both
  }
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值