<template>
  <div class="view">
    <div class="img-box">
      <div class="show-box" :style="{
          transform: 'translateX(' + translate + 'rem)',
          transition: tsion ? 'all 0.5s' : 'none',
        }" ref="swip">
        <img src="https://img1.baidu.com/it/u=1966616150,2146512490&fm=253&fmt=auto&app=138&f=JPEG?w=751&h=500" alt="" />
        <img v-for="(item, i) in msg" :key="i" :src="item.img" alt="" class="imgg" />
        <img src="https://img1.baidu.com/it/u=1966616150,2146512490&fm=253&fmt=auto&app=138&f=JPEG?w=751&h=500" alt="" />
      </div>
    </div>
    <div class="arrowhead-box">
      <span @click="last"><img src="../../assets/OIP-C.jpg" alt="" style="height: 40px; width: 40px" /></span>
      <span @click="next"><img src="../../assets/OIP-C.jpg" alt="" style="height: 40px; width: 40px" /></span>
    </div>
    <div class="swiper-boxs" ref="swiper">
      <span v-for="(item, i) in msg" :key="i" @click="swiper(i)"></span>
    </div>
  </div>
</template>

<script>
export default {
  name: "Banner",
  props: {
    msg: Array,
    requirdd: true,
  },
  data () {
    return {
      translateX: 0,
      tsion: true,
    };
  },
  methods: {
    last () {
      this.translateX--;
      this.tsion = true;
      if (this.translateX < 0) {
        setTimeout(() => {
          this.tsion = false;
          this.translateX = this.msg.length - 1;
        }, 500);
      }
    },
    next () {
      this.translateX++;
      this.tsion = true;
      if (this.translateX > this.msg.length - 1) {
        setTimeout(() => {
          this.tsion = false;
          this.translateX = 0;
        }, 500);
      }
    },
    swiper (i) {
      this.translateX = i;
    },
  },
  mounted () {
    setInterval(() => {
      this.next();
    }, 3000);
  },
  computed: {
    translate () {
      return -(this.translateX + 1) * 36;
    },
  },
  watch: {
    translateX: {
      handler (val) {
        let a = this.$refs.swiper.querySelectorAll("span");
        a.forEach((element) => {
          element.style.width = "12px";
        });
        if (this.translateX < 0) {
          val = this.msg.length - 1;
        }
        if (this.translateX > this.msg.length - 1) {
          val = 0;
        }
        a[val].style.width = "30px";
      },
    },
  },
};
</script>
<style scoped>
body {
  margin: 0;
  padding: 0;
  background-image: linear-gradient(to top, #37ecba 0%, #72afd3 100%);
}
button {
  width: 90px;
  height: 85px;
  margin-top: 10px;
}
.view {
  width: 380px;
  /* height: 160px; */
  margin-top: 10px;
}
span {
  margin: 0;
  padding: 0;
}
.big-box {
  width: 100%;
  background-color: #999;
  margin: 10px auto;
  /* position: fixed; */
  position: absolute;
  margin-top: 2%;
  margin-left: 2%;
}

.show-box {
  display: flex;
  transition: all 0.5s;
}
.show-box img {
  float: left;
  min-width: 35rem;
  height: 200px;
}
.arrowhead-box {
  position: fixed;
  top: 160px;
  float: left;
  width: 100%;
  height: 50px;
}
.arrowhead-box span {
  float: left;
  display: block;
  /* height: 60px; */
  border-radius: 50px;
  background-position: 6px 14px;
  cursor: pointer;
  opacity: 0.5;
}
.arrowhead-box span img {
  border-radius: 50px;
}
.arrowhead-box span:nth-child(2) {
  float: right;
  transform: rotate(180deg);
}
.arrowhead-box span:hover {
  opacity: 1;
}
.swiper-boxs {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  top: 230px;
}
.swiper-boxs span {
  float: left;
  width: 12px;
  height: 12px;
  background-color: rgb(250, 177, 177);
  border-radius: 50px;
  margin-left: 10px;
  cursor: pointer;
  transition: all 0.5s;
}
.swiper-boxs span:nth-child(1) {
  width: 30px;
}
</style>
