Vue achieves seamless carousel effect

Vue achieves seamless carousel effect

This article shares the specific code of Vue to achieve seamless carousel effect for your reference. The specific content is as follows

Code

1. Subcomponent code

The code is as follows (example):

<template>
  <div>
    <div class="box" @mouseenter="mouse" @mouseleave="mouseleave">
      <ul class="box1">
        <li>
          <img
            :src="n"
            v-for="(n, i) in imgs"
            :key="i"
            alt=""
            :style="{ left: (i - index) * 500 + 'px' }"
            :class="hasAni ? 'animaton' : ''"
          />
        </li>
      </ul>
      <p class="tt" @click="left">&lt;</p>
      <p class="tt1" @click="right">></p>
    </div>
  </div>
</template>

The script code is as follows (example):

<script>
export default {
  name: "Lunbo",
  props: ["imgs"],
  data() {
    return {
      // To use images in js, you need to use require to import index: 1,
      hasAni: true,
      istrue: true,
    };
  },
  methods: {
    mouse() {
      clearInterval(this.timer);
    },
    mouseleave() {
      this.timer = setInterval(() => {
        this.index++;
        this.hasAni = true;
        if (this.index == this.imgs.length - 1) {
          setTimeout(() => {
            this.index = 0;
            this.hasAni = false;
          }, 750);
        }
      }, 1500);
    },
    right() {
      if (this.istrue) {
        this.index++;
        this.hasAni = true;
        this.istrue = false;
        if (this.index == this.imgs.length - 1) {
          setTimeout(() => {
            this.index = 1;
            this.hasAni = false;
          }, 750);
        }
        setTimeout(() => {
          this.istrue = true;
        }, 1000);
      }
    },
    left() {
       if (this.istrue) {
        this.index--;
        this.hasAni = true;
        this.istrue = false;
        if (this.index == 0) {
          setTimeout(() => {
            this.index = this.imgs.length - 1;
            this.hasAni = false;
          }, 750);
        }
        setTimeout(() => {
          this.istrue = true;
        }, 1000);
      }
    },
  },
  activated() {
    console.log(1);
    this.timer = setInterval(() => {
      this.index++;
      this.hasAni = true;
      if (this.index == this.imgs.length - 1) {
        setTimeout(() => {
          this.index = 0;
          this.hasAni = false;
        }, 750);
      }
    }, 1500);
  },
  decativated() {
    clearInterval(this.timer);
  },
};
</script>

CSS

<style scoped>
p {
  width: 30px;
  height: 60px;
  background-color: rgba(46, 139, 86, 0.356);
  line-height: 60px;
  font-size: 24px;
  position: absolute;
  top: 105px;
}
.tt {
  left: 0;
}
.tt1 {
  right: 0;
}
.box {
  width: 500px;
  height: 300px;
  margin: 100px auto;
  position: relative;
  overflow: hidden;
}
.box1 img {
  position: absolute;
  left: 0px;
  top: 0;
  width: 500px;
  height: 300px;
}
.animaton {
  transition: left 0.75s;
}
</style>

2. Parent component code

Parent Component

<keep-alive>
      <Lunbo :imgs="imgs" />
</keep-alive>

Importing modules

import Lunbo from "./components/Lunbo";

Image Data

data() {
    return {
      imgs:[
        require("./assets/6.jpg"),
        require("./assets/1.jpg"),
        require("./assets/2.jpg"),
        require("./assets/3.jpg"),
        require("./assets/4.jpg"),
        require("./assets/5.jpg"),
        require("./assets/6.jpg"),
        require("./assets/1.jpg"),
      ],
    }

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the idea of ​​using Vue to create a picture carousel component
  • Realizing the image carousel effect based on vue.js
  • Implementing carousel chart based on vue.js carousel component vue-awesome-swiper
  • Vue transition to achieve carousel effect
  • Detailed tutorial on referencing the swiper carousel plugin in vue
  • Vue.js integrates the carousel example code in mint-ui
  • Vue.js implements simple carousel effect
  • Use of vue-concise-slider, a plug-in for vue carousel
  • An example of how to use Vue to implement a mobile image carousel component
  • vue.js+elementUI realizes the function of switching avatars by clicking left and right arrows (similar to the effect of carousel pictures)

<<:  How to build a standardized vmware image for kubernetes under rancher

>>:  Mysql5.7.14 Linux version password forgotten perfect solution

Recommend

Two ways to implement square div using CSS

Goal: Create a square whose side length is equal ...

Sample code for changing the color of a png image through a CSS3 filter

This method uses the drop-shadow filter in CSS3 t...

Detailed analysis of SQL execution steps

Detailed analysis of SQL execution steps Let'...

The best solution for implementing digital plus and minus buttons with pure CSS

Preface: For the implementation of digital additi...

Lombok implementation JSR-269

Preface Introduction Lombok is a handy tool, just...

3D tunnel effect implemented by CSS3

The effect achievedImplementation Code html <d...

Detailed explanation of Linux text processing tools

1. Count the number of users whose default shell ...

Example code for text origami effect using CSS3

Preface This article mainly shares with you an ex...

mysql5.6.zip format compressed version installation graphic tutorial

Preface: MySQL is a relational database managemen...

JS implements jQuery's append function

Table of contents Show Me The Code Test the effec...

Docker commands are implemented so that ordinary users can execute them

After installing docker, there will usually be a ...

Vue implements Modal component based on Teleport

Table of contents 1. Get to know Teleport 2. Basi...

Solution to the problem of session failure caused by nginx reverse proxy

A colleague asked for help: the login to the back...

How to write beautiful HTML code

What Beautiful HTML Code Looks Like How to write ...