hagerCarousel.vue 9.19 KB
<!--
 * @Date: 2024-09-27 19:49:03
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2024-11-01 08:20:35
 * @FilePath: /hager/src/components/hagerCarousel.vue
 * @Description: 文件描述
-->
<template>
  <div class="hager-carousel">
    <div class="image-switcher">
      <el-row :gutter="0">
        <el-col :span="4">
          <!-- 缩略图区域 -->
          <div class="thumbnail-container">
            <!-- 上箭头 -->
            <div class="arrow-up" @click="prevImage"><i class="el-icon-arrow-up" style="font-size: 1.35rem;"></i></div>

            <!-- 缩略图列表 -->
            <div class="thumbnails">
              <div
                v-for="(image, index) in visibleThumbnails"
                :key="index"
                :class="['thumbnail', { active: selectedImage === (startIndex + index) }]"
                @click="selectImage(startIndex + index)"
              >
                <img :src="image" alt="thumbnail">
              </div>
            </div>

            <!-- 下箭头 -->
            <div class="arrow-down" @click="nextImage"><i class="el-icon-arrow-down" style="font-size: 1.35rem;"></i></div>
          </div>
        </el-col>
        <el-col :span="20">
          <!-- 右边大图区域 -->
          <div class="main-image" :class="{ 'fade-out': isTransitioning, 'fade-in': !isTransitioning }" :style="{ backgroundImage: `url(${images[selectedImage]})`}">
          </div>
          <!--<div class="ImageMagnifier">
            <div @mouseover="handOver" @mousemove.stop="handMove" @mouseout="handOut">
              <div ref="img" class="main-image" :class="{ 'fade-out': isTransitioning, 'fade-in': !isTransitioning }" :style="{ backgroundImage: `url(${images[selectedImage]})`}"></div>
              <!~~ 放大效果 ~~>
              <!~~ 遮罩层 ~~>
              <div
                v-show="showMask"
                class="magnifier-box"
                :style="{
                  width: magnifierConfigs.width + 'px',
                  height: magnifierConfigs.height + 'px',
                  opacity:magnifierConfigs.opacity,
                  top:magnifierConfigs.top+ 'px',
                  left:magnifierConfigs.left+ 'px',
                }"
              />
              <!~~ 放大图 ~~>
              <div
                v-show="showMask"
                class="big-box"
                :style="{
                  width: bigConfigs.width + 'px',
                  height: bigConfigs.height + 'px',
                  top:bigConfigs.top+'px',
                  left:bigConfigs.left+'px'
                }"
              >
                <div
                  class="image"
                  :style="{
                    width: bigConfigs.width + 'px',
                    height: bigConfigs.height + 'px',
                    transform: bigImageConfigs.transformImage
                  }"
                >
                  <img
                    :style="{
                      width: bigImageConfigs.width + 'px',
                      height:bigImageConfigs.height+'px'
                    }"
                    :src="images[selectedImage]"
                    alt="图片不见了"
                  >
                </div>
              </div>
            </div>
          </div>-->
        </el-col>
      </el-row>

    </div>
  </div>
</template>

<script>
import mixin from "common/mixin";

