原生js实现表格内容区滚动无线循环滚动,表头固定

 样式

 html

 <div class="boxall" style="height: 300px">
            <div class="alltitle">供应链</div>
            <div class="navboxall scrollable-table">
              <table id="my-table" class="table2" width="100%" border="0" cellspacing="0" cellpadding="0">
               <thead>
                  <tr>
                    <th scope="col">排名</th>
                    <th scope="col">单位</th>
                    <th scope="col">订单</th>
                  </tr>
                </thead>
                  <tbody>
                  <tr>
                    <td><span>1</span></td>
                    <td>xxxxxxxxxxxxxxxxxxxxxxxxxxx</td>
                    <td>11490单<br></td>
                  </tr>
                  <tr>
                    <td><span>2</span></td>
                    <td>xxxxxxxxxxxxxxxxxxxxxxxx</td>
                    <td>9238单</td>
                  </tr>

                  <tr>
                    <td><span>3</span></td>
                    <td>xxxxxxxxxxxxxxxxxxxxxxxx</td>
                    <td>1240单</td>
                  </tr>
                  <tr>
                    <td><span>4</span></td>
                    <td>xxxxxxxxxxxxxxxxxxxxxx</td>
                    <td>5323单</td>
                  </tr>
                  <tr>
                    <td><span>5</span></td>
                    <td>xxxxxxxxxxxxxxxxxxx</td>
                    <td>13423单</td>
                  </tr>
                  <tr>
                    <td><span>6</span></td>
                    <td>xxxxxxxxxxxxxxxxxxx</td>
                    <td>45666单</td>
                  </tr>

                </tbody>
              </table>
            </div>
          </div>

 css

使用定位将内容区固定,滚动animation: scroll 10s linear infinite;鼠标移入时停止滚动animation-play-state: paused;高度不变,移开在滚动基础上滚动

.boxall {
	padding: 15px;
	background: rgba(0, 0, 0, .2);
	position: relative;
	margin-bottom: 15px;
	;
	z-index: 10;
}

.alltitle {
	font-size: 18px;
	color: #fff;
	position: relative;
	padding-left: 12px;
	margin-bottom: 10px;
}

.alltitle:before {
	width: 5px;
	height: 20px;
	top: 2px;
	position: absolute;
	content: "";
	background: #49bcf7;
	border-radius: 20px;
	left: 0;
}

.navboxall {
	height: calc(100% - 30px);
	overflow-y: hidden;
}
.table2 th {
	border-bottom: 1px solid rgba(255, 255, 255, .2);
	font-size: 16px;
	color: rgba(255, 255, 255, .6);
	font-weight: normal;
	padding: 10px 0 10px 0;
}

.table2 td {
	font-size: 16px;
	color: rgba(255, 255, 255, .4);
	padding: 7px 0;
}

.table2 span {
	width: 24px;
	height: 24px;
	border-radius: 3px;
	display: block;
	background: #878787;
	color: #fff;
	line-height: 24px;
	text-align: center;
}

.table2 {
	width: 100%;
	border-collapse: collapse;
}

.table2 th {
	position: sticky;
	/* 固定表头 */
	top: 0;
	background: #0b1545;
	z-index: 1;
	line-height: 2;
}

.table2 tbody {
	position: absolute;
	top: 45px;
	animation: scroll 10s linear infinite;
}

@keyframes scroll {
	0% {
		top: 45px;
	}

	100% {
		top: -76.5%;/*该top可根据表格内容区高度自行调整,保证动画丝滑*/
	}
}

.table2 td:nth-child(1) {
	width: 157px;
}

.table2 td:nth-child(2) {
	width: 393px;
}
.table2 td:nth-child(3) {
    width: 160px;
}

  .table2 tbody:hover {
	animation-play-state: paused; /* 鼠标移过去就暂停滚动,松开继续滚动 */
  }
  .table2 tbody tr:hover {
	background-color: #021455;
  }

js

再复制一份tbody内容插入表格中,实现滚动闭环

 //供应链
    const table2 = document.querySelector('.table2');
    const tableCopy2 = table2.cloneNode(false);
    tableCopy2.classList.add('copy');

    // 复制所有 tbody
    const tbodyList2 = table2.querySelectorAll('tbody');
    tbodyList2.forEach(function (tbody) {
        const tbodyCopy2 = tbody.cloneNode(true);
        // 将克隆的 tbody 中的 tr、td 添加到原表格的 tbody 后面
        Array.from(tbodyCopy2.children).forEach(function (tr) {
            const newTr2 = tr.cloneNode(true);
            table2.querySelector('tbody').appendChild(newTr2);
        });
    });

    table2.parentNode.appendChild(tableCopy2);

锁定表头固定左边列,原生JS原创代码 需要注意的问题: 1.表格的宽度以及表格每一列的宽度需要固定(特殊情况除外:当表格列数少并且表格总宽度明显小于表格父容器的宽度时,也就是表格列不存在自动换行的问题) 2.表格父容器尺寸大小改变时,需要调用 setBoxSize 方法,目的是判断父容器是否出现滚动条 1.1 修正了对IE6、IE7的兼容问题 1.2 修正了固定行、列的样式问题 1.3 IE6/IE7浏览器 不启用该功能 1.4 增加了拆分线, 修正了重复生成锁定行列的问题以及生成错位的问题, 修正了行、列事件无法复制的问题, 增加了IE6/IE7/IE8 启用设置参数(ieLowVersionEnabled: true|false) ,默认不启用, 注:IE6/7/8锁定表头 由于兼容性问题,有些情况下会有一些错位的问题 1.5 增加了 显示/隐藏功能,修正了非IE浏览器下列宽错位问题 1.6 修正了当有合并单元格时,锁定单元格错位的问题,修正了表格行数较多时的性能问题 1.7 表格单元格可以不指定宽度(锁定时,取实际的单元格宽度),当单元格的左右两边的borderWidth不一样时,会有一些错位 若单元格宽度是固定的,可以在第3个参数(config)中指定 isFixedSize:true 修正表头列未锁定的Bug 1.8 修正表格含有thead时的样式丢失问题,以及行数设置问题,修正thead/tbody行归属问题 1.9 移除tbTopLeft的边框线 修正表格cellpadding/cellspacing的设置问题 修复Firefox下的padding导致的错位问题 修复thead复制没有底部分割线的问题 修复thead复制时无背景色的问题 增加复选框同步功能(如果是JS代码设置复选框选中,需要调用 .setCheckBoxSync()方法) .setCheckBoxSync 方法参数说明:4种参数 1) 复选框控件(html object)数组 2) 复选框控件(html object) 3) 复选框控件ID (string) 4) 空参数(尽量不用空参数)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值