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

14 篇文章 0 订阅
3 篇文章 0 订阅

在前端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钩子中监听屏幕大小改变,在组件销毁时监听尽可能的要移除事件监听,这里程序与文章相关不大省略掉了。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用中提到的,使用element-plus的el-carousel组件来放置滚动广告时,可以通过设置type="card"来实现效果。至于走马灯图片的大小,文档中并没有具体说明。 根据引用中的解决方法,可以为carousel组件添加显示条件list.length > 0,只有当list不为null且长度大于0时才会渲染元素。这意味着走马灯图片的大小可能取决于绑定给el-carousel的数据list中的元素数量和大小。 引用中提到,el-carousel绑定的数据需要一个初始值。如果初始值为空数组([]),可能会出现异常。所以可以通过为初始值提供一些图片元素,比如var bannerList = ref([1, 2, 3]),来确保开始时就有一些图片展示在走马灯中,从而确定走马灯的图片大小。 综上所述,element-plus carousel走马灯的图片大小是由绑定给el-carousel的数据中的图片元素数量和大小决定的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [element-plus 之el-carousel 走马灯 加载图片刚进入的时候为什么数据显示不对?](https://blog.csdn.net/weixin_43136717/article/details/128233256)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Vue3 + Element-Plus carousel走马灯 显示动态数据时 默认会显示一个空白元素](https://blog.csdn.net/qq_40007006/article/details/120440831)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值