Table of Contents
Step 1: Preparation
Step 2: Write the component
Step 3: Call the component
Conclusion
Home Web Front-end Vue.js How to implement image gallery using Vue?

How to implement image gallery using Vue?

Jun 25, 2023 pm 02:51 PM
vue accomplish Picture gallery

In today's Internet era, image display is an integral part of website design. How to effectively display images has become a problem that every website designer must solve. As a form of image display, picture gallery is widely favored by website designers for its good user experience and attractive visual effects. This article will introduce how to use Vue to implement a simple picture gallery.

Step 1: Preparation

The first thing to do is to introduce the required library files into the project. The two library files required in this article are Vue and Photoswipe. Photoswipe is a popular JavaScript library for image galleries that is lightweight, customizable, and supports all major mobile and desktop browsers. In order for this library to be seamlessly integrated with Vue, we need to introduce a small adapter file vue-photoswipe.js.

Step 2: Write the component

Next, we will create a Vue component named Gallery to display the image gallery. The code of the component is as follows:

<template>
  <div class="gallery">
    <div class="grid">
      <div class="grid-item" v-for="(item, index) in items" :key="index" @click="openGallery(index)">
        <img :src="item.thumbnailUrl">
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import PhotoSwipe from 'photoswipe'
import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default'
import 'photoswipe/dist/photoswipe.css'
import 'photoswipe/dist/default-skin/default-skin.css'
import adapter from './vue-photoswipe'

Vue.use(adapter)

export default {
  name: 'Gallery',
  props: {
    items: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      pswpOptions: {
        //Photoswipe的配置项
      }
    }
  },
  methods: {
    openGallery (index) {
      const pswpElement = document.querySelectorAll('.pswp')[0]
      const gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, this.items, this.pswpOptions)
      gallery.init()
      gallery.goTo(index)
    }
  }
}
</script>

<style scoped>
.gallery {
  margin: 20px auto;
  width: 80%;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  grid-gap: 20px;
}

.grid-item {
  cursor: pointer;
}

.grid-item img {
  width: 100%;
  height: auto;
}
</style>
Copy after login

In this code, we define a component named Gallery. The component receives an items prop, which is an array containing image information. This component uses the third-party library PhotoSwipe, which supports image scaling, enlarging, carousel and other functions. Please note when writing that vue-photoswipe.js references a small PhotoSwipe (only includes functions that can be automatically converted to a single file for use). If you need more functions or download the original code, please make sure you have included PhotoSwipe.

In this component, we use a loop-based method to output the thumbnail of each image, and bind the data using the v-for and v-bind instructions. We also added a method called openGallery that opens a Photoswipe image gallery and locates the image at a specific index. In this method, we use the instantiation method and goTo method in the PhotoSwipe library to control the displayed content.

Finally, we use scoped styles to ensure that the style of this component does not affect other places.

Step 3: Call the component

After completing the writing of the component, we can call the component anywhere on the page and pass in the corresponding data. For example:

<template>
  <div id="app">
    <Gallery :items="items"></Gallery>
  </div>
</template>

<script>
import Gallery from './components/Gallery'

export default {
  name: 'App',
  components: {
    Gallery
  },
  data () {
    return {
      items: [
        {
          src: 'image1-src.jpg',
          w: 1200,
          h: 800,
          title: 'Image1'
        },
        {
          src: 'image2-src.jpg',
          w: 1200,
          h: 800,
          title: 'Image2'
        }
      ]
    }
  }
}
</script>
Copy after login

In this example, we introduced the Gallery component in the App component and passed an array containing image information. By calling this component, we can display a set of images on the page, and users can click on the thumbnails to enter the Photoswipe image gallery to browse, zoom in, and out.

Conclusion

Through the above steps, we have successfully implemented a simple picture gallery component. Although the function of this component is relatively simple, it lays the foundation for solving image display problems. In the actual development process, in order to achieve more complex functions, we may need to further improve and expand this component. In any case, by using Vue and its rich ecosystem, I believe we will be able to create excellent image display effects.

The above is the detailed content of How to implement image gallery using Vue?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

React vs. Vue: Which Framework Does Netflix Use? React vs. Vue: Which Framework Does Netflix Use? Apr 14, 2025 am 12:19 AM

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

How to jump to the div of vue How to jump to the div of vue Apr 08, 2025 am 09:18 AM

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

See all articles