解决el-carousel默认高度300问题(实现自适应全屏banner图 )

代码:

<template>
  <el-carousel  indicator-position="none" id="el-carousel">
    <el-carousel-item v-for="item in 4" :key="item">
      <img :src=" 'http://XXXXX/image/home/banner' +item+'.png' ">
    </el-carousel-item>
  </el-carousel>
</template>
<script>
  export default {
    data() {
      return {
        bannerHeight: 500,
        screenWidth: 1920,
      };
    },
    mounted() {
      this.setSize1();
      const that = this;
      //监听浏览器窗口大小改变
      window.addEventListener('resize', function() {
        var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
        that.screenWidth = width;
        that.setSize();
      }, false);
    },
    methods: {
      setSize1: function() {
        var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
        this.screenWidth = width;
        //图片                高 / 宽  500 / 1920
        this.bannerHeight = 500 / 1920 * this.screenWidth - 50
        document.getElementById('el-carousel').style.height = this.bannerHeight + 'px';
      },
      setSize: function() {
        this.bannerHeight = 500 / 1920 * this.screenWidth - 50
        document.getElementById('el-carousel').style.height = this.bannerHeight + 'px';
      },
    }
  }
</script>
<style lang="scss">
  .el-carousel__container {
    height: 100% !important;
  }
 
  img {
    display: inline-block;
    height: auto;
    max-width: 100%;
  }
</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容