vue3封裝輪播圖組件功能的完整步驟
需求:
封裝一個輪播圖組件
功能:
1.有上下切換的點擊圖標,可以實現(xiàn)點擊切換上下張
2.底部有小圓點,對應每一張圖片,圖片切換,小圓點對應切換
3.小圓點可以點擊切換,對應的圖片也會切換
功能實現(xiàn)
父組件通過自定義屬性傳值傳遞包含圖片的數(shù)組變量,
子組件通過prorps接收數(shù)組,循環(huán)渲染數(shù)組中的圖片
新建公共組件src/components/carousel/index.vue
html部分:
- 輪播圖圖片部分由ul套li套img構(gòu)成
- 上一張下一張圖片由
- 底部小圓點
<template>
<div class="home-banner">
<div class="carousel">
<!-- 輪播圖圖片部分 -->
<ul class="carousel-body">
<li class="carousel-item fade">
<img src="" alt="" />
</li>
</ul>
<!-- 上一張下一張箭頭圖標 -->
<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>
<!-- 底部小圓點 -->
<div class="carousel-indicator">
<span class="active"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
</div>
</template>css部分
- 輪播圖切換通過類名fade的opacity實現(xiàn)
- 加上transition使變化更加順滑
- 給span設置border-radius變成小圓點
<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.導入輪播圖組件,頁面使用組件名便簽(此處為局部組件注冊,可以注冊為全局組件)
2.模擬輪播圖數(shù)據(jù),對象數(shù)組,每個對象有圖片數(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)渲染底部小圓點span,使小圓點個數(shù)與圖片個數(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>
......省略
<!-- 底部小圓點 -->
<div class="carousel-indicator">
<span v-for="(item, index) in slides" active :key="item.id"></span>
</div>實現(xiàn)點擊上下張圖標切換圖片,點擊底部小圓點切換圖片
點擊上下張圖標切換圖片:
- 給上下圖表注冊事件pre和next
- 定義一個變量active記錄當前顯示圖片的下標
- 判斷active記錄的下標和當前下標是否一致來顯示圖片(fade類名 )
點擊底部小圓點切換圖片:
- 給每一個span注冊點擊事件,點擊時把當前小圓點index賦值給active
- active變化,由于active是響應式數(shù)據(jù),所以圖片變化為和active相同的index圖片(fade類名實現(xiàn))
- 小圓點實現(xiàn)高亮變化同樣依靠類名實現(xiàn):class="{ active: active === index }",當active和當前index相同時獲得active類名,小圓點變?yōu)榘咨?/li>
<script lang="ts" setup name="Carousel">
import { ref } from 'vue';
const { slides } = defineProps<{
slides: []
}>()
//高亮下標
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>
<!-- 底部小圓點 -->
<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é)
組件傳值
封裝組件可以通過父子組件傳值,插槽傳值,實現(xiàn)數(shù)據(jù)傳遞
輪播圖實現(xiàn)無限滾動
我們需要在圖片到達臨界時對index進行處理,實現(xiàn)圖片首尾銜接
- 當index小于零時,把index賦值為最后的index
- 當index大于最后的index時,把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
}
}實現(xiàn)小圓點點擊高亮切換
- 首先設置一個active高亮類,加了這個類則對應小圓點高亮
- 設置一個active記錄當前選中的小圓點的index值,當active和當前index相同時對應的span獲得active類名
:class="{ active: active === index }"- 給每一個span注冊點擊事件,當點擊時,更改active值為點擊的小圓點的index
@click="active=index"
總結(jié)
到此這篇關(guān)于vue3封裝輪播圖組件功能的文章就介紹到這了,更多相關(guān)vue3封裝輪播圖組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
elementUI Pagination 分頁指定最大頁的問題及解決方法(page-count)
項目中遇到數(shù)據(jù)量大,查詢的字段多,但用戶主要使用的是最近的一些數(shù)據(jù),1萬條以后的數(shù)據(jù)一般不使用,這篇文章主要介紹了elementUI Pagination 分頁指定最大頁的問題及解決方法(page-count),需要的朋友可以參考下2024-08-08
vue中動態(tài)設置meta標簽和title標簽的方法
這篇文章主要介紹了vue中動態(tài)設置meta標簽和title標簽的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07

