vue实现轮播图完整版,锁定图片元素纵横比

 

代码

(1)代码使用 Element官网Carousel 走马灯完成,自己加了一个自动根据页面锁定图片纵横比的功能。

(2)imageUrl中的src改成自己的图片路径。

 

<template>
  <div>
    <el-carousel trigger="click" type="card" ref="carousel1" :height="height_carousel" :interval="6000" arrow="always"
                 style="width:auto;margin-left: 2%;margin-right: 2%">
      <el-carousel-item v-for="item in imagesUrl" :key="item.name">
          <img :src="item.src" style="height:100%;width:100%;" alt="图片替代文本" :title="item.title"/>
      </el-carousel-item>
    </el-carousel>
    <h1 style="text-align: center;font-size: 48px;">欢迎来到系统主页</h1>
  </div>
</template>

<script>
export default {
  name: "index",
  data() {
    return {
      imagesUrl: [
        {
          name: "1",
          src: require('@/assets/backImg/1.jpg'),
          title: "点击查看大图",
        },
        {
          name: "2",
          src: require('@/assets/backImg/2.jpg'),
          title: "点击查看大图",
        },
        {
          name: "3",
          src: require('@/assets/backImg/4.jpg'),
          title: "点击查看大图",
        },
        {
          name: "4",
          src: require('@/assets/backImg/5.jpg'),
          title: "点击查看大图",
        },
      ],
      height_carousel: "500px",
    }
  },
  mounted: function () {

  },
  methods: {
//动态获取元素的宽度,锁定元素纵横比
    setCarouselHeight() {
      this.$nextTick(() => {
        const car_width = this.$refs.carousel1.$el.clientWidth;
        console.log(car_width);
        this.height_carousel = (car_width * 9 / 16 / 2).toFixed(0) + "px";
        console.log(this.height_carousel);
        return this.height_carousel;
      });
    }

  },
  //
  created() {
    window.addEventListener('resize', this.setCarouselHeight)
    this.setCarouselHeight()
  },
  destroyed() {
    window.removeEventListener('resize', this.setCarouselHeight)
  },
  computed: {},
}
</script>

<style scoped>

.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值