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

MySQL database table partitioning considerations [recommended]

Table partitioning is different from database par...

Pure CSS3 realizes the effect of div entering and exiting in order

This article mainly introduces the effect of div ...

JavaScript to implement search data display

This article shares the data display code for Jav...

MySQL initialization password operation under Mac

A simple record of the database startup problems ...

Ubuntu basic settings: installation and use of openssh-server

Record the installation and use of openssh-server...

Tomcat source code analysis of Web requests and processing

Table of contents Preface 1. EndPoint 2. Connecti...

The corresponding attributes and usage of XHTML tags in CSS

When I first started designing web pages using XH...

MySQL scheduled database backup operation example

This article describes the example of MySQL sched...

Tutorial diagram of installing TomCat in Windows 10

Install TomCat on Windows This article will intro...

Solve the problem of insufficient docker disk space

After the server where Docker is located has been...

Vue+webrtc (Tencent Cloud) practice of implementing live broadcast function

Table of contents 1. Live broadcast effect 2. Ste...

React configuration px conversion rem method

Install related dependencies npm i lib-flexible -...

Detailed explanation of zabbix executing scripts or instructions on remote hosts

Scenario Requirements 1. We can use the script fu...