Vue實(shí)現(xiàn)首頁banner自動(dòng)輪播效果
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)首頁banner自動(dòng)輪播的具體代碼,供大家參考,具體內(nèi)容如下
效果如圖:

①創(chuàng)建Banner.vue組件,需傳入banner數(shù)組,可設(shè)置輪播間隔ms
<template>
? <div class="m-banner-wrap" v-if="bannerData.length">
? ? <div class="m-banner-list">
? ? ? <div
? ? ? ? class="u-banner-item fade"
? ? ? ? v-for="(item, index) in bannerData"
? ? ? ? :key="index"
? ? ? ? v-show="index===activeIndex"
? ? ? ? :style="`background: url(${item.tupian}) no-repeat; background-size: cover;`"
? ? ? ? @mouseover="onOver()" @mouseout="onOut()">
? ? ? ? <a :href="item.link"></a>
? ? ? </div>
? ? </div>
? ? <div class="m-dot-list" v-if="bannerData.length > 1">
? ? ? <div v-for="(item, index) in bannerData" :key="index" :class="['u-dot-item', {active: index===activeIndex}]" @mouseenter="onEnter(index)" @mouseleave="onLeave">
? ? ? </div>
? ? </div>
? </div>
</template>
<script>
export default {
? name: 'Banner',
? props: {
? ? bannerData: { // banner數(shù)組
? ? ? type: Array,
? ? ? default: () => {
? ? ? ? return []
? ? ? }
? ? },
? ? interval: { // 切換間隔ms
? ? ? type: Number,
? ? ? default: 3000
? ? }
? },
? data () {
? ? return {
? ? ? activeIndex: 0,
? ? ? timer: null
? ? }
? },
? mounted () {
? ? setTimeout(() => {
? ? ? this.startSlider()
? ? }, 1000)
? },
? methods: {
? ? onOver () {
? ? ? clearInterval(this.timer)
? ? },
? ? onOut () {
? ? ? this.startSlider()
? ? },
? ? onEnter (index) {
? ? ? this.activeIndex = index
? ? ? clearInterval(this.timer)
? ? },
? ? onLeave () {
? ? ? this.startSlider()
? ? },
? ? startSlider () {
? ? ? clearInterval(this.timer)
? ? ? if (this.bannerData.length > 1) {
? ? ? ? this.timer = setInterval(this.onMove, this.interval)
? ? ? }
? ? },
? ? onMove () {
? ? ? if (this.activeIndex === this.bannerData.length - 1) {
? ? ? ? this.activeIndex = 0
? ? ? } else {
? ? ? ? this.activeIndex++
? ? ? }
? ? }
? },
? beforeDestroy () {
? ? clearInterval(this.timer)
? ? this.timer = null
? }
}
</script>
<style lang="less" scoped>
@mainColor: #108ee9;
.m-banner-wrap {
? width: 100%;
? height: 600px;
? min-width: 1200px;
? margin: 0 auto;
? overflow: hidden;
? position: relative;
? .m-banner-list {
? ? height: 600px;
? ? .u-banner-item {
? ? ? min-width: 1200px;
? ? ? height: 600px;
? ? ? width: 100%;
? ? ? a {
? ? ? ? display: block;
? ? ? ? height: 100%;
? ? ? }
? ? }
? ? .fade {
? ? ? animation: fade 0.5s ease-in-out; // 切換banner時(shí)的過渡效果
? ? }
? ? @keyframes fade {
? ? ? 0% {opacity:0;}
? ? ? 5% {opacity:0.05;}
? ? ? 10% {opacity:0.1;}
? ? ? 20% {opacity:0.2;}
? ? ? 35% {opacity:0.35;}
? ? ? 50% {opacity:0.5;}
? ? ? 65% {opacity:0.65;}
? ? ? 80% {opacity:0.8;}
? ? ? 90% {opacity:0.9;}
? ? ? 95% {opacity:0.95;}
? ? ? 100%{opacity:1;}
? ? }
? }
? .m-dot-list {
? ? width: 600px;
? ? position: absolute;
? ? bottom: 20px;
? ? left: 50%;
? ? margin-left: -300px;
? ? text-align: center;
? ? .u-dot-item { // 圓點(diǎn)樣式
? ? ? display: inline-block;
? ? ? width: 12px;
? ? ? height: 12px;
? ? ? margin: 0 5px;
? ? ? background: #fff;
? ? ? cursor: pointer;
? ? ? border: 1px solid #fff;
? ? ? border-radius: 50%;
? ? ? opacity: 0.8;
? ? }
? ? .active { // 圓點(diǎn)選中樣式
? ? ? width: 30px;
? ? ? background: @mainColor;
? ? ? border: 1px solid @mainColor;
? ? ? border-radius: 12px;
? ? ? opacity: 1;
? ? }
? }
}
</style>②在要使用的頁面引入
<Banner :bannerData="bannerData" :interval="3000"/>
import Banner from '@/components/Banner'
components: {
? ? Banner
}
data () {
? ? return {
? ? ? bannerData: [
? ? ? ? {
? ? ? ? ? link: '跳轉(zhuǎn)地址...',
? ? ? ? ? src: '圖片地址...'
? ? ? ? },
? ? ? ? {
? ? ? ? ? link: '跳轉(zhuǎn)地址...',
? ? ? ? ? src: '圖片地址...'
? ? ? ? },
? ? ? ? {
? ? ? ? ? link: '跳轉(zhuǎn)地址...',
? ? ? ? ? src: '圖片地址...'
? ? ? ? }
? ? ? ]
? ? }
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+Element的后臺(tái)管理框架的整合實(shí)踐
本文主要介紹了Vue+Element的后臺(tái)管理框架,在框架上,領(lǐng)導(dǎo)要用AdminLTE這套模板,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vuejs2.0子組件改變父組件的數(shù)據(jù)實(shí)例
本篇文章主要介紹了vuejs2.0子組件改變父組件的數(shù)據(jù)實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
vue2/vue3路由權(quán)限管理的方法實(shí)例
最近用vue框架做了個(gè)后臺(tái)管理項(xiàng)目,涉及權(quán)限,所以下面這篇文章主要給大家介紹了關(guān)于vue2/vue3路由權(quán)限管理的相關(guān)資料,需要的朋友可以參考下2021-06-06
vue實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)綁定
本篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)綁定,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

