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

vue3封裝輪播圖組件功能的完整步驟

 更新時(shí)間:2022年11月23日 14:44:27   作者:風(fēng)無(wú)雨  
我們都知道,輪播圖組件模板結(jié)構(gòu)通常是ul包裹li的結(jié)構(gòu),在vue中,li的數(shù)量也通常是由后端接口返回的數(shù)據(jù)決定,下面這篇文章主要給大家介紹了關(guān)于vue3封裝輪播圖組件功能的相關(guān)資料,需要的朋友可以參考下

需求:

封裝一個(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)

父組件通過(guò)自定義屬性傳值傳遞包含圖片的數(shù)組變量,

子組件通過(guò)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部分

  • 輪播圖切換通過(guò)類名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)入輪播圖組件,頁(yè)面使用組件名便簽(此處為局部組件注冊(cè),可以注冊(cè)為全局組件)

2.模擬輪播圖數(shù)據(jù),對(duì)象數(shù)組,每個(gè)對(duì)象有圖片數(shù)據(jù)imageUrl和id

3.通過(guò)自定義屬性把輪播圖數(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)是否一致來(lái)顯示圖片(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é)

組件傳值

封裝組件可以通過(guò)父子組件傳值,插槽傳值,實(shí)現(xiàn)數(shù)據(jù)傳遞

輪播圖實(shí)現(xiàn)無(wú)限滾動(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)文章

最新評(píng)論

托克托县| 佛山市| 卢氏县| 中宁县| 同德县| 吉安市| 时尚| 宁城县| 玉林市| 雷州市| 定边县| 家居| 台北市| 天台县| 怀来县| 永清县| 攀枝花市| 广东省| 新宁县| 双鸭山市| 玉田县| 奉节县| 怀来县| 隆昌县| 安泽县| 华池县| 镇远县| 武强县| 安义县| 仙居县| 沈丘县| 宝清县| 栾川县| 东港市| 嘉祥县| 会同县| 谢通门县| 大悟县| 合山市| 永胜县| 淳化县|