vue自定義js圖片碎片輪播圖切換效果的實(shí)現(xiàn)代碼
定義一個(gè)banner.js文件,代碼如下
;window.requestAnimationFrame = window.requestAnimationFrame||function(a){return setTimeout(a,1000/60)}; window.cancelAnimationFrame = window.cancelAnimationFrame||clearTimeout; var FragmentBanner = function(option) { //實(shí)例化時(shí),可傳的參數(shù) this.whiteList = ['container','controller','size','imgs','size','grid','index','fnTime','boxTime','type']; //容器將包容控制器 this.container = '.banner'; //默認(rèn)的控制器 this.controller = { view : '.banner-view', btn : '.banner-btn', num : '.banner-number', progre : '.banner-progres' }; //柵格 行*列 this.grid = { line : 5, list : 10 }; //容器的大小 this.size = { width : 1200, height : 675, }; //切換類型 this.type = 1; //索引位置 this.index = 0; //函數(shù)每次切換時(shí)間 this.fnTime = 5000; //柵格每次運(yùn)動(dòng)時(shí)間 this.boxTime = 2000; //柵格運(yùn)動(dòng)結(jié)束的時(shí)間 this.activeTime = new Date(); for(var a = 0,attrLenght = this.whiteList.length; a < attrLenght;a++){ var attr = this.whiteList[a]; if(option[attr] != undefined){ this[attr] = option[attr]; } } for(var i in option){ if(this.whiteList[i] !== undefined){ ; } } init(); } function setIndex(){ this.index %= this.imgs.length; this.index = (this.index < 0) ? this.imgs.length - 1 : this.index; this.elem.numFind[this.index].className = 'on'; } function init(){ this.container = document.querySelector(this.container) if(!this.container){ return alert('獲取banner容器失敗'); }else{ this.container.style.width = this.size.width+'px'; this.container.style.height = this.size.height+'px'; } this.elem = {}; for(var e in this.controller){ this.elem[e] = this.container.querySelector(this.controller[e]); if(this.elem[e] == null){ return alert('獲取'+e+'容器'); } } //柵格 var w = this.size.width / this.grid.list, h = this.size.height / this.grid.line; this.elem.viewBox = new Array(); for(var i = 0,iL = this.grid.line;i < iL;i++){ for(var j = 0,jL = this.grid.list;j < jL;j++){ var newI = document.createElement('i'); setCss(newI,{ width : w+'px', height : h+'px', left : 0, top : 0, opacity : 1, backgroundImage : 'url("'+this.imgs[this.index]+'")', backgroundSize : this.size.width + 'px ' + this.size.height +'px', backgroundPosition : w * -j+'px ' + h * -i+'px' }); this.elem.view.appendChild(newI); this.elem.viewBox.push(newI); } } //按鈕動(dòng)作 for (var b = 1; b >= 0; b--) { var oB = document.createElement('span'); (b) ? oB.innerHTML = '<' : oB.innerHTML = '>'; oB.setIndex = b; oB.onclick = function(obj){ show({ switch : true, change : obj.setIndex == 0 }); }.bind(this,oB); this.elem.btn.appendChild(oB); } //數(shù)量 for(var n = 0,nL = this.imgs.length; n < nL;n++){ var oI = document.createElement('i'); oI.setIndex = n; oI.onclick = function(obj){ //顯示動(dòng)畫 show({ switch : true, change : obj.setIndex }); }.bind(this,oI) this.elem.num.appendChild(oI); } this.elem.numFind = this.elem.num.querySelectorAll('i'); //進(jìn)度條 this.progre = new Array; for(var p = 1;p >= 0;p--){ var oP = document.createElement('i'); setCss(oP,{ width : 0, backgroundColor : p ? '#00c3ff' : '#ffc300' }); this.elem.progre.appendChild(oP); this.progre.push(oP); } //顯示動(dòng)畫 show(); this.elem.numFind[this.index].className = 'on'; } function setCss(obj,json){ for( c in json){ if(c == 'opacity'){ obj.style.opacity = c; obj.style.filter = "alpha(opacity="+ (json[c]*100) +")"; }else{ obj.style[c] = json[c]; } } return this; } function show(order){ order = order || {}; if(new Date() >= this.activeTime){ this.elem.numFind[this.index].className = ''; // 下次播放動(dòng)畫時(shí)候的進(jìn)度條 setCss(this.progre[1],{width : 0}) anime(this.progre[1],{ width : this.size.width },this.fnTime,function(){ show({ switch : true, change : true }); }.bind(this)); var status = true, activeTime = 0; for( var i = 0,iL = this.elem.viewBox.length;i < iL;i++ ){ var startTime = getTypeTime(), endTime = getTypeTime(), obj = this.elem.viewBox[i]; activeTime = Math.max(activeTime,startTime[1] + endTime[1]); anime(obj,{ left : Math.ceil(Math.random() * this.size.width * 2 - this.size.width), top : Math.ceil(Math.random() * this.size.height * 2 - this.size.height), opacity: 0 }, startTime[0] ,function(obj){ if(order.switch && status){ if(/number/i.test(typeof order.change)){ this.index = order.change; }else{ (order.change) ? ++this.index : --this.index; } setIndex(); this.elem.numFind[this.index].className = 'on'; status = false; } setCss(obj,{backgroundImage : 'url("'+this.imgs[this.index]+'")'}) anime(obj,{ left : 0, top : 0, opacity : 1 },endTime[0]); }.bind(this,obj)); } //柵格結(jié)束運(yùn)動(dòng)的時(shí)間 this.activeTime = new Date(new Date().getTime() + activeTime); setCss(this.progre[0],{width : 0}) anime(this.progre[0],{width : this.size.width},activeTime); } } function getTypeTime(){ var timer = new Array(); switch(this.type){ case 1: timer.push(this.boxTime / 4 + Math.random() * this.boxTime / 4); timer.push(timer[0]); break; default: timer.push([Math.random() * this.boxTime / 5,this.boxTime / 10 * 3]); timer.push(timer[0][0] + timer[0][1]); break; } return timer; } function anime(obj,attr,endTime,callback) { (obj.timer) && cancelAnimationFrame(obj.timer); var cssJosn = obj.currentStyle || getComputedStyle(obj,null), start = {},end = {},goTime; //設(shè)置初始屬性值和結(jié)束屬性值 for(var key in attr){ if(attr[key] != parseFloat(cssJosn[key])){ start[key] = parseFloat(cssJosn[key]); end[key] = attr[key] - start[key]; } } goTime = new Date(); if(endTime instanceof Array){ (function delayFn(){ if((new Date() - goTime) >= endTime[0]){ endTime = endTime[1]; goTime = new Date(); ref(); }else{ obj.timer = requestAnimationFrame(delayFn); } })(); }else{ ref(); } function ref(){ var prop = (new Date() - goTime) / endTime; (prop >= 1) ? prop = 1 : obj.timer = requestAnimationFrame(ref); for(var key in start){ var val = -end[key] * prop *(prop-2) + start[key]; if(key == 'opacity'){ obj.style.opacity = val; obj.style.filter = "alpha(opacity="+ (val*100) +")"; }else{ obj.style[key] = val+'px'; } } (prop === 1) && callback && callback.call(obj); }; } module.exports.FragmentBanner = FragmentBanner;
在需要的組件里面引入改js
import {FragmentBanner} from '../../../static/js/banner.js'
使用方式如下
// html代碼 <header class="js_header mod-header"> <div class="banner" id="banner1" style="margin: 50px auto;margin-bottom:0px"> <div class="banner-view"></div> <div class="banner-btn"></div> <div class="banner-number"></div> <div class="banner-progres"></div> </div> </header> //css樣式 <style> .banner{ position: relative; overflow: hidden; } .banner-view{ position: relative; height: 100%; z-index: 999; background-color: #090b22; background-repeat: no-repeat; } .banner-view i{ position: relative; display: block; float: left; background-repeat: no-repeat; } .banner-btn{ position: absolute; width: 100%; height: 0; top: 45%; font-family: "宋體"; font-size: 20px; z-index: 999; } .banner-btn span{ display: block; float: left; width: 50px; line-height: 50px; text-align: center; background-color: #27cfc3; color: white; cursor: pointer; font-weight: 800; /* position: absolute; right:-150px; */ /* background-image: url(../../../static/images/style-index.d437fb5e.png); background-position: -268px -18px; width: 56px; height: 56px; */ } /* .banner-btn span:first-child{ left: -150px; } */ .banner-btn span:hover{ background-color: rgba(0,0,0,0.6); } .banner-btn span + span{ float: right; } .banner-number{ position: absolute; bottom: 35px; width: 100%; height: 0; font-size: 0; text-align: center; z-index: 1000; } .banner-number > *{ display: inline-block; border: 2px solid #fff; border-radius: 50%; margin: 0 8px; width: 10px; height: 10px; background-color: #00c3ff; cursor: pointer; } .banner-number > *:hover, .banner-number > *.on{ background-color: #ffc300; } .banner-progres{ width: 100%; position: absolute; bottom: 0; height: 3px; z-index: 1000; } .banner-progres i{ position: absolute; left: 0; top: 0; border-radius: 3px; display: block; height: 100%; width: 0; } </style> // js的引用 this.obj = { container : '#banner1',//選擇容器 必選 imgs : ['https://www.xuanfengge.com/wp-content/themes/lee3.0/dist/img/banner/1.0bd56759.jpg','https://www.xuanfengge.com/wp-content/themes/lee3.0/dist/img/banner/6.c6b4ec87.jpg'],//圖片集合 必選 size : { width : 1200, height : 300 },//容器的大小 可選 //行數(shù)與列數(shù) 可選 grid : { line : 12, list : 14 }, index: 0,//圖片集合的索引位置 可選 type : 2,//切換類型 1 , 2 可選 boxTime : 5000,//小方塊來回運(yùn)動(dòng)的時(shí)長(zhǎng) 可選 fnTime : 10000//banner切換的時(shí)長(zhǎng) 可選 } mounted () { FragmentBanner(this.obj ); }
總結(jié)
以上所述是小編給大家介紹的vue自定義js圖片碎片輪播圖切換效果的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
vue項(xiàng)目記錄鎖定和解鎖功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目記錄鎖定和解鎖功能實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vue實(shí)現(xiàn)列表無縫滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)列表無縫滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Vue動(dòng)態(tài)生成el-checkbox點(diǎn)擊無法賦值的解決方法
這篇文章主要給大家介紹了關(guān)于Vue動(dòng)態(tài)生成el-checkbox點(diǎn)擊無法賦值的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Vue使用高德地圖搭建實(shí)時(shí)公交應(yīng)用功能(地圖 + 附近站點(diǎn)+線路詳情 + 輸入提示+換乘詳情)
這篇文章主要介紹了vue中使用高德地圖搭建實(shí)時(shí)公交應(yīng)用(地圖 + 附近站點(diǎn)+線路詳情 + 輸入提示+換乘詳情),主要是讓大家熟悉下高德地圖在vue中的使用及vue的常用指令,需要的朋友可以參考下2018-05-05解決vscode?Better?Comments插件在vue文件中不顯示相對(duì)應(yīng)的顏色的問題
最近使用了Better?Comments這款插件,發(fā)現(xiàn)在ts文件中可以顯示對(duì)應(yīng)的顏色,但在vue文件中并不顯示對(duì)應(yīng)顏色?,下面小編給大家分享解決方法,感興趣的朋友跟隨小編一起看看吧2022-09-09詳解Vue 普通對(duì)象數(shù)據(jù)更新與 file 對(duì)象數(shù)據(jù)更新
本篇文章主要介紹了詳解Vue 普通對(duì)象數(shù)據(jù)更新與 file 對(duì)象數(shù)據(jù)更新 ,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04vuejs+element-ui+laravel5.4上傳文件的示例代碼
本篇文章主要介紹了vuejs+element-ui+laravel5.4上傳文件的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08vue+flask實(shí)現(xiàn)視頻合成功能(拖拽上傳)
這篇文章主要介紹了vue+flask實(shí)現(xiàn)視頻合成功能(拖拽上傳),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03