export default {
  mixins: [mixin.init],
  props: {
    // 父组件传递的图片列表
    images: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      selectedImage: 0, // 当前选中的图片索引
      startIndex: 0,    // 当前显示的缩略图起始索引
      thumbnailsToShow: 3, // 每次显示4个缩略图
      isTransitioning: false, // 控制大图的切换动画
      // 配置
      magnifierConfigs: { // 遮罩层
        width: 80,
        height: 80,
        opacity: 0.3,
        top: 0,
        left: 0
      },
      bigConfigs: { // 放大的盒子
        width: 200,
        height: 200,
        top: 0,
        left: 0
      },
      bigImageConfigs: { // 放大图片
        width: 500,
        height: 400,
        transformImage: 'translate(0px, 0px)'
      },
      showMask: false
    };
  },
  mounted() {},
  computed: {
    // 计算当前显示的缩略图
    visibleThumbnails() {
      return this.images.slice(this.startIndex, this.startIndex + this.thumbnailsToShow);
    },
    endIndex() {
      return this.startIndex + this.thumbnailsToShow - 1;
    }
  },
  methods: {
    // 选择某个图片并更新大图和选中状态
    selectImage(index) {
      this.isTransitioning = true; // 开始大图的淡出动画
      setTimeout(() => {
        this.selectedImage = index; // 切换大图
        this.isTransitioning = false; // 动画结束
        this.ensureVisibleThumbnail(); // 保证选中的缩略图在可视区域
      }, 500); // 动画时间为500ms
    },
    // 控制缩略图上下滚动
    scrollThumbnails(direction) {
      if (direction === 'up' && this.startIndex > 0) {
        this.startIndex--;
      } else if (direction === 'down' && this.endIndex < this.images.length - 1) {
        this.startIndex++;
      }
    },
    // 确保选中的缩略图在可见范围内
    ensureVisibleThumbnail() {
      if (this.selectedImage < this.startIndex) {
        // 如果选中的缩略图在当前显示范围的上方,则向上滚动
        this.startIndex = this.selectedImage - this.thumbnailsToShow + 1;
      } else if (this.selectedImage > this.endIndex) {
        // 如果选中的缩略图在当前显示范围的下方,则向下滚动
        this.startIndex = this.selectedImage - this.thumbnailsToShow + 3;
      }
    },
    // 切换到下一张图片
    nextImage() {
      // if (this.selectedImage < this.images.length - 1) {
      //   this.selectImage(this.selectedImage + 1);
      // }
      if (this.startIndex < this.images.length - 3) {
        this.startIndex += 3
        this.selectImage(this.startIndex);
      }
    },
    // 切换到上一张图片
    prevImage() {
      // if (this.selectedImage > 0) {
        // this.selectImage(this.selectedImage - 1);
      // }
      if (this.startIndex > 0) {
        this.startIndex -= 3;
        this.selectImage(this.startIndex);
      }
    },
    handOver() {
      this.showMask = true
    },
    handMove(event) {
      // this.showMask = true
      const rect = this.$refs.img.getBoundingClientRect()
      // 确定遮罩层坐标
      const maskY = event.clientY - rect.top - this.magnifierConfigs.height / 2
      this.magnifierConfigs.top = maskY
      const maskX = event.clientX - rect.left - this.magnifierConfigs.width / 2
      this.magnifierConfigs.left = maskX
      // 确定放大图坐标
      this.bigConfigs.top = event.clientY - rect.top - this.bigConfigs.height / 2
      this.bigConfigs.left = event.clientX - rect.left + this.magnifierConfigs.width / 2
      // 确定图像
      this.bigImageConfigs.transformImage = `translate(-${maskX * 2}px,-${maskY * 2}px)`
    },
    handOut() {
      this.showMask = false
    }
  },
};
</script>

<style lang="less" scoped>
.hager-carousel {
  .image-switcher {
    // display: flex;
    // align-items: center;
  }

  .thumbnail-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-right: 20px;
  }

  .arrow-up,
  .arrow-down {
    cursor: pointer;
    padding: 5px;
    font-size: 18px;
    user-select: none;
  }

  .thumbnails {
    display: flex;
    flex-direction: column;
    align-items: center;
    max-height: 190px; /* 限制缩略图容器的高度 */
    overflow: hidden;
    position: relative; /* 为缩略图滚动效果提供基础 */
  }

  .thumbnail {
    width: 50px;
    height: 50px;
    margin-bottom: 10px;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border 0.3s;
  }

  .thumbnail img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.5s ease-in-out; /* 缩略图切换时的动画效果 */
  }

  .thumbnail.active {
    border: 2px solid @primary-color; /* 选中的缩略图边框样式 */
  }

  .main-image {
    width: 100%;
    height: 12rem;
    position: relative;
    transition: opacity 0.5s ease-in-out; /* 大图切换时的淡入淡出效果 */
    opacity: 1;
    background-position: center;
    background-size: contain;
    background-repeat: no-repeat;
  }

  .main-image img {
    width: 80%;
    height: 80%;
    object-fit: cover;
    position: absolute;
    top: 20%;
  }

  .main-image.fade-out {
    opacity: 0; /* 当切换时,将大图的透明度渐变为0 */
  }

  .main-image.fade-in {
    opacity: 1; /* 切换后,图片渐渐显现 */
  }

  .thumbnails-wrapper {
    transition: transform 0.5s ease-in-out; /* 缩略图滑动时的动画效果 */
    display: flex;
    flex-direction: column;
  }


  .ImageMagnifier{
    position: relative;
    .magnifier-box{
      background-color: rgb(16, 101, 186);
      position: absolute;
      z-index: 999;
      pointer-events: none; /* 让子元素在事件上透明 */
    }
    .big-box{
      background-color: #fff;
      // border: 1px solid black;
      position: absolute;
      overflow: hidden;
      z-index: 999;
      pointer-events: none; /* 让子元素在事件上透明 */
    }
  }
}
</style>