最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3.0手寫輪播圖效果

 更新時(shí)間:2021年10月28日 17:12:44   作者:Youyzq  
這篇文章主要為大家詳細(xì)介紹了Vue3.0手寫輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Vue3.0手寫輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下

讓我們開始把

html結(jié)構(gòu)

<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語法

<script>
import { ref, watch, onUnmounted } from 'vue'
export default {
  name: 'Carousel',
  props: {
    // 圖片數(shù)據(jù)
    list: {
      type: Object,
      default: () => {}
    },
    // 輪播圖每張切換的事件
    duration: {
      type: Number,
      default: 2
    },
    // 是否開啟輪播圖
    autoplay: {
      type: Boolean,
      default: true
    },
    // 點(diǎn)擊翻幾張
    page: {
      type: Number,
      default: 1
    }
  },
  setup(props) {
    // 索引
    const indexid = ref(0)
    // 輪播
    const timer = ref(null)
    const TimeFn = () => {
      clearInterval(timer)
      // 每次執(zhí)行前都清除上一次的定時(shí)器
      timer.value = setInterval(() => {
        indexid.value++
        // 如果超出界限就重新回填
        if (indexid.value > props.list.length - 1) {
          indexid.value = 0
        }
      }, props.duration * 1000)
    }
    // 點(diǎn)擊+下一站圖片
    const nextPage = () => {
      indexid.value += props.page
      if (indexid.value > props.list.length - 1) {
        indexid.value = 0
      }
    }
    // 點(diǎn)擊+上一張圖片
    const lastPage = () => {
      indexid.value -= props.page
      if (indexid.value < 0) {
        indexid.value = props.list.length - 1
      }
    }
    // 清除定時(shí)器
    const leaveFn = () => {
      // console.log('清除定時(shí)器')
      clearInterval(timer.value)
      // console.log(timer)
    }
    // 組件消耗,清理定時(shí)器
    onUnmounted(() => {
      clearInterval(timer.value)
    })
    // 開啟定時(shí)器
    const enterFn = () => {
      if (props.list.length > 1 && props.autoplay) {
        // console.log('開啟定時(shí)器')
        TimeFn()
      }
    }
    watch(
      () => props.list,
      () => {
        if (props.list.length > 1 && props.autoplay) {
          TimeFn()
        }
      }
    )
    return { indexid, leaveFn, enterFn, nextPage, lastPage }
  }
}
</script>

css樣式

<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>

注冊(cè)成全局插件

import Carousel from '../carousel.vue'
// vue2.0插件寫法要素:導(dǎo)出一個(gè)對(duì)象,有install函數(shù),默認(rèn)傳入了Vue構(gòu)造函數(shù),Vue基礎(chǔ)之上擴(kuò)展
// vue3.0插件寫法要素:導(dǎo)出一個(gè)對(duì)象,有install函數(shù),默認(rèn)傳入了app應(yīng)用實(shí)例,app基礎(chǔ)之上擴(kuò)展

export default {
  install(app) {
    // 在app上進(jìn)行擴(kuò)展,app提供 component directive 函數(shù)
    // 如果要掛載原型 app.config.globalProperties 方式
    app.component(Carousel.name, xtxCarousel)
  }
}

在main.js入口文件掛載

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import libraryUI from '@/components/library/index' //自己的插件
createApp(App)
  .use(store)
  .use(router)
  .use(libraryUI) // 掛載插件
  .mount('#app')

如何使用插件呢?

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。 

