vue3+Pinia+TypeScript?實(shí)現(xiàn)封裝輪播圖組件
為什么封裝?
- 迎合es6模塊化開發(fā)思想
- 注冊(cè)為全局組件,可以更好地復(fù)用,需要用到的地方,直接使用標(biāo)簽即可
靜態(tài)結(jié)構(gòu) 后面再進(jìn)行更改
<script lang="ts" setup name="XtxCarousel">
defineProps()
</script>
<template>
<div class="xtx-carousel">
<ul class="carousel-body">
<li class="carousel-item fade">
<RouterLink to="/">
<img
src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg"
alt=""
/>
</RouterLink>
</li>
<li class="carousel-item">
<RouterLink to="/">
<img
src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg"
alt=""
/>
</RouterLink>
</li>
<li class="carousel-item">
<RouterLink to="/">
<img
src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg"
alt=""
/>
</RouterLink>
</li>
</ul>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn prev"
><i class="iconfont icon-angle-left"></i
></a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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>
</template>
<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>請(qǐng)求數(shù)據(jù)都存放在pinia里面
import { defineStore } from 'pinia'
import request from '@/utils/request'
import { BannerItem, IApiRes } from '@/types/data'
export default defineStore('home', {
persist: {
enabled: true
},
state() {
return {
bannerList: [] as BannerItem[]
}
},
actions: {
// 拿輪播圖數(shù)據(jù)
async getBannerList() {
const res = await request.get<IApiRes<BannerItem[]>>('/home/banner')
console.log('拿輪播圖數(shù)據(jù)', res)
this.bannerList = res.data.result
}
}
})類型檢測(cè)
// 所有的接口的通用類型
export interface IApiRes<T> {
msg: string,
result: T
}
// 輪播圖數(shù)據(jù)中的項(xiàng)
export type BannerItem = {
id: string;
imgUrl: string;
hrefUrl: string;
type: string;
}頁面級(jí)組件
<script lang="ts" setup>
import useStore from '@/store';
const { home } = useStore()
// 發(fā)請(qǐng)求
home.getBannerList()
</script>
<template>
<div class="home-banner">
<!-- 輪播圖子組件 -->
<XtxCarousel :slides="home.bannerList" :autoPlay="true" :duration="1000"/>
</div>
</template>
<style scoped lang="less">
.home-banner {
width: 1240px;
height: 450px;
position: absolute;
left: 0;
top: 0;
z-index: 98;
background-color: #ccc;
}
/deep/ .xtx-carousel .carousel-btn.prev {
left: 270px !important;
}
/deep/ .xtx-carousel .carousel-indicator {
padding-left: 250px;
}
</style>全局組件
<script lang="ts" setup name="XtxCarousel">
import { BannerItem } from '@/types/data'
import { onBeforeUnmount, onMounted, ref } from 'vue';
// 定義props
const { slides, autoPlay, duration } = defineProps<{
slides: BannerItem[],
autoPlay?: boolean, // 是否開啟自動(dòng)播放
duration?: number // 自動(dòng)播放的間隔時(shí)間
}>()
// 當(dāng)前哪個(gè)圖片選中 高亮
const active = ref(1)
// 點(diǎn)擊右側(cè)箭頭++
const hNext = () => {
active.value++
if(active.value === slides.length){
active.value = 0
}
}
// 點(diǎn)擊左側(cè)箭頭--
const hPrev = () => {
active.value--
if(active.value < 0){
active.value = slides.length - 1
}
}
// 如果開啟了自動(dòng)播放,則每隔duration去播放下一張: hNext()
onMounted(() => {
start()
})
onBeforeUnmount(() => {
stop()
})
let timer = -1
// 鼠標(biāo)離開要繼續(xù)播放
const start = () => {
if(autoPlay) {
timer = window.setInterval(() => {
hNext()
}, duration)
}
}
// 鼠標(biāo)hover要暫停播放
const stop = () => {
clearInterval(timer)
}
</script>
<template>
<div class="xtx-carousel" @mouseleave="start()" @mouseenter="stop()">
<ul class="carousel-body">
<li class="carousel-item"
:class="{ fade: idx === active}"
v-for="(it, idx) in slides" :key="it.id">
<RouterLink to="/">
<img :src="it.imgUrl" alt="" />
</RouterLink>
</li>
</ul>
<a @click="hPrev" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
<a @click="hNext" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
<div class="carousel-indicator">
<span
v-for="(it, idx) in slides"
:class="{ active: active===idx}"
@mouseenter="active = idx"
></span>
</div>
</div>
</template>
<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>到此這篇關(guān)于vue3+Pinia+TypeScript 實(shí)現(xiàn)封裝輪播圖組件的文章就介紹到這了,更多相關(guān)vue3 封裝輪播圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3結(jié)合typescript中使用class封裝axios
- vue3+vite3+typescript實(shí)現(xiàn)驗(yàn)證碼功能及表單驗(yàn)證效果
- 使用Vite+Vue3+TypeScript?搭建開發(fā)腳手架的詳細(xì)過程
- vue?props使用typescript自定義類型的方法實(shí)例
- 基于Vue3+TypeScript的全局對(duì)象的注入和使用詳解
- Vue3+TypeScript+Vite使用require動(dòng)態(tài)引入圖片等靜態(tài)資源
- Vue+TypeScript中處理computed方式
- vue項(xiàng)目中使用ts(typescript)入門教程
- vue3中如何使用vue-types
相關(guān)文章
ionic 3.0+ 項(xiàng)目搭建運(yùn)行環(huán)境的教程
本篇文章主要介紹了ionic 3.0+ 項(xiàng)目搭建運(yùn)行的教程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
前端大文件分片下載具體實(shí)現(xiàn)方法(看這一篇就夠了)
本文介紹了在瀏覽器中下載大文件的技術(shù)方案,包括分片下載、斷點(diǎn)續(xù)傳、進(jìn)度條顯示、取消及暫停下載和文件合并等功能,分片下載可以降低網(wǎng)絡(luò)傳輸中斷的風(fēng)險(xiǎn),并減少內(nèi)存占用,需要的朋友可以參考下2024-10-10
js中的document.querySelector()方法舉例詳解
這篇文章主要給大家介紹了關(guān)于js中document.querySelector()方法的相關(guān)資料,document.querySelector是JavaScript中的一個(gè)內(nèi)置方法,用于通過CSS選擇器選擇文檔中的第一個(gè)匹配元素,需要的朋友可以參考下2024-01-01
js實(shí)現(xiàn)當(dāng)鼠標(biāo)移到表格上時(shí)顯示這一格全部內(nèi)容的代碼
下面小編就為大家?guī)硪黄猨s實(shí)現(xiàn)當(dāng)鼠標(biāo)移到表格上時(shí)顯示這一格全部內(nèi)容的代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
解決使用bootstrap的dropdown部件時(shí)報(bào)錯(cuò):error:Bootstrap dropdown require
這篇文章主要介紹了使用bootstrap的dropdown部件時(shí)報(bào)錯(cuò):error:Bootstrap dropdown require Popper.js ,小編把問題描述和解決方案分享給大家,需要的朋友可以參考下2018-08-08
JavaScript遞歸函數(shù)定義與用法實(shí)例分析
這篇文章主要介紹了JavaScript遞歸函數(shù)定義與用法,結(jié)合實(shí)例形式分析了javascript遞歸的原理、函數(shù)定義、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2019-01-01
JS實(shí)現(xiàn)移動(dòng)端按首字母檢索城市列表附源碼下載
我們常見的手機(jī)通訊錄或微信通訊錄,聯(lián)系人信息是按字母順序排列的列表,通過點(diǎn)擊右側(cè)的字母,會(huì)迅速定位檢索到首字母對(duì)應(yīng)的聯(lián)系人。下面通過本文給大家分享JS實(shí)現(xiàn)移動(dòng)端按首字母檢索城市列表功能,需要的的朋友參考下吧2017-07-07

