动态效果
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>table-auto-scroll</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<style>
body {
height: 100%;
width: 99%;
}
div {
height: 300px;
width: 100%;
overflow-y: auto;
}
table {
border-collapse: collapse;
border: 1px solid #666;
}
tr td {
border: 1px solid #666;
width: 120px;
}
</style>
</head>
<body>
<div id="scroll-div1">
<table id="table1"></table>
</div>
<script>
var timer;
$(function() {
initTable("table1", 60, 14);
timer = setInterval(function() {
autoscroll("scroll-div1", 1);
}, 10);
});
function initTable(eleId, row, col) {
var $table = $(`#${eleId}`);
for (let i = 0; i < row; i++) {
let $tr = $(`<tr></tr>`);
for (let j = 0; j < col; j++) {
let $td = $(`<td>Rows-${i} Columns-${j}</td>`);
$tr.append($td);
}
$table.append($tr);
}
}
function autoscroll(eleId, speed) {
const ele = $(`#${eleId}`);
ele.scrollTop(ele.scrollTop() + speed);
if (ele.scrollTop() + ele.height() >= ele.get(0).scrollHeight) {
ele.scrollTop(0);
}
}
</script>
</body>
</html>
上面大多代码用来构造表格,实际上核心代码只有几行
$(function() {
setInterval(function() {
const ele = $('#table1');
ele.scrollTop(ele.scrollTop() + 1);
if (ele.scrollTop() + ele.height() >= ele.get(0).scrollHeight) {
ele.scrollTop(0);
}
}, 10);
});