Vue3.0 handwritten carousel effect

Vue3.0 handwritten carousel effect

This article shares the specific code of Vue3.0 handwritten carousel effect for your reference. The specific content is as follows

Let's start

HTML structure

<template>
  <div class="xtx-carousel" @mouseleave="enterFn" @mouseenter="leaveFn">
    <ul class="carousel-body">
      <li class="carousel-item" :class="{ fade: indexid === index }" v-for="(item, index) in list" :key="item.id">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a href="javascript:;" class="carousel-btn prev" @click="lastPage"><i class="iconfont icon-angle-left"></i></a>
    <a href="javascript:;" class="carousel-btn next" @click="nextPage"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <span @click="indexid = i - 1" v-for="i in list.length" :key="i" :class="{ active: indexid === i - 1 }"></span>
    </div>
  </div>
</template>

js syntax

<script>
import { ref, watch, onUnmounted } from 'vue'
export default {
  name: 'Carousel',
  props: {
    // Image data list: {
      type: Object,
      default: () => {}
    },
    // Duration of each switching event of the carousel: {
      type: Number,
      default: 2
    },
    // Whether to enable autoplay of carousel: {
      type: Boolean,
      default: true
    },
    // Click to turn several pages: {
      type: Number,
      default: 1
    }
  },
  setup(props) {
    // Index const indexid = ref(0)
    // Carousel const timer = ref(null)
    const TimeFn = () => {
      clearInterval(timer)
      // Clear the previous timer before each execution timer.value = setInterval(() => {
        indexid.value++
        // If it exceeds the limit, refill if (indexid.value > props.list.length - 1) {
          indexid.value = 0
        }
      }, props.duration * 1000)
    }
    // Click + next stop picture const nextPage = () => {
      indexid.value += props.page
      if (indexid.value > props.list.length - 1) {
        indexid.value = 0
      }
    }
    // Click + previous picture const lastPage = () => {
      indexid.value -= props.page
      if (indexid.value < 0) {
        indexid.value = props.list.length - 1
      }
    }
    // Clear timer const leaveFn = () => {
      // console.log('clear timer')
      clearInterval(timer.value)
      // console.log(timer)
    }
    // Component consumption, cleanup timer onUnmounted(() => {
      clearInterval(timer.value)
    })
    // Start the timer const enterFn = () => {
      if (props.list.length > 1 && props.autoplay) {
        // console.log('Start timer')
        TimeFn()
      }
    }
    watch(
      () => props.list,
      () => {
        if (props.list.length > 1 && props.autoplay) {
          TimeFn()
        }
      }
    )
    return { indexid, leaveFn, enterFn, nextPage, lastPage }
  }
}
</script>

CSS Styles

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>

Register as a global plugin

import Carousel from '../carousel.vue'
// Vue2.0 plugin writing elements: export an object, have install function, pass in Vue constructor by default, extend Vue based on // Vue3.0 plugin writing elements: export an object, have install function, pass in app application instance by default, extend app based on export default {
  install(app) {
    // Expand on app, app provides component directive function // If you want to mount the prototype app.config.globalProperties method app.component(Carousel.name, xtxCarousel)
  }
}

Mount in main.js entry file

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import libraryUI from '@/components/library/index' //own plugin createApp(App)
  .use(store)
  .use(router)
  .use(libraryUI) // Mount the plugin.mount('#app')

How to use the plugin?

<Carousel :list="list" duration="1" />

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:
  • How to encapsulate the carousel component in Vue3
  • vue+rem custom carousel effect
  • Sample code of uniapp vue and nvue carousel components

<<:  Solution to the failure of entering the container due to full docker space

>>:  Tips for adding favicon to a website: a small icon in front of the URL

Recommend

React and Redux array processing explanation

This article will introduce some commonly used ar...

Using JS timer to move elements

Use JS timer to make an element to make a method ...

Mysql practical exercises simple library management system

Table of contents 1. Sorting function 2. Prepare ...

Echarts legend component properties and source code

The legend component is a commonly used component...

Teach you the detailed process of installing DOClever with Docker Compose

Table of contents 1. What is Docker Compose and h...

js canvas implements verification code and obtains verification code function

This article example shares the specific code of ...

Detailed explanation of the Docker deployment tutorial for Jenkins beginners

This article deploys Jenkins+Maven+SVN+Tomcat thr...

Do you know why vue data is a function?

Official website explanation: When a component is...

Manual and scheduled backup steps for MySQL database

Table of contents Manual backup Timer backup Manu...

Application of HTML and CSS in Flash

Application of HTML and CSS in Flash: I accidental...

Ideas and codes for implementing waterfall flow layout in uniapp applet

1. Introduction Is it considered rehashing old st...

DOM operation implementation in react

Table of contents Previous words Usage scenarios ...