vue實(shí)現(xiàn)圖片滾動(dòng)的示例代碼(類(lèi)似走馬燈效果)
上次寫(xiě)了一個(gè)簡(jiǎn)單的圖片輪播,這個(gè)相當(dāng)于在上面的一些改進(jìn)。這個(gè)組件除了可以進(jìn)行圖片滾動(dòng)外,也可以嵌入任何內(nèi)容的標(biāo)簽進(jìn)行滾動(dòng),里面用了slot進(jìn)行封裝。
父:
<template>
<div id="app">
<er-carousel-index :typeNumber=2 :pageNumber=3 :timeSpace=2 :duration=2 :isOrNotCircle="true" url="/src/js/index.json" :isOrNotButton=false>
<template scope="props">-----使用子組件傳過(guò)來(lái)的值,封裝slot
<div class="articleList-box-photo ">
<div class="tu imageEffectsAnimate imageEffects_Magnifier">
<a>
<img class="minMax" :src="props.item.img">
</a>
</div>
</div>
<div class="articleList-box-title">
<div class="title">
<a class="textleft">{{props.item.title}}</a>
</div>
</div>
</template>
</er-carousel-index>
</div>
</template>
<script>
import ErCarouselIndex from './components/carouselIndex/src/carouselIndex.vue'
export default {
name: 'app',
data() {
}
},
components: {
ErCarouselIndex//一定要進(jìn)行組件聲明,不然不能引用子組件
}
}
</script>
子組件:
<template>
<div tag="div" class="articleList articleListMod-3 er-carouseindex" name="slide-fade" id="articleList" :style="{height:imgHeight+'px'}" >
<span id="btn1" class="er-carouseindex-left" @mousedown="imgMove('mouseLeft')" @mouseup="cancelMove('left')" v-show="isOrNotButton"></span>
<span id="btn2" class="er-carouseindex-right" @mousedown="imgMove('mouseRight')" @mouseup="cancelMove('right')" v-show="isOrNotButton"></span>
<div id="packageAll" class="er-carouseindex-con" @mouseover="clearAuto" @mouseout="slideAuto">
<div class="er-carouseindex-bar" v-show="isOrNotCircle">
<div v-for="(item,dex) in imgList" @mouseup="clearAuto" class="er-carouseindex-circle" @click="circleClick(dex)" :class="{circleSelected:dex===indexCircle}">
</div>
</div>
<div id="imageAll" class="er-carouseindex-item" :style="{transform:translateX,transition:transFlag?transitionTime:''}">
<div class="articleList-box er-carouseindex-box" v-for="(list,index) in imgLisShow" :style="{width:imgWidth+'%'}"
style="max-height:50%;">
<slot :item="list"></slot>
</div>
</div>
</div>
</div>
</template>
<script>
export default
{
name: "ErCarouselIndex",
data(){
return {
imgList: [],//請(qǐng)求接口數(shù)據(jù)
imgLisShow: [],//圖片滾動(dòng)數(shù)據(jù),包括左中右三種
timer: null,//自動(dòng)循環(huán)滾動(dòng)時(shí)的間隔時(shí)間
timeout:null,//長(zhǎng)按時(shí)的圖片滾動(dòng)間隔時(shí)間
index:0,//圖片索引
translateXnum:0,//圖片滾動(dòng)時(shí)的偏移量
translateX:"",//生成圖片偏移時(shí)的表達(dá)式
imgWidth:"",//圖片所占寬度
timeDown:"",//鼠標(biāo)剛按下時(shí)的時(shí)間
timeup:"",//鼠標(biāo)松開(kāi)時(shí)的時(shí)間
clickSpace:"",//鼠標(biāo)按下松開(kāi)的時(shí)間間隙
transFlag:true,//是否勻速滾動(dòng),
transitionTime:"",
indexCircle:0//小圓圈滾動(dòng)索引
}
},
props:{
duration:0,//圖片延時(shí)滾動(dòng)
typeNumber:0, //每次滾動(dòng)幾張
timeSpace:0, //圖片滾動(dòng)時(shí)間間隔
url:String,//請(qǐng)求接口地址
pageNumber:0,//當(dāng)前頁(yè)面顯示幾張圖片
isOrNotButton:true,//是否顯示左右按鈕
isOrNotCircle:true,//是否顯示小圓圈
imgHeight:""http://圖片滾動(dòng)顯示高度
},
watch:{
index:{
handler(){
var _this=this;
if(Math.abs(this.index)==this.imgList.length){
this.indexCircle=0;
setTimeout(function(){
_this.reset();
},_this.duration*1000*0.98);
}else{
this.indexCircle=this.index;
}
this.calcXnum();
}
},
translateXnum:{
handler(){
this.translateX="translateX("+this.translateXnum+"%)";
}
}
},
methods:{
//頁(yè)面初始化復(fù)賦值
imgView:function() {
var _this = this;
_this.$http.get(_this.url).then(function (res) {
_this.imgList = res.data.imgList;
for(var i=0;i<3;i++){
_this.imgList.forEach(function (item, index) {
_this.imgLisShow.push(item);
});
}
_this.reset();
_this.slideAuto();
_this.imgWidth=(100/_this.pageNumber)-1;
_this.transitionTime="all "+_this.duration*0.98+"s linear";
console.log(_this.transitionTime);
});
},
//圖片滾動(dòng)方法(長(zhǎng)按)
imgMove:function(direct){
var _this = this;
_this.timeDown=new Date();//記錄按下的時(shí)間
_this.timeout = setInterval(function() {
if(direct=="mouseLeft") {
_this.leftMove();
}else{
_this.rightMove();
}
},300);
},
//鼠標(biāo)送開(kāi)時(shí)執(zhí)行的方法
cancelMove:function(direct){
var _this = this;
_this.clearAuto();
this.timeup=new Date();//記錄松開(kāi)的時(shí)間
this.clickSpace=this.timeup.getTime() - this.timeDown.getTime();
//時(shí)間間隔小于500毫秒為點(diǎn)擊,反之為長(zhǎng)按
if(this.clickSpace<500){
for(var i=0;i<_this.typeNumber;i++){
if(direct=="left"){
_this.leftMove();
}else{
_this.rightMove();
}
}
}
if (this.timeout) {
clearInterval(this.timeout);
this.timeout = null;
}
},
//向左移動(dòng)
leftMove:function(){
this.index--;
this.transFlag=true;
},
//向右移動(dòng)
rightMove:function(){
this.transFlag=true;
this.index++;
},
slideAuto:function () {
var _this = this;
_this.timer = setTimeout(function () {
if(Math.abs(_this.index)!==_this.imgList.length){
_this.rightMove();
_this.slideAuto();
}
}, _this.timeSpace * 1000);
},
clearAuto:function () {
console.log("停止");
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
},
//重置
reset:function(){
this.index=0;
this.transFlag=false;
this.calcXnum();
},
calcXnum:function(){
var _this=this;
this.translateXnum=-(this.index+this.imgList.length)*(100/this.pageNumber);
},
//點(diǎn)擊圓圈跳轉(zhuǎn)圖片
circleClick:function(dex){
this.index=dex;
this.clearAuto();
}
},
mounted()
{
this.$nextTick(function () {
this.imgView();
});
}
}
</script>
這個(gè)組件相對(duì)來(lái)說(shuō)功能比較完整,用戶(hù)可以通過(guò)傳參來(lái)控制當(dāng)前頁(yè)面需要顯示幾張圖片,圖片滾動(dòng)時(shí)間間隔,是否顯示左右點(diǎn)擊按鈕等等,詳細(xì)參數(shù)可以查看props,里面都有注釋。
以上這篇vue實(shí)現(xiàn)圖片滾動(dòng)的示例代碼(類(lèi)似走馬燈效果)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)列表無(wú)縫循環(huán)滾動(dòng)
- vue實(shí)現(xiàn)公告消息橫向無(wú)縫循環(huán)滾動(dòng)
- 基于vue實(shí)現(xiàn)循環(huán)滾動(dòng)列表功能
- Vue實(shí)現(xiàn)一種簡(jiǎn)單的無(wú)限循環(huán)滾動(dòng)動(dòng)畫(huà)的示例
- vue實(shí)現(xiàn)循環(huán)滾動(dòng)列表
- 使用vue?v-for循環(huán)圖片路徑方式
- 在vue中v-for循環(huán)遍歷圖片不顯示錯(cuò)誤的解決方案
- 解決vue的 v-for 循環(huán)中圖片加載路徑問(wèn)題
- vue2.0 循環(huán)遍歷加載不同圖片的方法
- vue實(shí)現(xiàn)循環(huán)滾動(dòng)圖片
相關(guān)文章
Vue中消息橫向滾動(dòng)時(shí)setInterval清不掉的問(wèn)題及解決方法
最近在做項(xiàng)目時(shí),需要進(jìn)行兩個(gè)組件聯(lián)動(dòng),一個(gè)輪詢(xún)獲取到消息,然后將其傳遞給另外一個(gè)組件進(jìn)行橫向滾動(dòng)展示,結(jié)果滾動(dòng)的速度越來(lái)越快。接下來(lái)通過(guò)本文給大家分享Vue中消息橫向滾動(dòng)時(shí)setInterval清不掉的問(wèn)題及解決方法,感興趣的朋友一起看看吧2019-08-08
解決vue動(dòng)態(tài)下拉菜單 有數(shù)據(jù)未反應(yīng)的問(wèn)題
這篇文章主要介紹了解決vue動(dòng)態(tài)下拉菜單 有數(shù)據(jù)未反應(yīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄
這篇文章主要介紹了vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
基于WebRTC實(shí)現(xiàn)音視頻通話(huà)功能
WebRTC作為一種開(kāi)放標(biāo)準(zhǔn)的實(shí)時(shí)通信協(xié)議,能輕松實(shí)現(xiàn)瀏覽器之間的實(shí)時(shí)音視頻通信,本次主要分享基于WebRTC的音視頻通話(huà)技術(shù),講解WebRTC原理和音視頻傳輸?shù)汝P(guān)鍵概念,通過(guò)案例實(shí)踐,帶大家掌握如何搭建一個(gè)音視頻通話(huà)應(yīng)用,需要的朋友可以參考下2024-05-05
vue實(shí)現(xiàn)圖片下載點(diǎn)擊按鈕彈出本地窗口選擇自定義保存路徑功能
vue前端實(shí)現(xiàn)前端下載,并實(shí)現(xiàn)點(diǎn)擊按鈕彈出本地窗口,選擇自定義保存路徑,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12

