Element-ui的Carousel走马灯组件动态渲染高度

这篇博客介绍了在前端Vue项目中,如何处理Element-UI的el-carousel组件因默认固定高度导致的全屏banner变形问题。通过动态计算图片实际大小并设置组件高度,确保了走马灯内容自适应显示。代码示例展示了如何在组件挂载后监听窗口大小变化,实时调整高度,以保持图片比例,避免图片变形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在前端vue项目开发中经常会用到走马灯的场景,然而在采用Element-ui的情况下,el-carousel走马灯组件有一个默认的固定高度300px。如下所示:

.el-carousel__container {
    position: relative;
    height: 300px; // element-ui 默认高度
}

这样会导致网页的全屏的banner被压缩或拉伸,变形十分难看,在一个认真的切图仔眼里是无法容忍的。然而业务方在使用的时候没有按照相关图片规范来上传符合规格的图片大小。所以我们需要在程序中开发根据业务后台上传的图片大小来动态渲染走马灯的高度。
下面我们来看看具体的代码实现:

<template>
 <!--banner 组件-->
	<el-carousel
        :height="imgHeight"
        style="width:100%;"
        indicator-position="none"
        :arrow="bannerList.length>1 ? 'hover' : 'never'"
        :loop="true"
        :autoplay="true"
      >
        <el-carousel-item v-for="(item,index) in bannerList" :key="index">
          <template v-if="item.jumpType">
            <el-image ref="image"
              style="width:100%;"
              :src="item.image"
              fit="fit"
              class="banner-img" />
          </template>
        </el-carousel-item>
      </el-carousel>
</template>
<script>
export default {
  props:{
    bannerList:{
      type: Array,
      require: false,
      default: () => []
    }
  },
  data(){
    return {
      imgHeight: ''
    }
  },
  mounted(){
    this.imgLoad();
    // 监听窗口变动大小计算banner高度
    window.addEventListener("resize", () => {
      this.imgLoad()
    });
  },
  methods:{
    // 获取图片的实际长度和宽度
    getImgAttr (url, callback) {
        let img = new Image()
        img.src = url
        if (img.complete) {
            callback(img.width, img.height);
            // console.log(img.width, img.height, 'img.width, img.height')
        } else {
            img.onload = function () {
                callback(img.width, img.height)
                img.onload = null
            }
        }
    },
    imgLoad() {
      this.$nextTick(function() {
        // 定义页面初始的走马灯高度, 默认以第一张图片高度
        if (this.bannerList.length) {
            this.getImgAttr(this.bannerList[0].image, (width, height) => {
                // 获取屏宽计算图片容器高度
                let screenWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
                this.imgHeight = height / width * screenWidth + 'px'
                // console.log(this.imgHeight, 'this.imgHeight')
            })
        }
      });
    },
  }
}
</script>

写到这儿基本上解决了图片变形问题。注意:组件的mounted钩子中监听屏幕大小改变,在组件销毁时监听尽可能的要移除事件监听,这里程序与文章相关不大省略掉了。

要实现Vue使用Element-ui走马灯功能动态改变图片和容器大小,可以按照以下步骤进行: 1.Vue中引入Element-uiCarousel组件,并在template中使用。 2. 在data中定义图片列表和容器大小。 3. 在mounted生命周期中获取容器的宽度,以便动态调整图片大小。 4. 在methods中定义一个函数,用于动态改变容器大小。 5. 在template中使用v-bind指令将容器大小绑定到Carousel组件上,并使用v-for指令循环渲染图片。 下面是示例代码: ``` <template> <el-carousel :height="containerHeight"> <el-carousel-item v-for="(item, index) in imgList" :key="index"> <img :src="item.src" :style="{ width: imgWidth + 'px', height: imgHeight + 'px' }"> </el-carousel-item> </el-carousel> </template> <script> export default { data() { return { imgList: [ { src: 'https://xxx.com/1.jpg' }, { src: 'https://xxx.com/2.jpg' }, { src: 'https://xxx.com/3.jpg' }, ], containerWidth: 0, containerHeight: 300, // 初始化高度,可根据需求设置 imgWidth: 0, imgHeight: 0, } }, mounted() { this.containerWidth = this.$el.clientWidth this.setImgSize() window.addEventListener('resize', this.setImgSize) }, beforeDestroy() { window.removeEventListener('resize', this.setImgSize) }, methods: { setImgSize() { this.containerWidth = this.$el.clientWidth this.imgWidth = this.containerWidth this.imgHeight = this.containerHeight }, }, } </script> ``` 在上面的代码中,我们使用了v-bind指令将容器高度绑定到Carousel组件上,并使用v-for指令循环渲染了图片。在mounted生命周期中,我们获取了容器的宽度,并在setImgSize函数中动态调整了图片的大小。同时,我们还添加了一个resize事件监听器,以便在窗口大小改变时重新计算图片大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值