Table of Contents
Card Title
Home Web Front-end Vue.js How to use Vue to achieve 3D stereo effect

How to use Vue to achieve 3D stereo effect

Nov 07, 2023 pm 04:51 PM
vue 3D effect d

How to use Vue to achieve 3D stereo effect

How to use Vue to achieve 3D stereoscopic effects

Vue is a front-end framework that can be used to build highly interactive single-page applications. Implementing 3D stereoscopic effects in Vue can add dynamics and visual effects to web pages. This article will introduce how to use Vue to achieve 3D stereoscopic effects and provide some specific code examples.

1. Preparation

Before we start, we need to make sure that Vue has been installed. If it is not installed yet, you can install it through the instructions on the official website (https://vuejs.org/).

2. Use CSS3 to achieve 3D effect

To achieve 3D stereoscopic effect in Vue, we mainly achieve it by using the transform attribute of CSS3. The transform attribute can change the shape, size and position of an element. The following is a simple Vue component that uses CSS3 to achieve 3D effects:

<template>
  <div class="container">
    <div class="card">
      <img src="/static/imghw/default1.png"  data-src="image.jpg"  class="lazy" alt="Image" />
      <div class="text">
        <h2 id="Card-Title">Card Title</h2>
        <p>Card Description</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Card",
};
</script>

<style>
.container {
  perspective: 1000px;  // 设置透视视角
  width: 300px;
  height: 200px;
}

.card {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;  // 设置元素的变换样式为3D
  transition: transform 0.5s;
}

.card:hover {
  transform: rotateY(180deg);  // 当鼠标悬停时,元素绕Y轴旋转180度
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  backface-visibility: hidden;  // 设置图像在背面时不可见
}

.text {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;  // 设置文本在背面时不可见
  transform: rotateY(180deg);  // 默认显示背面
  background-color: rgba(0, 0, 0, 0.5);
  text-align: center;
  color: #fff;
  padding: 20px;
}

h2, p {
  margin: 0;
  padding: 0;
}
</style>
Copy after login

In this Vue component, we use the transform property of CSS3 to achieve 3D effects. When the mouse is hovered over the card, the card rotates 180 degrees around the Y-axis, showing the reverse side. When the mouse leaves the card, the card returns to its default state.

3. Use JavaScript to achieve interactive effects

In addition to using CSS3, we can also use JavaScript to achieve more complex interactive effects. In Vue, you can use the component's life cycle hook function and combine it with JavaScript for more flexible operations. The following is an example of using Vue and JavaScript to achieve a 3D three-dimensional effect:

<template>
  <div class="container">
    <div class="cube" ref="cube">
      <div class="side" v-for="(color, index) in colors" :key="index" :style="{ backgroundColor: color }"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Cube",
  data() {
    return {
      colors: ["red", "green", "blue", "yellow", "purple", "orange"],
    };
  },
  mounted() {
    const cube = this.$refs.cube;
    let rotateX = 0;
    let rotateY = 0;

    cube.addEventListener("mousemove", (event) => {
      rotateX = (event.clientY / window.innerHeight - 0.5) * 90;
      rotateY = (event.clientX / window.innerWidth - 0.5) * 90;

      this.rotateCube(rotateX, rotateY);
    });

    cube.addEventListener("mouseout", () => {
      rotateX = 0;
      rotateY = 0;

      this.rotateCube(rotateX, rotateY);
    });
  },
  methods: {
    rotateCube(rotateX, rotateY) {
      const cube = this.$refs.cube;
      cube.style.transform = `rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
    },
  },
};
</script>

<style>
.container {
  perspective: 1000px;
  width: 300px;
  height: 300px;
}

.cube {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transform-origin: center center;
  transition: transform 0.5s;
}

.side {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0.9;
}

.side:nth-child(1) { transform: translateZ(150px); }
.side:nth-child(2) { transform: rotateY(90deg) translateZ(150px); }
.side:nth-child(3) { transform: rotateY(180deg) translateZ(150px); }
.side:nth-child(4) { transform: rotateY(270deg) translateZ(150px); }
.side:nth-child(5) { transform: rotateX(90deg) translateZ(150px); }
.side:nth-child(6) { transform: rotateX(-90deg) translateZ(150px); }
</style>
Copy after login

In this example, we use a div element as a cube, set up 6 faces, and listen to mouse movement events through JavaScript. to calculate the position of the mouse on the screen and change the rotation angle of the cube. By changing the value of the transform attribute, the rotation effect of the cube is achieved.

Summary

Using Vue to achieve 3D stereoscopic effects can add dynamics and visual effects to web pages. This article introduces how to use CSS3 and JavaScript to achieve 3D effects, and provides some specific code examples for reference. I hope this article can help you and enable you to better use Vue to achieve 3D stereoscopic effects.

The above is the detailed content of How to use Vue to achieve 3D stereo effect. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

From PHP to Go or Front-end? The suggestions and confusions of reality from experienced people From PHP to Go or Front-end? The suggestions and confusions of reality from experienced people Apr 01, 2025 pm 02:12 PM

Confusion and the cause of choosing from PHP to Go Recently, I accidentally learned about the salary of colleagues in other positions such as Android and Embedded C in the company, and found that they are more...

What are the AI ​​tools for mock interviews? What are the AI ​​tools for mock interviews? Nov 28, 2024 pm 09:52 PM

Mock interview AI tools are valuable tools for efficient candidate screening, saving recruiters time and effort. These tools include HireVue, Talview, Interviewed, iCIMS Video, and Eightfold AI. They provide automated, session-based assessments with benefits including efficiency, consistency, objectivity and scalability. When choosing a tool, recruiters should consider integrations, user-friendliness, accuracy, pricing, and support. Mock interviewing AI tools improve hiring speed, decision quality, and candidate experience.

How to find the right training program for programmers' entry-level skills? How to find the right training program for programmers' entry-level skills? Apr 01, 2025 am 11:30 AM

Programmers' "tickling" needs: From leisure to practice, this programmer friend has been a little idle recently and wants to improve his skills and achieve success through some small projects...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

How to manually trigger the onBlur event of a cell in Avue-crud row editing mode? How to manually trigger the onBlur event of a cell in Avue-crud row editing mode? Apr 04, 2025 pm 02:00 PM

The onBlur event that implements Avue-crud row editing in the Avue component library manually triggers the Avue-crud component. It provides convenient in-line editing functions, but sometimes we need to...

Vue Element uploads large files online errors: How to troubleshoot cross-domain problems and other potential causes? Vue Element uploads large files online errors: How to troubleshoot cross-domain problems and other potential causes? Apr 01, 2025 pm 12:48 PM

Vue Elementel-upload upload file online error reporting and troubleshooting using Vue and Element...

How to monetize programmer skills? From being 'difficult to panic' to taking orders and making money, what are the practical ways? How to monetize programmer skills? From being 'difficult to panic' to taking orders and making money, what are the practical ways? Apr 01, 2025 am 08:27 AM

Programmers’ daily worries and skills monetization: from “I’m so idle” to “helpful” Recently, a programmer friend posted on a forum, expressing “I’m so idle…

Is the convergence of the technology stack the selection of the technology stack? Is the convergence of the technology stack the selection of the technology stack? Apr 02, 2025 pm 04:42 PM

Title: The relationship between technology stack convergence and selection: Does technology stack convergence refer to the selection of technology stack? I saw an article that has a convergence technology stack...

See all articles