利用vueJs實現(xiàn)圖片輪播實例代碼
最近新學(xué)習(xí)了vuejs,嘗試著用vuejs寫了一個簡單的圖片輪播,便做個簡單的記錄
以下只貼出carousel.vue代碼,其他的省略
<template>
<div ref="root">
<div class="sliderPanel">
<div 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>
</div>
</div>
<div @click="clickLeft" @mouseover="clearAuto" @mouseout="slideAuto" class="arrowLeft verticalCenter horizaCenter">
左移
</div>
<div @click="clickRight" @mouseover="clearAuto" @mouseout="slideAuto" class="arrowRight verticalCenter horizaCenter">
右移
</div>
<div class="sliderBar horizaCenter">
<div v-for="(item,index) in imgArray" @mouseover="clearAuto" @mouseout="slideAuto" @click="setIndex(index)" class="circle" :class="{circleSelected:index===selectIndex}">
</div>
</div>
</div>
</template>
<script>
const SCREEN_WIDTH=document.body.clientWidth//網(wǎng)頁可見區(qū)域?qū)?
const SCREEN_HEIGHT=document.body.scrollHeight//網(wǎng)頁正文全文高
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滑動效果等,純當(dāng)練手。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vuex中...mapstate和...mapgetters的區(qū)別及說明
這篇文章主要介紹了vuex中...mapstate和...mapgetters的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue3組合式API中使用forwardRef()函數(shù)
本文主要介紹了Vue3組合式API中使用forwardRef()函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
vant中l(wèi)ist的使用以及首次加載觸發(fā)兩次解決問題
這篇文章主要介紹了vant中l(wèi)ist的使用以及首次加載觸發(fā)兩次解決問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue如何獲取new Date().getTime()時間戳
在Web開發(fā)中,前端使用Vue.js獲取的是毫秒級時間戳,而PHP后端則是秒級時間戳,處理此類問題時,需要將PHP的時間戳乘以1000轉(zhuǎn)換為毫秒級,以保證數(shù)據(jù)的一致性和正確的邏輯判斷2024-10-10
Vue讀取本地靜態(tài).md并側(cè)邊欄導(dǎo)航跳轉(zhuǎn)、展示.md文件的操作方法
這篇文章主要介紹了Vue讀取本地靜態(tài).md并側(cè)邊欄導(dǎo)航跳轉(zhuǎn)、展示.md文件,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08

