1. 项目准备

在开始之前,确保你的开发环境已配置好Node.js和Vue CLI。以下是在Vue项目中创建新组件的基本步骤:

1.1 创建组件目录

在项目根目录下创建一个名为 components 的文件夹,并在其中创建一个名为 Thumbnail.vue 的文件。

1.2 编写组件结构

Thumbnail.vue 文件中,编写以下基本结构:

<template>
  <div class="thumbnail-container">
    <img :src="imageSrc" :alt="imageAlt" class="thumbnail-image"/>
  </div>
</template>

<script>
export default {
  name: 'Thumbnail',
  props: {
    imageSrc: String,
    imageAlt: String
  }
}
</script>

<style scoped>
.thumbnail-container {
  width: 100px; /* 容器宽度 */
  height: 100px; /* 容器高度 */
  overflow: hidden; /* 隐藏溢出的内容 */
  border-radius: 5px; /* 圆角效果 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影效果 */
}

.thumbnail-image {
  width: 100%; /* 图片宽度 */
  height: 100%; /* 图片高度 */
  object-fit: cover; /* 覆盖容器,保持图片比例 */
}
</style>

1.3 引入组件

在需要使用缩略图组件的Vue文件中,引入并使用该组件:

<template>
  <div>
    <Thumbnail imageSrc="path/to/image.jpg" imageAlt="描述图片内容"/>
  </div>
</template>

<script>
import Thumbnail from './components/Thumbnail.vue';

export default {
  components: {
    Thumbnail
  }
}
</script>

2. 功能扩展

2.1 图片加载动画

2.2 点击放大效果

2.3 添加图注

3. 代码示例

<template>
  <div class="thumbnail-container" @click="openFullImage">
    <img :src="imageSrc" :alt="imageAlt" class="thumbnail-image" v-if="imageLoaded"/>
    <div class="loading" v-else>Loading...</div>
  </div>
</template>

<script>
export default {
  name: 'Thumbnail',
  data() {
    return {
      imageLoaded: false
    };
  },
  props: {
    imageSrc: String,
    imageAlt: String
  },
  methods: {
    openFullImage() {
      window.open(this.imageSrc);
    }
  },
  mounted() {
    const img = new Image();
    img.src = this.imageSrc;
    img.onload = () => {
      this.imageLoaded = true;
    };
  }
}
</script>

<style scoped>
.thumbnail-container {
  /* ... */
}

.thumbnail-image {
  /* ... */
}

.loading {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}
</style>

4. 总结