js輪播圖之旋轉(zhuǎn)木馬效果
本文實(shí)例為大家分享了js輪播圖之旋轉(zhuǎn)木馬效果的具體代碼,供大家參考,具體內(nèi)容如下
思路:給定一個(gè)數(shù)組,儲(chǔ)存每張圖片的位置,旋轉(zhuǎn)將位置進(jìn)行替換左旋轉(zhuǎn):將數(shù)組第一個(gè)數(shù)據(jù)刪除,然后添加到數(shù)組的最后右旋轉(zhuǎn):將數(shù)組最后一個(gè)數(shù)據(jù)刪除,然后添加到數(shù)組的開(kāi)頭先附上效果圖,再來(lái)實(shí)現(xiàn)
接下來(lái)就是最主要的,封裝原生js動(dòng)畫(huà)函數(shù)
//封裝函數(shù)獲取任意一個(gè)元素的任意屬性的值(兼容ie8)function getStyle(element, attr) { return window.getComputedStyle ? window.getComputedStyle(element, null)[attr] : element.currentStyle[attr];}//封裝js變速動(dòng)畫(huà)function animate(element, json, fn) { //每次啟動(dòng)定時(shí)器之前先停止 clearInterval(element.tmId); element.tmId = setInterval(function () { var flag = true; //遍歷對(duì)象中的每個(gè)屬性 for (var attr in json) { //執(zhí)行透明度動(dòng)畫(huà) if (attr == 'opacity') { //獲取當(dāng)前元素的屬性值 var current = parseInt(getStyle(element, attr)*100); //獲取目標(biāo)值 var target = json[attr]*100; //移動(dòng)的步數(shù) var step = (target - current) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); //移動(dòng)后的值 current += step; element.style[attr] = current / 100; } else if (attr == 'zIndex') { //改變層級(jí)屬性 element.style[attr] = json[attr]; } else { //獲取當(dāng)前元素的屬性值 var current = parseInt(getStyle(element, attr)); //獲取目標(biāo)值 var target = json[attr]; //移動(dòng)的步數(shù) var step = (target - current) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); //移動(dòng)后的值 current += step; element.style[attr] = current + 'px'; if (current != target) { flag = false; } } } if (flag) { clearInterval(element.tmId); //如果有回調(diào)函數(shù)就調(diào)用 if (fn) fn(); } // 測(cè)試 // console.log('目標(biāo):' + target + '/當(dāng)前:' + current + '/步數(shù):' + step); }, 20);}
封裝完函數(shù),剩下的直接調(diào)用就可以了,最后附上旋轉(zhuǎn)木馬完整代碼?
<!DOCTYPE html><html><head lang='en'> <meta charset='UTF-8'> <title>旋轉(zhuǎn)木馬輪播圖</title> <link rel='stylesheet' href='http://www.cgvv.com.cn/bcjs/css/css(1).css' rel='external nofollow' /> <script src='http://www.cgvv.com.cn/bcjs/common.js'></script> <script> var config = [ { width: 400, top: 20, left: 50, opacity: 0.2, zIndex: 2 },//0 { width: 600, top: 70, left: 0, opacity: 0.8, zIndex: 3 },//1 { width: 800, top: 100, left: 200, opacity: 1, zIndex: 4 },//2 { width: 600, top: 70, left: 600, opacity: 0.8, zIndex: 3 },//3 { width: 400, top: 20, left: 750, opacity: 0.2, zIndex: 2 }//4 ]; window.onload = function () { var flag = true; var list = $query('#slide').getElementsByTagName('li'); function flower() { //1、圖片散開(kāi) for (var i = 0; i < list.length; i++) { //設(shè)置每個(gè)li的寬,透明度,left,top,zindex animate(list[i], config[i], function () { flag = true; }); } } flower();//初始化調(diào)用函數(shù) //按鈕的顯示與隱藏 $query('#slide').onmouseover = function () { $query('#arrow').style.opacity = '1'; } $query('#slide').onmouseout = function () { $query('#arrow').style.opacity = '0'; } //點(diǎn)擊切換 $query('#arrLeft').onclick = function () { if (flag) { config.unshift(config.pop()); flower(); flag = false; } } $query('#arrRight').onclick = function () { if (flag) { config.push(config.shift()); flower(); flag = false; } } //自動(dòng)切換 setInterval(function () { config.push(config.shift()); flower(); }, 2000); } </script></head><body><div id='wrap'> <div id='slide'> <ul> <li><a href='http://www.cgvv.com.cn/bcjs/14370.html#'><img src='http://www.cgvv.com.cn/bcjs/images/slidepic1.jpg' alt=''/></a></li> <li><a href='http://www.cgvv.com.cn/bcjs/14370.html#'><img src='http://www.cgvv.com.cn/bcjs/images/slidepic2.jpg' alt=''/></a></li> <li><a href='http://www.cgvv.com.cn/bcjs/14370.html#' ><img src='http://www.cgvv.com.cn/bcjs/images/slidepic3.jpg' alt=''/></a></li> <li><a href='http://www.cgvv.com.cn/bcjs/14370.html#'><img src='http://www.cgvv.com.cn/bcjs/images/slidepic4.jpg' alt=''/></a></li> <li><a href='http://www.cgvv.com.cn/bcjs/14370.html#'><img src='http://www.cgvv.com.cn/bcjs/images/slidepic5.jpg' alt=''/></a></li> </ul> <div id='arrow'> <a href='javascript:void(0);' id='arrLeft'></a> <a href='javascript:void(0);' id='arrRight'></a> </div> </div></div></body></html>
精彩專(zhuān)題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 測(cè)試模式 - XSL教程 - 52. python b站視頻下載的五種版本3. vue實(shí)現(xiàn)簡(jiǎn)易圖片左右旋轉(zhuǎn),上一張,下一張組件案例4. 每日六道java新手入門(mén)面試題,通往自由的道路第二天5. Python結(jié)合百度語(yǔ)音識(shí)別實(shí)現(xiàn)實(shí)時(shí)翻譯軟件的實(shí)現(xiàn)6. 解決Java中的java.io.IOException: Broken pipe問(wèn)題7. python如何寫(xiě)個(gè)俄羅斯方塊8. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)9. 教你JS更簡(jiǎn)單的獲取表單中數(shù)據(jù)(formdata)10. JAVA抽象類(lèi)及接口使用方法解析
