WEB前端:05_scroll滚动(图片/文字滚动)

scroll滚动(图片/文字滚动)

网站常用效果之一,以下为简化版,用于学习javascript基础知识。

效果图:

scroll滚动(图片/文字滚动)- 纯JS简化版

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>scroll滚动 - 纯js简化版</title>
<style type="text/css">
*{margin: 0; padding: 0;}
#probox{ position: relative; border: 1px solid #ccc; 1000px;overflow:hidden;height:120px; margin: 10px auto;}
#prolist{ position: relative; 2000px; height: 120px; list-style: none; margin: 0; padding: 0;}
#prolist li {float: left;  250px; height: 120px; text-align: center;}
#left,#right{ background: #333; color: #fff; padding: 5px; cursor:pointer;}
</style>
<script type="text/javascript">
window.onload = function() {
	var probox = document.getElementById('probox');
	var prolist = document.getElementById('prolist');
	var prolistli = prolist.getElementsByTagName('li');
	var btnleft = document.getElementById('left');
	var btnright = document.getElementById('right');

	var cut = prolistli[1].offsetLeft - prolistli[0].offsetLeft;
	var len = prolist.offsetLeft;
	var timer = null;

	function autoplay() {
		if(timer) {
			clearInterval(timer);
		}
		timer = setInterval(function() {
			if(prolist.offsetLeft == -cut) {
				clearInterval(timer);
				prolist.appendChild(prolistli[0]);
				prolist.style.left = "0px";
			} else {
				prolist.style.left = prolist.offsetLeft - 10 + "px";
			}
		}, 30)
	}

	probox.onmouseover = function() {
		clearInterval(o);
	}

	probox.onmouseout = function() {
		o = setInterval(autoplay, 3000);
	}

	btnleft.onclick = function() {
		clearInterval(o);
		if(timer) {
			clearInterval(timer);
		}
		
		timer = setInterval(function() {
			if(prolist.offsetLeft == 0) {
				clearInterval(timer);
				prolist.insertBefore(prolistli[prolistli.length - 1], prolist.firstChild);
				prolist.style.left = -cut + "px";
			} else {
				prolist.style.left = prolist.offsetLeft + 10 + "px";
			}
		}, 30);

		o = setInterval(autoplay, 3000);
	}

	btnright.onclick = function() {
		clearInterval(o);
		if(timer) {
			clearInterval(timer);
		}
		timer = setInterval(function() {
			if(prolist.offsetLeft == -cut) {
				clearInterval(timer);
				prolist.appendChild(prolistli[0]);
				prolist.style.left = "0px";
			} else {
				prolist.style.left = prolist.offsetLeft - 10 + "px";
			}
		}, 30);

		o = setInterval(autoplay, 3000);
	}

	o = setInterval(autoplay, 3000);

}
</script>
</head>
<body>

<div id="probox">
	<ul id="prolist">
		<li><img src="tab1.jpg" width="220" height="120" /></li>
		<li><img src="tab2.jpg" width="220" height="120" /></li>
		<li><img src="tab3.jpg" width="220" height="120" /></li>
		<li><img src="tab4.jpg" width="220" height="120" /></li>
		<li><img src="tab5.jpg" width="220" height="120" /></li>
		<li><img src="tab6.jpg" width="220" height="120" /></li>
		<li><img src="tab7.jpg" width="220" height="120" /></li>
		<li><img src="tab8.jpg" width="220" height="120" /></li>
	</ul>
</div>
<span id="left">上一个</span> <span id="right">下一个</span>

</body>
</html>
原文地址:https://www.cnblogs.com/haicheng/p/3718565.html