直接上代码。
主要的思路是:1.复制节点添加到最后。2.移动left值。3.删除前面多余的节点,重置left值。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <!doctype html> <html> <head> <meta charset="utf-8"> <title></title> </head> <style type="text/css"> *{ margin: 0; padding:0;} li{ list-style: none; } div{ margin: 0 auto; width: 350px; height:60px; } ul{ position: absolute; left: 0; } </style> <body> <div> <input type="button" value="<<<<<<<<<<" id="input1" /> <input type="button" value=">>>>>>>>>>" id="input2" /> </div> <div id="div1"> <ul id="ul1"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> </body> <script> var oInput1 = document.getElementById('input1'); var oInput2 = document.getElementById('input2'); var oDiv = document.getElementById('div1'); var oUl1 = document.getElementById('ul1'); var aLi = document.getElementsByTagName('li'); var onSize = aLi[0].offsetWidth + 10; var startMove = true; function getWidth(){ oUl1.style.width = aLi.length * onSize + 'px'; } getWidth() //剪切节点 不适用于本例子 /*oInput1.onclick = function(){ oUl1.appendChild(aLi[0]); } oInput2.onclick = function(){ oUl1.insertBefore(aLi[aLi.length-1],aLi[0]); }*/
//复制节点 cloneNode(boolean) : true:同时复制所有子节点 flase:只复制当前节点 var iNum = 4; oInput1.onclick = function(){ if(startMove){ startMove = false; oUl1.style.transition = "1s"; for(var i=0;i<iNum;i++){ var oLi = aLi[i].cloneNode(true); oUl1.appendChild(oLi); getWidth(); } oUl1.style.left = -i*onSize + 'px';
setTimeout(function(){ oUl1.style.transition = "0s"; for(var i=0;i<iNum;i++){ oUl1.removeChild(aLi[0]); } oUl1.style.left = 0; startMove = true; },1000) } }
var colors = ['red','blue','yellow','green','orange','red','pink','red','blue','orange','green']; for(var i=0;i<aLi.length;i++){ aLi[i].style.background = colors[parseInt(Math.random()*10)] } </script> </html>
|