- 更新,鼠标移入暂停滚动,鼠标移出继续滚动。
- html 实现,可以根据实现思路平移到 vue 或 react.
1 效果
2 思路
- 第一行设置高度0,实现隐藏效果
- 等隐藏动画执行完成后,移除第一行,恢复样式后 append 到最后一行
3 实现代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>轮播表</title>
<style>
.wrap {
position: relative;
overflow: hidden;
width: 300px;
height: 200px;
}
.scroll-table {
width: 100%;
cursor: pointer;
}
.scroll-table .row {
display: flex;
justify-content: space-around;
height: 38px;
line-height: 38px;
border-bottom: 1px solid rgba(0, 0, 44, .5);
overflow: hidden;
will-change: height;
transition: height 0.3s ease-in-out;
}
.scroll-table .row span {
flex: 1;
text-align: center;
}
</style>
</head>
<body>
<div class="wrap">
<div class="scroll-table"></div>
</div>
<script>
const table = document.querySelector('.scroll-table');
for (let i = 0; i < 12; i++) {
const row = document.createElement('div');
row.className = 'row'
for (let j = 0; j < 5; j++) {
const cell = document.createElement('span');
cell.innerText = Math.floor(Math.random() * (200 - 1) + 1);
row.appendChild(cell)
}
table.appendChild(row)
}
let interval =null ;
startScroll()
table.addEventListener("mouseenter",() => {
clearInterval(interval)
});
table.addEventListener("mouseleave",()=> {
startScroll()
});
function startScroll() {
interval = setInterval(() => {
let hideRow = table.children[0];
hideRow.style.height = 0
hideRow.style.lineHeight = 0
setTimeout(() => {
table.removeChild(hideRow)
hideRow.style.height = '38px';
hideRow.style.lineHeight = '38px'
table.appendChild(hideRow)
}, 300);
}, 2000);
}
</script>
</body>
</html>