纯js无缝滚动效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>无缝滚动</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			#div {
				position: relative;
				width: 713px;
				height: 200px;
				background: yellow;
				margin: 100px auto;
				overflow: hidden;
			}

			#div ul {
				border: 1px red solid;
				position: absolute;
				left: 0;
				top: 0;
			}

			#div ul li {
				list-style: none;
				float: left;
				width: 178px;
				height: 108px;
			}

			#div ul li img {
				width: 178px;
				height: 108px;
			}
		</style>
		<script type="text/javascript">
			window.onload = function() {
				var oDiv = document.getElementById('div');
				var oUl = oDiv.getElementsByTagName('ul')[0];
				var oLi = oDiv.getElementsByTagName('li');
				oUl.innerHTML = oUl.innerHTML + oUl.innerHTML;
				oUl.style.width = oLi[0].offsetWidth * oLi.length + 'px';
				setInterval(function() {
					if (oUl.offsetLeft > 0) {
						oUl.style.left = -parseInt(oUl.offsetWidth) / 2 + 'px';
					}
					if (oUl.offsetLeft < -parseInt(oUl.offsetWidth) / 2) {
						oUl.style.left = 0;
					}
					oUl.style.left = oUl.offsetLeft + 2 + 'px';
				}, 30)

			}
		</script>
	</head>
	<body>
		<div id="div">
			<ul>
				<li><img src='images/b1.jpg'></li>
				<li><img src='images/b2.jpg'></li>
				<li><img src='images/b3.jpg' /></li>
				<li><img src='images/b4.jpg'></li>
			</ul>
		</div>
	</body>
</html>

posted @ 2023-10-10 09:57  songxia777  阅读(116)  评论(0编辑  收藏  举报