Vue實(shí)現(xiàn)可復(fù)用輪播組件的方法
本文用vue簡(jiǎn)單的實(shí)現(xiàn)了一個(gè)輪播圖的基礎(chǔ)功能,并抽離出來(lái)作為一個(gè)公共組件,方便復(fù)用
html和js部分
<template> ? <div ? ? class="img-box" ? ? ref="img-box" ? ? :style="{width: styles.width, height: styles.height}" ? > ? ? <div v-for="(item, index) in imgList" ? ? ? ? ?:key="index" ? ? ? ? ?class="img-item" ? ? ? ? ?:ref="'img-item-' + index" ? ? ? ? ?:class="{'active': index === active}" ? ? > ? ? ? <img ? ? ? ? :src="item" ? ? ? ? style="width:100%" ? ? ? ? :style="{height: styles.height}" ? ? ? /> ? ? </div> ? ? <div ? ? ? class="img-position" ? ? ? v-if="isShowPosition" ? ? > ? ? ? <template v-for="(item, index) in imgList"> ? ? ? ? <span :key="index" ? ? ? ? ? ? ? class="img-position-item" ? ? ? ? ? ? ? :ref="'img-position-' + index" ? ? ? ? ? ? ? :class="[ ? ? ? ? ? ? ? ? {'active': index === active}, ? ? ? ? ? ? ? ? isCircle ? 'circle' : '', ? ? ? ? ? ? ? ? isNums ? 'nums' : '' ? ? ? ? ? ? ? ]" ? ? ? ? ? ? ? @click="clickSpan(index)" ? ? ? ? > ? ? ? ? ? {{isNums ? index + 1 : ''}} ? ? ? ? </span> ? ? ? </template> ? ? </div> ? ? <div ? ? ? class="left-btn" ? ? ? v-if="isShowLeftOrRightBtn" ? ? ? @click="clickBtn('left')" ? ? > ? ? ? <i class="iconfont roll-zuo"></i> ? ? </div> ? ? <div ? ? ? class="right-btn" ? ? ? v-if="isShowLeftOrRightBtn" ? ? ? @click="clickBtn('right')" ? ? > ? ? ? <i class="iconfont roll-you"></i> ? ? </div> ? </div> </template> <script> export default { ? name: 'Roll', ? props: { ? ? imgList: { // 圖片列表 src數(shù)組 ? ? ? type: Array, ? ? ? default: () => [] ? ? }, ? ? isShowPosition: { // 是否顯示下方小圓點(diǎn) ? ? ? type: Boolean, ? ? ? default: true ? ? }, ? ? positionInner: { // 圓點(diǎn)內(nèi)容 ? ? ? type: String, ? ? ? default: 'circle' // 默認(rèn)圓點(diǎn),可選值 circle => 圓點(diǎn) num => 數(shù)字 both => 圓點(diǎn)+數(shù)字 ? ? }, ? ? isShowLeftOrRightBtn: { // 是否顯示左右按鈕 ? ? ? type: Boolean, ? ? ? default: true ? ? }, ? ? duration: { // 切換間隔 ? ? ? type: [Number, String], ? ? ? default: 3000 ? ? }, ? ? styles: { // 自定義輪播圖片寬高 默認(rèn)500*300 ? ? ? type: Object, ? ? ? default: () => { ? ? ? ? return { ? ? ? ? ? width: '500px', ? ? ? ? ? height: '300px' ? ? ? ? } ? ? ? } ? ? } ? }, ? data () { ? ? return { ? ? ? active: 0, // 當(dāng)前輪播圖片 ? ? ? timer: null // 定時(shí)器 ? ? } ? }, ? computed: { ? ? isCircle () { ? ? ? return ['circle', 'both'].includes(this.positionInner) ? ? }, ? ? isNums () { ? ? ? return ['num', 'both'].includes(this.positionInner) ? ? } ? }, ? updated () { ? ? if (this.timer) this.clearTimer() ? ? this.setTimer() ? }, ? created () { ? ? this.setTimer() ? }, ? methods: { ? ? clickSpan (index) { ? ? ? this.clearTimer() ? ? ? this.active = index ? ? }, ? ? clickBtn (arg) { ? ? ? this.clearTimer() ? ? ? if (arg === 'left') { ? ? ? ? this.active = this.active - 1 < 0 ? this.imgList.length - 1 : this.active - 1 ? ? ? } else { ? ? ? ? this.active = this.active + 1 === this.imgList.length ? 0 : this.active + 1 ? ? ? } ? ? ? this.setTimer() ? ? }, ? ? setTimer () { ? ? ? this.timer = setInterval(() => { ? ? ? ? this.clickBtn('right') ? ? ? }, Number(this.duration)) ? ? }, ? ? clearTimer () { ? ? ? clearInterval(this.timer) ? ? ? this.timer = null ? ? } ? } } </script>
css樣式部分
<style scoped> @import url('//at.alicdn.com/t/font_1451815_senarwrqu6.css'); * { ? margin: 0; ? padding: 0; } .img-box { ? position: relative; ? margin: 0 auto; } .img-item { ? height: 100%; ? width: 100%; ? opacity: 0; ? position: absolute; ? top: 0; ? right: 0; ? left: 0; ? bottom: 0; ? z-index: -5; ? text-align: center; } .img-item.active { ? z-index: 0; ? opacity: 1; ? transition: .3s; } .img-position { ? position: absolute; ? bottom: 5px; ? left: 50%; ? display: flex; ? transform: translate(-50%, 0); } .img-position-item { ? display: inline-block; ? width:10px; ? height:10px; ? box-sizing: border-box; ? cursor: pointer; } .img-position-item.circle { ? border-radius: 50%; ? border: 1px solid #606266; } .img-position-item.nums { ? width: 18px; ? height: 18px; ? display: flex; ? justify-content: center; ? align-items: center; ? color: #606266; ? font-size:14px; } .img-position-item:hover, .img-position-item.active { ? border-color: #d1d2d3; ? color: #d1d2d3; ? transition: .3s; } .img-position-item + .img-position-item { ? margin-left: 10px; } .left-btn, .right-btn { ? position: absolute; ? top: 50%; ? bottom: 0; ? width: 20px; ? height: 30px; ? display: flex; ? justify-content: center; ? align-items: center; ? cursor: pointer; ? color: #d1d2d3; ? font-size: 20px; ? transform: translate(0, -50%); } .left-btn:hover, .right-btn:hover { ? color: #fff; ? transition: .3s; } .left-btn { ? left: 5px; } .right-btn { ? right: 5px; } </style>
只是簡(jiǎn)單的實(shí)現(xiàn)了一個(gè)輪播圖比較基礎(chǔ)的部分,之前用原生寫了一遍,現(xiàn)在用vue寫一遍作為一個(gè)組件,也還不錯(cuò)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決vue使用vant輪播組件swipe + flex時(shí)文字抖動(dòng)問(wèn)題
- Vue實(shí)現(xiàn)圖片輪播組件思路及實(shí)例解析
- vue輪播組件實(shí)現(xiàn)$children和$parent 附帶好用的gif錄制工具
- vue移動(dòng)端輕量級(jí)的輪播組件實(shí)現(xiàn)代碼
- 基于Vue2x實(shí)現(xiàn)響應(yīng)式自適應(yīng)輪播組件插件VueSliderShow功能
- 使用Vue制作圖片輪播組件思路詳解
- 利用Vue實(shí)現(xiàn)移動(dòng)端圖片輪播組件的方法實(shí)例
- 基于vue實(shí)現(xiàn)swipe輪播組件實(shí)例代碼
- 基于vue.js輪播組件vue-awesome-swiper實(shí)現(xiàn)輪播圖
相關(guān)文章
詳解vue3結(jié)合ts項(xiàng)目中使用mockjs
這篇文章主要為大家介紹了vue3結(jié)合ts項(xiàng)目中使用mockjs示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07vue 無(wú)法覆蓋vant的UI組件的樣式問(wèn)題
這篇文章主要介紹了vue 無(wú)法覆蓋vant的UI組件的樣式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue+elemen實(shí)現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn)
el-tooltip組件本身就是懸浮提示功能,在對(duì)它進(jìn)行二次封裝時(shí),實(shí)現(xiàn)超出的文本浮現(xiàn),本文就來(lái)介紹一下vue+elemen實(shí)現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn),感興趣的可以了解一下2023-12-12用npm安裝vue和vue-cli,并使用webpack創(chuàng)建項(xiàng)目的方法
今天小編就為大家分享一篇用npm安裝vue和vue-cli,并使用webpack創(chuàng)建項(xiàng)目的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue中addEventListener()?監(jiān)聽事件案例講解
這篇文章主要介紹了Vue中addEventListener()?監(jiān)聽事件案例講解,包括語(yǔ)法講解和事件冒泡或事件捕獲的相關(guān)知識(shí),本文結(jié)合示例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-12-12vue?element?el-select下拉滾動(dòng)加載的方法
很多朋友都遇到這樣一個(gè)問(wèn)題在使用vue+element的el-select下拉框加載返回?cái)?shù)據(jù)太多時(shí),會(huì)造成卡頓,用戶體驗(yàn)欠佳,這篇文章主要介紹了vue?element?el-select下拉滾動(dòng)加載方法,需要的朋友可以參考下2022-11-11