相關(guān)文章

  • vue動(dòng)態(tài)配置模板 ''component is''代碼

    vue動(dòng)態(tài)配置模板 ''component is''代碼

    這篇文章主要介紹了vue動(dòng)態(tài)配置模板 'component is'代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 通過Vue實(shí)現(xiàn)Excel文件的上傳和預(yù)覽功能

    通過Vue實(shí)現(xiàn)Excel文件的上傳和預(yù)覽功能

    在業(yè)務(wù)系統(tǒng)中,Excel 文件作為一種常用的數(shù)據(jù)存儲(chǔ)和傳輸格式,經(jīng)常需要被處理和展示,這篇文章將講解如何通過 Vue 和 xlsx.js 實(shí)現(xiàn) Excel 文件的上傳和預(yù)覽功能,需要的朋友可以參考下
    2025-04-04
  • 使用Vue3實(shí)現(xiàn)交互式雷達(dá)圖的代碼實(shí)現(xiàn)

    使用Vue3實(shí)現(xiàn)交互式雷達(dá)圖的代碼實(shí)現(xiàn)

    雷達(dá)圖是一種可視化數(shù)據(jù)的方式,用于比較多個(gè)類別中不同指標(biāo)的相對(duì)值,它適用于需要展示多個(gè)指標(biāo)之間的關(guān)系和差異的場(chǎng)景,本文給大家介紹了如何用Vue3輕松創(chuàng)建交互式雷達(dá)圖,需要的朋友可以參考下
    2024-06-06
  • vue仿element實(shí)現(xiàn)分頁器效果

    vue仿element實(shí)現(xiàn)分頁器效果

    這篇文章主要介紹了vue仿element實(shí)現(xiàn)分頁器效果,實(shí)現(xiàn)思路是定一個(gè)值foldPage, 意為當(dāng)前最多顯示的標(biāo)簽數(shù),當(dāng)總頁數(shù)超過即顯示省略.省略分為左邊省略(folder1)和右邊省略(folder2),具體實(shí)例代碼大家參考下本文
    2018-09-09
  • 詳解vue中computed 和 watch的異同

    詳解vue中computed 和 watch的異同

    本篇文章主要介紹了vue中computed 和 watch的異同,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • VUE 自定義取色器組件的實(shí)現(xiàn)

    VUE 自定義取色器組件的實(shí)現(xiàn)

    本文主要介紹了VUE 自定義取色器組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • vue3.0報(bào)錯(cuò)Cannot?find?module‘worker_threads‘的解決辦法

    vue3.0報(bào)錯(cuò)Cannot?find?module‘worker_threads‘的解決辦法

    這篇文章介紹了vue3.0報(bào)錯(cuò)Cannot?find?module‘worker_threads‘的解決辦法。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • Vue3中實(shí)現(xiàn)代碼高亮的兩種方法(prismjs和highlight.js)

    Vue3中實(shí)現(xiàn)代碼高亮的兩種方法(prismjs和highlight.js)

    最近忙著開發(fā)自己的博客系統(tǒng),在做界面展示的時(shí)候,需要讓代碼高亮,于是經(jīng)過在網(wǎng)上查閱,發(fā)現(xiàn)有兩款比較好用的插件實(shí)現(xiàn)代碼高亮,分別是prismjs和highlight.js,下面我分別介紹下,方便給需要的同學(xué)參考
    2025-04-04
  • vue動(dòng)態(tài)路由實(shí)現(xiàn)多級(jí)嵌套面包屑的思路與方法

    vue動(dòng)態(tài)路由實(shí)現(xiàn)多級(jí)嵌套面包屑的思路與方法

    在實(shí)際項(xiàng)目中我們會(huì)碰到多層嵌套的組件組合而成,比如我們常見的面包屑導(dǎo)航,下面這篇文章就來給大家介紹關(guān)于vue實(shí)現(xiàn)動(dòng)態(tài)路由多級(jí)嵌套面包屑的思路與方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • 詳解vue3的沙箱機(jī)制

    詳解vue3的沙箱機(jī)制

    這篇文章主要介紹了vue3的沙箱機(jī)制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-04-04

最新評(píng)論

勐海县| 海丰县| 中山市| 易门县| 启东市| 十堰市| 称多县| 梅河口市| 沙田区| 高邮市| 巢湖市| 汉源县| 专栏| 额尔古纳市| 洪湖市| 桦甸市| 临清市| 齐齐哈尔市| 婺源县| 疏勒县| 绥化市| 莎车县| 莱芜市| 治多县| 乌兰浩特市| 勐海县| 太仆寺旗| 青河县| 阜宁县| 双峰县| 申扎县| 张家川| 孟津县| 保靖县| 宕昌县| 牙克石市| 章丘市| 五河县| 哈尔滨市| 宝应县| 讷河市|