jq实现表格按行滚动

jq实现表格按行滚动

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>表格按行滚动</title>
		<style>
			.container {
				width: 80%;
				height: 120px;
				overflow: hidden;
				border: 1px solid #009688;
			}

			.table {
				width: 100%;
				text-align: center;
				border-collapse: collapse;
				border-spacing: 0;
			}

			.table thead {
				background-color: #0000FF;
			}

			.table tbody {
				position: relative;
				background-color: #009688;
				z-index: -1;
			}
		</style>
	</head>
	<body>
		<div class="container">
			<table class="table">
				<thead>
					<tr>
						<th>生产线</th>
						<th>日下线</th>
						<th>已合格</th>
						<th>未合格</th>
					</tr>
				</thead>
				<tbody>
					<!-- 表格内容 -->
				</tbody>
			</table>
		</div>

		<!-- jq实现表格滚动 -->
		<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
		<script>
			(function() {
				//用于清除定时器
				var tabelTimer = null;
				//刷新表格数据
				function getData() {
					// 1.添加表格内容
					var tbody = $('.table tbody');
					tbody.empty();
					for(var i=0; i<10; i++) {
						var tr = "<tr><td>" + i +
							"</td><td>" + i +
							"</td><td>" + i +
							"</td><td>" + i +
							"</td></tr>";
						tbody.append(tr);
					}
					
					//2.表格滚动
					var rowList = $('.table tbody tr');
					var top = 0;
					var index = 0;
					
					//清除上一个定时器
					if (tabelTimer) {
						clearInterval(tabelTimer);
					}
					
					//定时器执行表格滚动
					tabelTimer = setInterval(function() {
						// jq动画
						tbody.animate({
							top: -top + 'px'
						}, "slow");
						
						top += rowList[index].clientHeight;
						index += 1;
						if (index == rowList.length) {
							top = 0;
							index = 0;
						}
						
					}, 1000);
				}
				
				getData();
				
				//定时刷新数据
				setInterval(function() {
					getData();
				}, 20000)

			})();
		</script>
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值