为什么我跟着老师写,还是错了?

来源:4-1 无限滚动

任性不是罪

2016-07-19 09:35

window.onload = function(){
	var container = document.getElementById('container');
	var photos = document.getElementById('photos');
	var buttons = document.getElementById('buttons');
	var prev = document.getElementById('prev');
	var next = document.getElementById('next');
	var index = 1;
	
	
	function showButton(){
		buttons[index-1].className = 'on';
	}
		
	function animate(offset){
		var newLeft = parseInt(photos.style.left) + offset;
		photos.style.left = newLeft + 'px';
		if(newLeft > -600){
			photos.style.left = -3000 + 'px';
		}
		if(newLeft < -3000){
			photos.style.left = -600 + 'px';
		}
	}
	
	next.onclick = function(){
		index += 1;
		showButton();
		animate(-600);
		
	}
	prev.onclick = function(){
		index -= 1;
		
		animate(600);
		showButton();
	}
}

就是加上圆点的函数后开始出错的。。。

写回答 关注

1回答

  • 任性不是罪
    2016-07-19 11:08:52

    var buttons = document.getElementById('buttons').getElementsByTagName("span");

    这一句错了,获取的是buttons下面的五个按钮,而不是buttons。

焦点图轮播特效

通过本教程学习您将能掌握非常实用的焦点图轮播特效的制作过程

65296 学习 · 611 问题

查看课程

相似问题