欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實現(xiàn)列表循環(huán)滾動

 更新時間:2022年07月14日 14:10:21   作者:葉林染  
這篇文章主要為大家詳細介紹了js實現(xiàn)列表循環(huán)滾動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了js實現(xiàn)列表循環(huán)滾動的具體代碼,供大家參考,具體內(nèi)容如下

先介紹幾個屬性

  • clientHeight 元素的高度
  • clientTop 元素頂部邊框的寬度
  • scrollTop 滾動條遮擋的部分的高度(包含border
  • scrollHeight 整個內(nèi)容的高度(包含border
  • offsetTop 距離上一個 position 不為 static(默認) 的元素的頂部內(nèi)邊框的距離
<!DOCTYPE html>
<html lang="en">

<head>
? ? <meta charset="UTF-8" />
? ? <title>列表循環(huán)滾動</title>
</head>
<style>
? ? html,
? ? body {
? ? ? ? height: 100%;
? ? ? ? width: 100%;
? ? ? ? overflow: hidden;
? ? ? ? background-color: #999;
? ? }

? ? .parent {
? ? ? ? width: 728px;
? ? ? ? margin: 200px auto;
? ? ? ? height: 200px;
? ? ? ? overflow: hidden;
? ? ? ? background-color: #fff;
? ? }
</style>

<body>
? ? <div id="parent" class="parent">
? ? ? ? <table border="1" cellpadding="18" cellspacing="0" id="child" class="child">
? ? ? ? </table>
? ? ? ? <div id="cloneChild" class="child"></div>
? ? </div>
? ? <script type="text/javascript">

? ? ? ? let parent = document.getElementById('parent');
? ? ? ? let child = document.getElementById('child');

? ? ? ? let str = '';
? ? ? ? for (let i = 0; i < 10; i++) {
? ? ? ? ? ? str += `<tr>`;
? ? ? ? ? ? for (let j = 0; j < 6; j++) {
? ? ? ? ? ? ? ? str += `<td>第${i}行第${j}列</td>`;
? ? ? ? ? ? }
? ? ? ? ? ? str += `</tr>`
? ? ? ? }

? ? ? ? child.innerHTML = str;
? ? ? ? let cloneChild = document.getElementById('cloneChild');
? ? ? ? // 深度克隆一份表格 相比 innerHTML 的優(yōu)勢在于可以克隆元素的全部的屬性
? ? ? ? let cloneNoe = child.cloneNode(true);
? ? ? ? // 追加到 parent 里面 做無縫切換視覺效果
? ? ? ? parent.appendChild(cloneNoe);

? ? ? ? (function () {
? ? ? ? ? ? setInterval(function () {
? ? ? ? ? ? ? ? // parent.scrollTop + parent.clientHeight = child.scrollHeight;
? ? ? ? ? ? ? ? // child.scrollHeight - parent.scrollTop = parent.clientHeight;
? ? ? ? ? ? ? ? // 讓他多滾動 parent 一顯示區(qū)域的高度。再跳到 最頂部 ,正好 給人一種在不斷滾動的錯覺
? ? ? ? ? ? ? ? if (parent.scrollTop >= child.scrollHeight) {
? ? ? ? ? ? ? ? ? ? parent.scrollTop = 0;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? parent.scrollTop++;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }, 20);
? ? ? ? })()

? ? </script>
</body>

</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論