1. 程式人生 > >vue.js 輪播圖元件

vue.js 輪播圖元件

       之前用jQuery寫過輪播元件,用的jquery動畫實現的圖片滑動效果。這個元件的滑動特效是原生js搭配vue的資料繫結實現的,不依賴其他庫,雖然可以再vue.js中引入swiper,但是引入類庫的最大的缺點就是冗餘程式碼太多,所以還是自己寫一個比較好,簡單扼要。(ps:元件的寬高設定還有有點小bug,子元件中需要改為用js動態修改container的寬高,另外可能還有其他地方有不合理之處,歡迎各位批評指正)

github地址:[email protected]:cainiao222/vueslider-components.git

父元件程式碼:

<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 :style="{width:wrap_width+'px',height:wrap_height+'px',left:offset_left+'px'}" 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">&lt;</div> <div @click="next" class="click next">&gt;</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 元件

       之前用jQuery寫過輪播元件,用的jquery動畫實現的圖片滑動效果。這個元件的滑動特效是原生js搭配vue的資料繫結實現的,不依賴其他庫,雖然可以再vue.js中引入swiper,但是引入類庫的最大的缺點就是冗餘程式碼太多,所以還是自己寫一個比較好,簡單扼要

Vue元件實現

今天在上慕課老師fishenal的vue實戰課程的時候,有一個輪播圖元件實現,在跟著做的時候,自己也踩了一些坑。此外,在原課程案例的基礎上,我加入了不同方向的滑動功能。 本文章採用Vue結合Css3來實現輪播圖。 首先要了解的是Vue的動畫原理。在vue中,如果我們要給元素

vue 2.0 實戰 移動音樂app(三)元件的實現

1.slider子元件 和 recommend父元件結構。利用了slot 卡槽。簡單點來說,就是子元件預先在相應的位置留了一個坑,父元件引用了子元件以後,把對應的坑填上。 <template> <div class="slider" ref="slid

2018.01.26.使用vue元件

複習鞏固vue知識,開始製作bulbBright(類似於微博的程式)。關於vue官方元件輪播圖。通俗易懂的說:先開啟cmd,cd 你的專案路徑,然後全域性安裝就是輸入指令npm install -S vue-carousel。接下來main。js檔案下輸入import VueCarousel from 'vu

vue -元件

涉及知識點: <transition-group name="list"></transition-group> 事件 定時器 條件語句 生命週期 src/views/animate/index.vue 通俗易懂

自寫:js 鼠標劃過下方按鈕,繼續下一個

就會 定時器 pac color lis hover 路線 css 輪播圖 自寫:js輪播圖 鼠標劃過下方按鈕,繼續下一個 定時器關閉後再開啟一般都會按照原來的路線繼續走,不會因為你點了3而下個就會是4 $(".focus-rad>ul>li").hover(

js

輪播圖 定時器 .com logs images lun script img doc <script> var i = 0; //創建一個定時器 function init(){ setInterval(changeImg,2000); } //定義切換圖片的

原生js實現

索引 獲取 mouseout abs length ati point css ack 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta chars

JavaScript的案例(數據校驗,js,頁面定時彈窗)

頁面 span one align 數據校驗 lse 格式 用戶輸入 ade 1.數據校驗 步驟 1.確定事件(onsubmit)並綁定一個函數 2.書寫這個函數,獲取數據,並綁定id

js封裝

banner.js結合move.js實現封裝輪播圖 banner.js程式碼 var swiper = (function() { var timer = null; return { init(ele) { if(typeof ele == 'string') { e

vue的實現

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewp

適合【前端入門者】的原生JS實現

學前端已經一年多了,在JavaScript方面,學會了基本語法,頁面操作之後,慢慢走向了框架這條不歸路,框架用起來真的是省時省力,效果好、程式碼少、還節省時間。 在前幾天去做一個頁面的時候,要求只能是用以前版本的框架,我就去找框架之前的版本,可是在某些官網上老版本的框架已經不存在了,就我這暴脾氣

js程式碼

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> .banner{

Vue-簡單

Vue-簡單輪播圖 vue寫的一個簡單輪播圖 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="w

js的實現

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style>  &nbs

RN開發快速切換底部導航時react-native-swiper元件白屏

目前react-native平臺最好用的輪播圖元件:react-native-swiper 最近在專案迭代開發測試中發現一個問題,就是在快速切換APP底部tab導航欄時,整合的輪播圖元件react-native-swiper會白屏,無法顯示圖片 如下圖所示: 通過查詢react-native-swiper的

JS-----封裝(新手)

效果圖:3D旋轉+切割 //輪播圖 //使用例子: //1.在需要的位置放置一個元素,樣式什麼的自己給,不影響     <div id="lunbotu" style="width: 400px;height: 200px;">  &

vue-原生(非連續)

效果 思路 與之前的連續輪播圖不同,每個圖片按順序排列成一條,只用改變條的位置,不需要改變圖片的順序。 程式碼 template <template> <div

ReactNative 元件Banner的建立

================================================================== //這裡是單獨的一個js檔案,到時直接匯入就可以用  這裡用的ES5的格式 //這裡用的定時器匯入計時器類庫 react-timer-mix

vue+mui

mui的輪播圖,如果圖片是請求來的,直接在html中迴圈是不會動的。 需要請求完圖片之後,在setTimeout方法裡,使用slider()方法,這樣才會動 而且mui的輪播圖,有點坑的,需要重複最後一張和第一張,才會無縫連結 <template> <div class="r