当前位置:首页 > VUE

vue实现画廊

2026-02-10 10:58:38VUE

Vue实现画廊功能

使用Vue实现画廊功能可以通过组件化开发结合第三方库或原生JavaScript实现。以下是两种常见方案:

基于vue-gallery组件

安装vue-gallery库:

npm install vue-gallery --save

基本实现代码:

<template>
  <gallery :images="images" :index="index" @close="index = null"></gallery>
  <div class="image" v-for="(image, imageIndex) in images" :key="imageIndex" 
       @click="index = imageIndex" :style="{ backgroundImage: 'url(' + image + ')' }"></div>
</template>

<script>
import VueGallery from 'vue-gallery';
export default {
  components: {
    'gallery': VueGallery
  },
  data() {
    return {
      images: [
        'image1.jpg',
        'image2.jpg',
        'image3.jpg'
      ],
      index: null
    }
  }
}
</script>

<style>
.image {
  float: left;
  width: 200px;
  height: 200px;
  margin: 5px;
  cursor: pointer;
  background-size: cover;
}
</style>

原生Vue实现方案

<template>
  <div class="gallery">
    <div class="thumbnails">
      <img v-for="(img, idx) in images" :key="idx" 
           :src="img.thumbnail" @click="openLightbox(idx)">
    </div>

    <div class="lightbox" v-if="lightboxVisible" @click="closeLightbox">
      <img :src="currentImage.full" class="lightbox-image">
      <button @click.stop="prevImage">Previous</button>
      <button @click.stop="nextImage">Next</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        { thumbnail: 'thumb1.jpg', full: 'full1.jpg' },
        { thumbnail: 'thumb2.jpg', full: 'full2.jpg' }
      ],
      currentIndex: 0,
      lightboxVisible: false
    }
  },
  computed: {
    currentImage() {
      return this.images[this.currentIndex]
    }
  },
  methods: {
    openLightbox(index) {
      this.currentIndex = index
      this.lightboxVisible = true
    },
    closeLightbox() {
      this.lightboxVisible = false
    },
    prevImage() {
      this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length
    },
    nextImage() {
      this.currentIndex = (this.currentIndex + 1) % this.images.length
    }
  }
}
</script>

<style>
.thumbnails {
  display: flex;
  flex-wrap: wrap;
}
.thumbnails img {
  width: 150px;
  height: 150px;
  object-fit: cover;
  margin: 5px;
  cursor: pointer;
}
.lightbox {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
}
.lightbox-image {
  max-width: 80%;
  max-height: 80%;
}
</style>

高级功能扩展

添加过渡动画

<transition name="fade">
  <div class="lightbox" v-if="lightboxVisible">
    <!-- 内容 -->
  </div>
</transition>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

键盘导航支持

mounted() {
  window.addEventListener('keydown', this.handleKeydown)
},
beforeDestroy() {
  window.removeEventListener('keydown', this.handleKeydown)
},
methods: {
  handleKeydown(e) {
    if (this.lightboxVisible) {
      if (e.key === 'ArrowLeft') this.prevImage()
      if (e.key === 'ArrowRight') this.nextImage()
      if (e.key === 'Escape') this.closeLightbox()
    }
  }
}

图片懒加载优化

使用vue-lazyload插件:

npm install vue-lazyload

实现代码:

import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload, {
  preLoad: 1.3,
  loading: 'loading.gif',
  attempt: 1
})

// 模板中使用
<img v-lazy="img.thumbnail">

响应式设计建议

/* 响应式布局 */
.thumbnails {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}

@media (max-width: 768px) {
  .thumbnails {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  }
}

vue实现画廊

标签: 画廊vue
分享给朋友:

相关文章

实现vue组件

实现vue组件

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

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CS…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…