您好,欢迎来到骅坨科技网。
搜索
您的当前位置:首页vueJs如何实现图片轮播的实例代码分享

vueJs如何实现图片轮播的实例代码分享

来源:骅坨科技网


本篇文章主要介绍了利用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滑动效果等,纯当练手。

Copyright © 2019- huatuo5.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务