<template>
  <div class="banner-card">
    <ul class="ul">
      <li v-for="(item,index) in bannerDate" :key="index">
        <div v-if="$i18n.locale  === 'cn'" @click="clickTo(item.link)">
          <div class="card_img" >
            <img
              :src="item.imgurl"
              alt=""/>
          </div>
          <div class="card_title">
            {{item.title}}
          </div>
        </div>
        <div v-if="$i18n.locale  === 'en'" @click="clickTo(item.enLink)">
          <div class="card_img" >
            <img
              :src="item.imgurl"
              alt=""/>
          </div>
          <div class="card_title">
            {{item.enTitle}}
          </div>
        </div>
      </li>
    </ul>
    <div class="left-btn">
        <span>
          <button class="el-carousel__arrow el-carousel__arrow--left"><i class="el-icon-arrow-left"></i></button>
        </span>
    </div>
    <div class="right-btn">
      <span>
        <button class="el-carousel__arrow el-carousel__arrow--right"><i class="el-icon-arrow-right"></i></button>
      </span>
    </div>
  </div>
</template>
<script>
import {banner} from "@/api";

export default {
  data() {
    return {
      defaultConfig: {
//默认值
        classArray: ["one", "two", "three", "four", "five", "six"],
        // classArray: ["one", "two", "three", "four", "five"],
        leftSlider: true,
      },
      bannerWrap: null,
      time: null,
      lisArray: null, //卡片的集合
      classlist: null,
      leftBtn: null,
      rightBtn: null,
      bannerDate:[]
    };
  },
  beforeCreate() {
    banner("STCplDRCWAA3zcFE0EBTcQI").then(res=>{
      this.bannerDate = res.data
    })
  },
  created() {
  },
  methods: {
    clickTo(link){
      //console.log(link)
      window.location.href=link;//当前标签页
    },
    constructor() {
      this.bannerWrap = document.getElementsByClassName("banner-card")[0];
      let lis = this.bannerWrap.getElementsByTagName("li");
// 将类数组转化为数组
      this.lisArray = Array.from(lis);
      this.classlist = this.defaultConfig.classArray;
//获取左右按钮
      this.leftBtn = document.getElementsByClassName("left-btn")[0];
      this.rightBtn = document.getElementsByClassName("right-btn")[0];

      this.move(this.classlist);
      this.timer();
      this.listenMouseEvent();
      this.listenMouseClickBtnEvent();
    },
//移动
    move(list) {
      this.lisArray.forEach((value, index) => {
        value.setAttribute("class", list[index]); //给每一个li添加对应的类名
      });
    },
// 向左运动
    leftMove() {
      let popValue = this.classlist.pop(); //删除并返回数组的最后一个
      this.classlist.unshift(popValue); //把刚才删除的最后一个添加到数组开头
      this.move(this.classlist); //调用移动方法，每一个li会根据新数组的位置进行移动，从而实现轮播的效果
    },
// 向右运动
    rightMove() {
      let shiftValue = this.classlist.shift(); //删除并返回数组的第一个
      this.classlist.push(shiftValue); //把刚才删除的第一个push到数组末尾
      this.move(this.classlist);
    },
// 定时器自动切换
    timer() {
      this.time = setInterval(this.leftMove.bind(this), 1000);
    },
// 鼠标移入移出事件
    listenMouseEvent() {
      this.bannerWrap.addEventListener("mouseover", () => {
        clearInterval(this.time);
        this.leftBtn.style.cssText = `left: 6rem; opacity: 1`;
        this.rightBtn.style.cssText = `right: 6rem; opacity: 1`;
      });
      this.bannerWrap.addEventListener("mouseout", () => {
        this.timer();
        this.leftBtn.style.cssText = `left: 2rem; opacity: 0`;
        this.rightBtn.style.cssText = `right: 2rem; opacity: 0`;
      });
    },
// 点击左右按钮事件
    listenMouseClickBtnEvent() {
      this.leftBtn.addEventListener("click", () => {
        this.leftMove();
      });
      this.rightBtn.addEventListener("click", () => {
        this.rightMove();
      });
    },
  },
  mounted() {
    setTimeout(() =>{
      this.constructor();
    },1000);
  },
};

</script>
<style scoped lang="scss">

.banner-card {
  width: 100%;
  height: 50rem;
  //background: #f5f5f5;
  position: relative;

  ul {
    width: 71.5%;
    height: 100%;
    margin: 0 auto;
    text-align: center;
    position: relative;
    overflow: hidden;
  }

  li {
    border-radius: 2rem;
    width: 40rem;
    height: 40rem;
    position: absolute;
    top: 2.5rem;
    list-style-type: none;
    transition: 0.3s;
    cursor: pointer;
    background-color: #fff;

    .card_img {
      width: 100%;
      height: 75%;

      img {
        border-radius: 2rem 2rem 0 0;
        display: inline-block;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }

    .card_title {
      width: 90%;
      font-size: 2rem;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #1C1C1C;
      text-align: left;
      //width: 36rem;
      margin: 2rem auto;
      overflow:hidden; //超出的文本隐藏
      text-overflow:ellipsis; //溢出用省略号显示
      display: -webkit-box; //作为弹性伸缩盒子模型显示。
      -webkit-box-orient: vertical; //设置伸缩盒子的子元素排列方式--从上到下垂直排列
      -webkit-line-clamp: 2; //显示的行
    }

  }
}

.one {
  z-index: 98;
  left: -6rem;
  transform: scale(0.8);
}

.two {
  z-index: 99;
  left: 14rem;
  transform: scale(0.9);
}

.three {
  left: 30rem;
  z-index: 100;
  transform: scale(1);
  border: 1px solid #DA2D2D;
}

.four {
  z-index: 99;
  left: 48rem;
  transform: scale(0.9);
}

.five {
  left: 66rem;
  z-index: 98;
  transform: scale(0.8);
}

.six {
  left: 74rem;
  z-index: 96;
  transform: scale(0.7);
}

.banner-card .left-btn, .right-btn {
  transition: 0.3s;
  position: absolute;
  top: 20rem;
  cursor: pointer;
  opacity: 0;
}

.banner-card div span {
  display: inline-block;
  font-size: 2.5rem;
}

.left-btn {
  left: 2rem;
}

.right-btn {
  right: 10rem;
}

@media screen and (min-width: 1100px) and (max-width: 1400px) {
  .banner-card {
    height: 34rem;

    li {
      width: 28rem !important;
      height: 28rem !important;

      .card_title {
        font-size: 1.6rem;
        margin: 1rem 1rem;
      }
    }

    .one {
      left: -2.8rem;
    }

    .two {
      left: 10rem;
    }

    .three {
      left: 24rem;
    }

    .four {
      left: 38rem;
    }

    .five {
      left: 50.3rem;
    }

    .six {
      left: 80rem;
    }
  }


}

@media screen and (max-width: 1100px) {
  .banner-card {
    height: 34rem;

    li {
      width: 22rem !important;
      height: 22rem !important;

      .card_title {
        font-size: 1.6rem;
        margin: 1rem 1rem;
        display: -webkit-box; //作为弹性伸缩盒子模型显示。
        -webkit-box-orient: vertical; //设置伸缩盒子的子元素排列方式--从上到下垂直排列
        -webkit-line-clamp: 2; //显示的行
      }
    }

    .one {
      left: -2.6rem;
    }

    .two {
      left: 6rem;
    }

    .three {
      left: 16rem;
    }

    .four {
      left: 26rem;
    }

    .five {
      left: 34rem;
    }

    .six {
      left: 80rem;
    }
  }
}
</style>
