search
HomeWeb Front-endVue.jsHow to use Vue to implement an imitation photo wall component?

In modern web development, componentization is a very popular development model. Vue.js is a front-end framework that is very suitable for componentization. In this article, we’ll show you how to create a faux photo wall component using Vue.js.

Before we start, we need to clarify some preparations. First, we need to install Vue.js. The installation method is very simple, just enter the following command in the terminal:

npm install vue

After the installation is complete, we can create a Vue component named photo-wall. In the template of this component, we render a grid layout containing multiple photo cells. In the script section, we will load a set of photos and pass them to the template section in order to render out the photo wall component.

The following is the specific code implementation:

<template>
  <div class="photo-wall">
    <div class="row" v-for="row in rows">
      <div class="cell" v-for="cell in row">
        <img  class="{ selected: cell.selected } lazy"  src="/static/imghwm/default1.png"  data-src="cell.src"  : @click="selectCell(cell)" : alt="How to use Vue to implement an imitation photo wall component?" >
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      photos: [
        { id: 1, src: 'https://via.placeholder.com/300x200?text=Photo+1', selected: false },
        { id: 2, src: 'https://via.placeholder.com/300x200?text=Photo+2', selected: false },
        { id: 3, src: 'https://via.placeholder.com/300x200?text=Photo+3', selected: false },
        { id: 4, src: 'https://via.placeholder.com/300x200?text=Photo+4', selected: false },
        { id: 5, src: 'https://via.placeholder.com/300x200?text=Photo+5', selected: false },
        { id: 6, src: 'https://via.placeholder.com/300x200?text=Photo+6', selected: false },
        { id: 7, src: 'https://via.placeholder.com/300x200?text=Photo+7', selected: false },
        { id: 8, src: 'https://via.placeholder.com/300x200?text=Photo+8', selected: false },
        { id: 9, src: 'https://via.placeholder.com/300x200?text=Photo+9', selected: false },
        { id: 10, src: 'https://via.placeholder.com/300x200?text=Photo+10', selected: false },
        { id: 11, src: 'https://via.placeholder.com/300x200?text=Photo+11', selected: false },
        { id: 12, src: 'https://via.placeholder.com/300x200?text=Photo+12', selected: false },
        { id: 13, src: 'https://via.placeholder.com/300x200?text=Photo+13', selected: false },
        { id: 14, src: 'https://via.placeholder.com/300x200?text=Photo+14', selected: false },
        { id: 15, src: 'https://via.placeholder.com/300x200?text=Photo+15', selected: false },
        { id: 16, src: 'https://via.placeholder.com/300x200?text=Photo+16', selected: false },
        { id: 17, src: 'https://via.placeholder.com/300x200?text=Photo+17', selected: false },
        { id: 18, src: 'https://via.placeholder.com/300x200?text=Photo+18', selected: false }
      ],
      rows: []
    }
  },
  mounted() {
    this.loadPhotos()
  },
  methods: {
    loadPhotos() {
      let rowCount = Math.ceil(this.photos.length / 6)
      let photosIndex = 0
      for (let i = 0; i < rowCount; i++) {
        let row = []
        for (let j = 0; j < 6; j++) {
          if (photosIndex >= this.photos.length) break
          row.push(this.photos[photosIndex])
          photosIndex++
        }
        this.rows.push(row)
      }
    },
    selectCell(cell) {
      cell.selected = !cell.selected
    }
  }
}
</script>

<style>
.photo-wall {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  height: 500px;
  padding: 20px;
}

.cell {
  width: calc(33.33% - 10px);
  margin-bottom: 10px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  cursor: pointer;
}

.cell img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.selected {
  border: 3px solid #007aff;
}
</style>

In this example, we first define a photos array, which contains all the photo information to be displayed, including The id, src, and selected attributes. We also define the rows array, which is used to process and display the rows and columns of the photo wall in the Vue component.

In the mounted hook function, we called the loadPhotos method. This method is responsible for processing the photos array and converting it into a two-dimensional array form, and finally displaying it in the grid layout.

selectCell The method is used to handle the event when the user clicks on the photo in the cell. By modifying the selected attribute, we can switch the selected state of the image very simply.

Finally, in the style part of the component, we define style rules to control the width, height and other properties of the grid layout itself, as well as the styles of the contained cells.

Using the above code, we can implement a simple Vue.js photo wall component. By adding more interactions and effects to this component, we can create richer, more efficient web applications.

The above is the detailed content of How to use Vue to implement an imitation photo wall component?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What Happens When the Vue.js Virtual DOM Detects a Change?What Happens When the Vue.js Virtual DOM Detects a Change?May 14, 2025 am 12:12 AM

WhentheVue.jsVirtualDOMdetectsachange,itupdatestheVirtualDOM,diffsit,andappliesminimalchangestotherealDOM.ThisprocessensureshighperformancebyavoidingunnecessaryDOMmanipulations.

How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?May 13, 2025 pm 04:05 PM

Vue.js' VirtualDOM is both a mirror of the real DOM, and not exactly. 1. Create and update: Vue.js creates a VirtualDOM tree based on component definitions, and updates VirtualDOM first when the state changes. 2. Differences and patching: Comparison of old and new VirtualDOMs through diff operations, and apply only the minimum changes to the real DOM. 3. Efficiency: VirtualDOM allows batch updates, reduces direct DOM operations, and optimizes the rendering process. VirtualDOM is a strategic tool for Vue.js to optimize UI updates.

Vue.js vs. React: Scalability and MaintainabilityVue.js vs. React: Scalability and MaintainabilityMay 10, 2025 am 12:24 AM

Vue.js and React each have their own advantages in scalability and maintainability. 1) Vue.js is easy to use and is suitable for small projects. The Composition API improves the maintainability of large projects. 2) React is suitable for large and complex projects, with Hooks and virtual DOM improving performance and maintainability, but the learning curve is steeper.

The Future of Vue.js and React: Trends and PredictionsThe Future of Vue.js and React: Trends and PredictionsMay 09, 2025 am 12:12 AM

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

Netflix's Frontend: A Deep Dive into Its Technology StackNetflix's Frontend: A Deep Dive into Its Technology StackMay 08, 2025 am 12:11 AM

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js and the Frontend: Building Interactive User InterfacesVue.js and the Frontend: Building Interactive User InterfacesMay 06, 2025 am 12:02 AM

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

What are the disadvantages of VueJs?What are the disadvantages of VueJs?May 05, 2025 am 12:06 AM

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix: Unveiling Its Frontend FrameworksNetflix: Unveiling Its Frontend FrameworksMay 04, 2025 am 12:16 AM

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

See all articles

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 Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)