<div class="parent"> <div class="child" data-index="0" style="background-color: red;">1</div> <div class="child" style="background-color: rebeccapurple;">2</div> <div class="child" style="background-color: yellowgreen;">3</div></div>js代码如下:(function(){ var child_arr = document.getElementsByClassName("child"); var len = child_arr.length; var parent = document.getElementsByClassName("parent")[0]; var move_width = parent.offsetWidth; var index = 0; // 保存当前显示的div索引 function animate() { var childArr = [].slice.call(child_arr); if (index === len) { index = 0; } childArr.forEach(function(item, key) { if (index - key >= 0) { child_arr[key].style.marginLeft = '-' + move_width * (index - key) + 'px'; } else { child_arr[key].style.marginLeft = move_width * (key - index) + 'px'; } }) index++; } clearInterval(t); var t = setInterval(animate, 1000);})()我这种写法就不行,为什么呢?我依然是对Dom元素操作的啊,有没有人知道原因的?求教
添加回答
举报
0/150
提交
取消