点击切换播放图片

点击切换播放图片_第1张图片

<template>
  <!-- banner组件 -->
  <div class="wrap-box">
    <div class="image-container">
      <img :src="currentImage" alt="Image">
    </div>
    <div class="controls">
      <div class="btn" @click="prevImage"><i class="iconfont icon-kuaijin"></i></div>
      <div class="time">{{ time }}</div>
      <div class="btn btnPlay" @click="play" v-if="isShow"><i class="el-icon-caret-right"></i></div>
      <div class="btn btnStop" @click="stop" v-else>| |</div>
      <div class="btn" @click="nextImage"><i class="iconfont icon-kuaijin-copy"></i></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "index",
  data() {
    return {
      images: [
        require("@/assets/images/code.png"),
        require("@/assets/images/co2.png"),
        require("@/assets/images/forest-icon.png"),
      ],
      currentIndex: 0,
      interval: null,
      isShow: true,
      timeArr: ["2022", "2023", "2024"],
      // time: "2023",
    };
  },
  computed: {
    time() {
      return this.timeArr[this.currentIndex];
    },
    currentImage() {
      return this.images[this.currentIndex];
    },
  },
  created() {
    this.$emit("getImg", this.currentIndex);
  },
  watch: {
    currentIndex(newVal, oldVal) {
      console.log("监听:", newVal, oldVal);
    },
  },
  mounted() {},

  methods: {
    prevImage() {
      this.currentIndex =
        (this.currentIndex - 1 + this.images.length) % this.images.length;
      this.$emit("getImg", this.currentIndex);
    },
    nextImage() {
      this.currentIndex = (this.currentIndex + 1) % this.images.length;
      this.$emit("getImg", this.currentIndex);
      console.log("下一页:", this.currentIndex, this.images.length - 1);
    },
    play() {
      this.isShow = false;
      this.interval = setInterval(this.playAnimation, 1500);
    },
    stop() {
      clearInterval(this.interval);
      this.isShow = true;
      console.log("暂停");
    },
    // 间隔几秒进行动画
    playAnimation() {
      this.currentIndex = (this.currentIndex + 1) % this.images.length;
      this.$emit("getImg", this.currentIndex);
    },
  },
};
</script>

<style lang="scss" scoped>
.wrap-box {
  height: -webkit-fill-available;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
.image-container {
  width: 30%;
  // height: 300px;
  background: rgb(238, 166, 166);
  margin-top: 16px;
  margin-bottom: 16px;
  flex-grow: 1;
  img {
    width: 100%;
    height: 100%;
  }
}
.controls {
  width: 30%;
  height: 54px;
  line-height: 54px;
  border: 1px solid #e1e6f0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  padding: 10px;
  grid-column-gap: 10px;
  .btn {
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #e1e6f0;
    border-radius: 50%;
    color: #c0c4cc;
    cursor: pointer;
    .el-icon-caret-right {
      font-size: 24px;
    }
    .iconfont {
      font-size: 14px;
    }
  }
  .btnPlay {
    color: #606266;
    background: #e1e6f0;
  }
  .btnStop {
    font-weight: 700;
  }
  .time {
    height: 34px;
    line-height: 34px;
    text-align: center;
    border-radius: 4px;
    background: #e1e6f0;
    flex-grow: 1;
    color: #606266;
    font-size: 14px;
  }
}
</style>

你可能感兴趣的:(javascript,前端,vue.js)