本篇文章主要介紹了利用vueJs實作圖片輪播實例程式碼,小編覺得蠻不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧
最近新學習了vuejs,嘗試著用vuejs寫了一個簡單的圖片輪播,便做個簡單的記錄
以下只貼出carousel. vue程式碼,其他的省略
<template> <p ref="root"> <p class="sliderPanel"> <p v-for="(item,index) in imgArray" class="verticalCenter picbox"> <transition name="slide-fade"> <img :style="{width:width,top:top}" @mouseover="clearAuto" @mouseout="slideAuto" v-show="index===selectIndex" :src="item.url" style="min-height: 100%"> </transition> </p> </p> <p @click="clickLeft" @mouseover="clearAuto" @mouseout="slideAuto" class="arrowLeft verticalCenter horizaCenter"> 左移 </p> <p @click="clickRight" @mouseover="clearAuto" @mouseout="slideAuto" class="arrowRight verticalCenter horizaCenter"> 右移 </p> <p class="sliderBar horizaCenter"> <p v-for="(item,index) in imgArray" @mouseover="clearAuto" @mouseout="slideAuto" @click="setIndex(index)" class="circle" :class="{circleSelected:index===selectIndex}"> </p> </p> </p> </template> <script> const SCREEN_WIDTH=document.body.clientWidth//网页可见区域宽 const SCREEN_HEIGHT=document.body.scrollHeight//网页正文全文高 var selectIndex=0 var timer=null export default { name: "ErCarousel", data() { return { selectIndex:0, width:'100%', height:SCREEN_HEIGHT+'px', top:0, imgArray:[ { url:'/src/components/carousel/image/1.jpg', }, { url:'/src/components/carousel/image/2.jpg', }, { url:'/src/components/carousel/image/3.jpg', } ] } }, methods:{ slideAuto:function () { var that=this; timer=setInterval(function(){ that.clickRight(); },3000) //clearInterval(timer); }, clearAuto:function(){ clearInterval(timer); }, clickLeft:function(){ if(this.selectIndex==0){ this.selectIndex=this.imgArray.length-1; }else{ this.selectIndex--; } console.log(this.selectIndex); }, clickRight:function(){ if(this.selectIndex==this.imgArray.length-1){ this.selectIndex=0; }else{ this.selectIndex++; } }, setIndex:function (index) { this.selectIndex=index; } }, mounted:function(){ this.slideAuto(); } } </script> <style>
整個模組也是分為了template,script,style三個部分,簡單的介紹了圖片左右切換,以及css滑動效果等,純當練手。
以上是vueJs如何實作圖片輪播的實例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!