vue3封裝輪播圖組件功能的完整步驟
需求:
封裝一個(gè)輪播圖組件
功能:
1.有上下切換的點(diǎn)擊圖標(biāo),可以實(shí)現(xiàn)點(diǎn)擊切換上下張
2.底部有小圓點(diǎn),對(duì)應(yīng)每一張圖片,圖片切換,小圓點(diǎn)對(duì)應(yīng)切換
3.小圓點(diǎn)可以點(diǎn)擊切換,對(duì)應(yīng)的圖片也會(huì)切換
功能實(shí)現(xiàn)
父組件通過自定義屬性傳值傳遞包含圖片的數(shù)組變量,
子組件通過prorps接收數(shù)組,循環(huán)渲染數(shù)組中的圖片
新建公共組件src/components/carousel/index.vue
html部分:
- 輪播圖圖片部分由ul套li套img構(gòu)成
- 上一張下一張圖片由
- 底部小圓點(diǎn)
<template> <div class="home-banner"> <div class="carousel"> <!-- 輪播圖圖片部分 --> <ul class="carousel-body"> <li class="carousel-item fade"> <img src="" alt="" /> </li> </ul> <!-- 上一張下一張箭頭圖標(biāo) --> <a href=" javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a> <a href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a> <!-- 底部小圓點(diǎn) --> <div class="carousel-indicator"> <span class="active"></span> <span></span> <span></span> <span></span> <span></span> </div> </div> </div> </template>
css部分
- 輪播圖切換通過類名fade的opacity實(shí)現(xiàn)
- 加上transition使變化更加順滑
- 給span設(shè)置border-radius變成小圓點(diǎn)
<style scoped lang="less"> .home-banner { width: 1240px; height: 500px; position: absolute; left: 0; top: 0; z-index: 98; background-color: pink; } .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>
父組件中引入輪播圖組件,傳入數(shù)組
1.導(dǎo)入輪播圖組件,頁面使用組件名便簽(此處為局部組件注冊(cè),可以注冊(cè)為全局組件)
2.模擬輪播圖數(shù)據(jù),對(duì)象數(shù)組,每個(gè)對(duì)象有圖片數(shù)據(jù)imageUrl和id
3.通過自定義屬性把輪播圖數(shù)據(jù)傳遞給子組件
父組件部分
<script setup lang='ts'> import Carousel from "@/components/carousel/index.vue" import { ref } from "vue"; const bannerList = ref([ { imageUrl: "1.jpg", id: 1 }, { imageUrl: "2.jpg", id: 2 }, { imageUrl: "3.jpg", id: 3 }, { imageUrl: "4.jpg", id: 4 }, { imageUrl: "5.jpg", id: 5 } ]) </script> <template> <Carousel :slides="bannerList" /> </template> <style scoped> </style>
子組件接收父組件數(shù)據(jù)并渲染
- v-for循環(huán)渲染圖片.key值綁定item.id
- v-for循環(huán)渲染底部小圓點(diǎn)span,使小圓點(diǎn)個(gè)數(shù)與圖片個(gè)數(shù)相同
<script lang="ts" setup name="Carousel"> const { slides } = defineProps<{ slides: [] }>() </script> .....省略 <!-- 輪播圖圖片 --> <ul class="carousel-body"> <li class="carousel-item " fade v-for="(item, index) in slides" :key="item.id"> <img :src="item.imageUrl" alt="" /> </li> </ul> ......省略 <!-- 底部小圓點(diǎn) --> <div class="carousel-indicator"> <span v-for="(item, index) in slides" active :key="item.id"></span> </div>
實(shí)現(xiàn)點(diǎn)擊上下張圖標(biāo)切換圖片,點(diǎn)擊底部小圓點(diǎn)切換圖片
點(diǎn)擊上下張圖標(biāo)切換圖片:
- 給上下圖表注冊(cè)事件pre和next
- 定義一個(gè)變量active記錄當(dāng)前顯示圖片的下標(biāo)
- 判斷active記錄的下標(biāo)和當(dāng)前下標(biāo)是否一致來顯示圖片(fade類名 )
點(diǎn)擊底部小圓點(diǎn)切換圖片:
- 給每一個(gè)span注冊(cè)點(diǎn)擊事件,點(diǎn)擊時(shí)把當(dāng)前小圓點(diǎn)index賦值給active
- active變化,由于active是響應(yīng)式數(shù)據(jù),所以圖片變化為和active相同的index圖片(fade類名實(shí)現(xiàn))
- 小圓點(diǎn)實(shí)現(xiàn)高亮變化同樣依靠類名實(shí)現(xiàn):class="{ active: active === index }",當(dāng)active和當(dāng)前index相同時(shí)獲得active類名,小圓點(diǎn)變?yōu)榘咨?/li>
<script lang="ts" setup name="Carousel"> import { ref } from 'vue'; const { slides } = defineProps<{ slides: [] }>() //高亮下標(biāo) const active = ref(0) // 上一張下一張 const prev = () => { active.value-- if (active.value < 0) { active.value = slides.length - 1 } } const next = () => { active.value++ if (active.value > slides.length - 1) { active.value = 0 } } </script <template> <div class="carousel" @mouseenter="stop" @mouseleave="start"> <!-- 輪播圖圖片 --> <ul class="carousel-body"> <li class="carousel-item " :class="{ fade: active === index }" v-for="(item, index) in slides" :key="item.id"> <RouterLink :to="item.hrefUrl"> <img :src="item.imgUrl" alt="" /> </RouterLink> </li> </ul> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn prev" @click="prev"><i class="iconfont icon-angle-left"></i></a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn next" @click="next"><i class="iconfont icon-angle-right"></i></a> <!-- 底部小圓點(diǎn) --> <div class="carousel-indicator"> <span v-for="(item, index) in slides" @click="active=index" :class="{ active: active === index }" :key="item.id"></span> </div> </div> </template>
總結(jié)
組件傳值
封裝組件可以通過父子組件傳值,插槽傳值,實(shí)現(xiàn)數(shù)據(jù)傳遞
輪播圖實(shí)現(xiàn)無限滾動(dòng)
我們需要在圖片到達(dá)臨界時(shí)對(duì)index進(jìn)行處理,實(shí)現(xiàn)圖片首尾銜接
- 當(dāng)index小于零時(shí),把index賦值為最后的index
- 當(dāng)index大于最后的index時(shí),把index值賦值為0
// 上一張下一張 const prev = () => { active.value-- if (active.value < 0) { active.value = slides.length - 1 } } const next = () => { active.value++ if (active.value > slides.length - 1) { active.value = 0 } }
實(shí)現(xiàn)小圓點(diǎn)點(diǎn)擊高亮切換
- 首先設(shè)置一個(gè)active高亮類,加了這個(gè)類則對(duì)應(yīng)小圓點(diǎn)高亮
- 設(shè)置一個(gè)active記錄當(dāng)前選中的小圓點(diǎn)的index值,當(dāng)active和當(dāng)前index相同時(shí)對(duì)應(yīng)的span獲得active類名
:class="{ active: active === index }"
- 給每一個(gè)span注冊(cè)點(diǎn)擊事件,當(dāng)點(diǎn)擊時(shí),更改active值為點(diǎn)擊的小圓點(diǎn)的index
@click="active=index"
總結(jié)
到此這篇關(guān)于vue3封裝輪播圖組件功能的文章就介紹到這了,更多相關(guān)vue3封裝輪播圖組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
elementUI Pagination 分頁指定最大頁的問題及解決方法(page-count)
項(xiàng)目中遇到數(shù)據(jù)量大,查詢的字段多,但用戶主要使用的是最近的一些數(shù)據(jù),1萬條以后的數(shù)據(jù)一般不使用,這篇文章主要介紹了elementUI Pagination 分頁指定最大頁的問題及解決方法(page-count),需要的朋友可以參考下2024-08-08如何利用vue3實(shí)現(xiàn)一個(gè)俄羅斯方塊
俄羅斯方塊這個(gè)游戲相信大家都玩過,下面這篇文章主要給大家介紹了關(guān)于如何利用vue3實(shí)現(xiàn)一個(gè)俄羅斯方塊的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01vue實(shí)現(xiàn)移動(dòng)端div拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動(dòng)端div拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vue自動(dòng)路由-單頁面項(xiàng)目(非build時(shí)構(gòu)建)
這篇文章主要介紹了vue自動(dòng)路由-單頁面項(xiàng)目(非build時(shí)構(gòu)建),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04Vue3?KeepAlive實(shí)現(xiàn)原理解析
KeepAlive?是一個(gè)內(nèi)置組件,那封裝一個(gè)組件對(duì)于大家來說應(yīng)該不會(huì)有太大的困難,它的核心邏輯在于它的?render?函數(shù),它用?map?去記錄要緩存的組件,就是?[key,vnode]?的形式,這篇文章主要介紹了Vue3?KeepAlive實(shí)現(xiàn)原理,需要的朋友可以參考下2022-09-09vue中動(dòng)態(tài)設(shè)置meta標(biāo)簽和title標(biāo)簽的方法
這篇文章主要介紹了vue中動(dòng)態(tài)設(shè)置meta標(biāo)簽和title標(biāo)簽的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07Vue3 Composition API的使用簡(jiǎn)介
這篇文章主要介紹了Vue3 Composition API的使用簡(jiǎn)介,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下2021-03-03深入分析element ScrollBar滾動(dòng)組件源碼
這篇文章主要介紹了element ScrollBar滾動(dòng)組件源碼深入分析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01