1. 程式人生 > >用element-ui的走馬燈carousel輕鬆實現自適應全屏banner圖 解決el-carousel預設高度300問題 元件程式碼

用element-ui的走馬燈carousel輕鬆實現自適應全屏banner圖 解決el-carousel預設高度300問題 元件程式碼

用element-ui的走馬燈carousel輕鬆實現自適應全屏banner圖 解決el-carousel預設高度300問題  元件程式碼

 

 

<template>
  <el-carousel :interval="2000" indicator-position="none" id="el-carousel">
    <el-carousel-item v-for="item in 4" :key="item">
      <img :src=" 'http://192.168.1.123:81/image/home/banner' +item+'.png' ">
    </el-carousel-item>
  </el-carousel>
</template>


<script>
  export default {
    data() {
      return {
        bannerHeight: 700,
        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;
        //圖片                高 / 寬  700 / 1920
        this.bannerHeight = 700 / 1920 * this.screenWidth - 50
        document.getElementById('el-carousel').style.height = this.bannerHeight + 'px';

      },

      setSize: function() {
        this.bannerHeight = 700 / 1920 * this.screenWidth - 50
        document.getElementById('el-carousel').style.height = this.bannerHeight + 'px';
      },

    }

  }
</script>


<style>
  .el-carousel__container {
    height: 100% !important;
  }

  img {
    display: inline-block;
    height: auto;
    max-width: 100%;
  }
</style>