当前位置:首页 > VUE

vue实现图片组件

2026-02-18 18:51:11VUE

Vue 实现图片组件的方法

基础图片组件实现

创建一个基础的图片组件,接收图片路径作为 props,并显示图片。

<template>
  <img :src="src" :alt="alt" :width="width" :height="height" />
</template>

<script>
export default {
  props: {
    src: {
      type: String,
      required: true
    },
    alt: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: 'auto'
    },
    height: {
      type: [String, Number],
      default: 'auto'
    }
  }
}
</script>

添加懒加载功能

使用 Intersection Observer API 实现图片懒加载,提升页面性能。

vue实现图片组件

<template>
  <img
    ref="image"
    :src="placeholder"
    :alt="alt"
    :width="width"
    :height="height"
    @load="handleLoad"
  />
</template>

<script>
export default {
  props: {
    src: {
      type: String,
      required: true
    },
    placeholder: {
      type: String,
      default: 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
    },
    alt: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: 'auto'
    },
    height: {
      type: [String, Number],
      default: 'auto'
    }
  },
  mounted() {
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          this.$refs.image.src = this.src
          observer.unobserve(entry.target)
        }
      })
    })
    observer.observe(this.$refs.image)
  },
  methods: {
    handleLoad() {
      this.$emit('load')
    }
  }
}
</script>

添加图片加载状态处理

处理图片加载过程中的不同状态,如加载中、加载成功和加载失败。

<template>
  <div class="image-container">
    <img
      v-if="status === 'loaded'"
      :src="src"
      :alt="alt"
      :width="width"
      :height="height"
    />
    <div v-else-if="status === 'loading'" class="loading-placeholder">
      <!-- 加载中占位内容 -->
    </div>
    <div v-else-if="status === 'error'" class="error-placeholder">
      <!-- 加载失败占位内容 -->
    </div>
  </div>
</template>

<script>
export default {
  props: {
    src: {
      type: String,
      required: true
    },
    alt: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: 'auto'
    },
    height: {
      type: [String, Number],
      default: 'auto'
    }
  },
  data() {
    return {
      status: 'loading' // loading, loaded, error
    }
  },
  mounted() {
    const img = new Image()
    img.src = this.src
    img.onload = () => {
      this.status = 'loaded'
    }
    img.onerror = () => {
      this.status = 'error'
    }
  }
}
</script>

<style>
.image-container {
  position: relative;
  width: 100%;
  height: 100%;
}

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

实现图片预览功能

添加点击图片放大预览的功能。

vue实现图片组件

<template>
  <div>
    <img
      :src="src"
      :alt="alt"
      :width="width"
      :height="height"
      @click="showPreview"
      style="cursor: pointer;"
    />
    <div v-if="previewVisible" class="preview-modal" @click="hidePreview">
      <img :src="src" :alt="alt" class="preview-image" />
    </div>
  </div>
</template>

<script>
export default {
  props: {
    src: {
      type: String,
      required: true
    },
    alt: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: 'auto'
    },
    height: {
      type: [String, Number],
      default: 'auto'
    }
  },
  data() {
    return {
      previewVisible: false
    }
  },
  methods: {
    showPreview() {
      this.previewVisible = true
      document.body.style.overflow = 'hidden'
    },
    hidePreview() {
      this.previewVisible = false
      document.body.style.overflow = ''
    }
  }
}
</script>

<style>
.preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.preview-image {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
}
</style>

响应式图片组件

根据屏幕尺寸加载不同大小的图片,优化性能。

<template>
  <picture>
    <source
      v-for="(source, index) in sources"
      :key="index"
      :media="source.media"
      :srcset="source.srcset"
      :type="source.type"
    />
    <img
      :src="fallback"
      :alt="alt"
      :width="width"
      :height="height"
    />
  </picture>
</template>

<script>
export default {
  props: {
    sources: {
      type: Array,
      required: true,
      validator: value => value.every(item => 
        item.media && item.srcset
      )
    },
    fallback: {
      type: String,
      required: true
    },
    alt: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: 'auto'
    },
    height: {
      type: [String, Number],
      default: 'auto'
    }
  }
}
</script>

使用方法:

<responsive-image
  :sources="[
    { media: '(min-width: 1200px)', srcset: 'large.jpg' },
    { media: '(min-width: 768px)', srcset: 'medium.jpg' },
    { media: '(max-width: 767px)', srcset: 'small.jpg' }
  ]"
  :fallback="'fallback.jpg'"
  alt="Responsive image"
/>

这些方法提供了从基础到高级的图片组件实现方案,可以根据项目需求选择适合的方式或组合使用多种功能。

标签: 组件图片
分享给朋友:

相关文章

vue实现图片查看

vue实现图片查看

Vue 实现图片查看功能 在 Vue 中实现图片查看功能,可以通过原生实现或使用第三方库。以下是几种常见的方法: 原生实现方式 使用 Vue 的指令和事件绑定实现基础的图片查看功能。 <…

网页设计制作css图片

网页设计制作css图片

CSS图片处理基础方法 在网页设计中,CSS可以控制图片的尺寸、位置、边框等样式属性。通过<img>标签或背景图方式引入图片后,使用CSS属性进行调整。 img { width: 1…

vue组件实现

vue组件实现

Vue 组件实现 Vue 组件是 Vue.js 的核心特性之一,允许开发者将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种常见方式: 单文件组件(SFC) 单文件组件是 Vue 中…

js实现图片轮播

js实现图片轮播

实现基础图片轮播 使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS设置样式和动画效果,JavaScript处理轮播逻辑。 <div c…

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gree…

vue实现折叠组件

vue实现折叠组件

实现折叠组件的基本思路 在Vue中实现折叠组件通常需要利用动态绑定和条件渲染。核心是通过控制一个布尔值状态来决定内容是否显示,并添加过渡动画提升用户体验。 基础实现方法 使用v-show或v-if控…