如何在vue.js中使用轮播图组件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
父组件代码:
<template> <div> <slider :img="img" :width="width" :height="height"></slider> </div> </template> <script> // import swiper from 'swiper' import slider from './slider1.vue' export default { data(){ return { img:[{src:require('../assets/slideShow/pic1.jpg'),name:'hehe1'}, {src:require('../assets/slideShow/pic2.jpg'),name:'hehe2'}, {src:require('../assets/slideShow/pic3.jpg'),name:'hehe3'}, {src:require('../assets/slideShow/pic4.jpg'),name:'hehe4'} ], width:460, height:250 } }, components:{ slider:slider } } </script>
子组件代码:
<template> <div class="box"> <div @mouseenter="endInterval" @mouseleave="startInterval" class="container"> <div : class="slider-wrap"> <div class='img' v-for="item in slider_des"> <img :src="item.src" alt=""> </div> </div> <div class="bottom"> <ul class="pointContainer"> <li @click="changeIndex(index)" :class="{point:true,active:nowIndex===index}" v-for="(point,index) in length"></li> </ul> </div> <div @click="pre" class="click pre"><</div> <div @click="next" class="click next">></div> </div> </div> </template> <script> export default { props:{ img:{ type:Array, default:[] }, width:{ type:Number, default:460 }, height:{ type:Number, default:250 } }, mounted(){ this.startInterval(); }, data(){ console.log(this.width); return{ length:new Array(this.img.length), nowIndex:0, wrap_width:this.width*(this.img.length+2), wrap_height:this.height, offset_left:-this.width, isTransition:true, timer:null, animateFlag:true, timerAuto:null } }, computed:{ slider_des:function () { var arr=[]; var arr1=arr.concat(this.img); arr1.push(this.img[0]); arr1.unshift(this.img[this.img.length-1]); return arr1; } }, methods:{ pre(){ if(this.nowIndex===0){ if(!this.animateFlag){ clearInterval(this.timer); this.animateFlag=true; this.offset_left=-(this.length.length)*this.width; } this.animate(-this.width,0,function (that) { that.offset_left=-(that.length.length)*that.width; }); this.nowIndex=this.img.length-1; return }else{ if(!this.animateFlag){ this.animateFlag=true; clearInterval(this.timer); this.offset_left=-(this.nowIndex*this.width); } this.animate(-(this.nowIndex+1)*this.width,-(this.nowIndex*this.width)); } this.nowIndex-=1; }, startInterval(){ this.timerAuto=setInterval(this.next,2000); }, endInterval(){ // console.log("leave"); clearInterval(this.timerAuto); }, next(){ if(this.nowIndex===this.length.length-1){ if(!this.animateFlag){ this.animateFlag=true; clearInterval(this.timer); this.offset_left=-this.width; } this.animate(-(this.length.length)*this.width,-(this.length.length+1)*this.width,function (that) { that.offset_left=-that.width; }); this.nowIndex=0; return }else{ if(!this.animateFlag){ this.animateFlag=true; clearInterval(this.timer); this.offset_left=-(this.nowIndex+2)*this.width; } this.animate(-(this.nowIndex+1)*this.width,-(this.nowIndex+2)*this.width); this.nowIndex+=1; } }, animate(start,end,fuc){ var distance=end-start; var pre_dis=distance/50; var that=this; this.timer=setInterval(function () { that.animateFlag=false; if(Math.abs(end-that.offset_left)<=Math.abs(pre_dis)){ that.offset_left=end; if(fuc){ fuc(that); } that.animateFlag=true; clearInterval(that.timer); that.timer=null; return } that.offset_left+=pre_dis; },1); }, changeIndex(index){ clearInterval(this.timer); this.animate(-(this.nowIndex+1)*this.width,-(index+1)*this.width); this.nowIndex=index; } } } </script> <style scoped> *{ margin: 0; list-style: none; /*height: 0;*/ } .box{ position: relative; } .container{ width: 460px; height: 250px; margin: 0 auto; border: 1px solid #3bb4f2; overflow: hidden; left: 0; top: 0; position: absolute; } .slider-wrap{ /*width: 2760px;*/ /*height: 250px;*/ position: absolute; /*left: -460px;*/ /*overflow: hidden;*/ top: 0; } .transition{ transition: 0.5s; } .img{ width: 460px; height: 250px; float: left; display: inline; } img{ width: 460px; height: 250px; /*float: left;*/ } .click{ width: 20px; background-color: rgba(255,255,255,.3); color: aliceblue; font-weight: bold; position: absolute; height: 40px; line-height: 40px; margin-top: -20px; top: 50%; cursor: pointer; } .pre{ left: 0; } .next{ right: 0; } .bottom{ position: absolute; bottom: 0; width: 100%; height: 20px; text-align: center; } .pointContainer{ height: 20px; } .point{ display: inline-block; border: 5px solid #eeeeee; border-radius: 5px; line-height: 0; margin-right: 3px; } .active{ border: 5px solid #42b983; } </style>
看完上述内容,你们掌握如何在vue.js中使用轮播图组件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。