您好,登錄后才能下訂單哦!
使用js實(shí)現(xiàn)輪播圖旋轉(zhuǎn)木馬效果?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
思路:給定一個(gè)數(shù)組,儲(chǔ)存每張圖片的位置,旋轉(zhuǎn)將位置進(jìn)行替換
左旋轉(zhuǎn):將數(shù)組第一個(gè)數(shù)據(jù)刪除,然后添加到數(shù)組的最后
右旋轉(zhuǎn):將數(shù)組最后一個(gè)數(shù)據(jù)刪除,然后添加到數(shù)組的開頭
先附上效果圖,再來實(shí)現(xiàn)
接下來就是最主要的,封裝原生js動(dòng)畫函數(shù)
//封裝函數(shù)獲取任意一個(gè)元素的任意屬性的值(兼容ie8) function getStyle(element, attr) { return window.getComputedStyle ? window.getComputedStyle(element, null)[attr] : element.currentStyle[attr]; } //封裝js變速動(dòng)畫 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)畫 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="css/css(1).css" rel="external nofollow" /> <script src="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、圖片散開 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 class="wrap" id="wrap"> <div class="slide" id="slide"> <ul> <li><a href="#"><img src="images/slidepic1.jpg" alt=""/></a></li> <li><a href="#"><img src="images/slidepic2.jpg" alt=""/></a></li> <li><a href="#" ><img src="images/slidepic3.jpg" alt=""/></a></li> <li><a href="#"><img src="images/slidepic4.jpg" alt=""/></a></li> <li><a href="#"><img src="images/slidepic5.jpg" alt=""/></a></li> </ul> <div class="arrow" id="arrow"> <a href="javascript:void(0);" class="prev" id="arrLeft"></a> <a href="javascript:void(0);" class="next" id="arrRight"></a> </div> </div> </div> </body> </html>
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。