0

0

HTML页面table滚动条的实现

不言

不言

发布时间:2018-05-08 15:14:23

|

5716人浏览过

|

来源于php中文网

原创

这篇文章主要介绍了关于HTML页面table滚动条的实现 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

table scrollbar & header fixed

有很多页面由于数据量过大,会产生横纵滚动条。为了便于使用者观看,我们要把表头固定一下。根据这个需求写了个demo来实现这个功能。
  • 主要解决了几点问题:

  • 1.table实现横纵滚动条

  • 2.table表头固定

  • 3.table列宽自适应

  • 4.table内容不换行

    立即学习前端免费学习笔记(深入)”;

主要代码块

css:

            .table-scroll {
                width: calc(100% - 5px);
                overflow-x: scroll;
                white-space: nowrap;
            }

            .table-scroll table {
                table-layout: fixed;
                width: calc(100% - 10px);
                background-color:darkseagreen ;
            }

            .table-scroll thead {
                display: table-row;
                background-color: bisque;
            }

            .table-scroll tbody {
                overflow-y: scroll;
                overflow-x: hidden;
                display: block;
                height: calc(100vh - 300px);
            }

            .table-scroll th,td {
                width: 160px;
                overflow: hidden;
                text-overflow: ellipsis;
                min-width: 160px;
                border: 1px solid #808080;
            }

            h4, h5 {
                color: cornflowerblue;
            }

js:

页面出现滚动条后才出现的返回顶部代码
页面出现滚动条后才出现的返回顶部代码

页面出现滚动条后才出现的返回顶部代码

下载
    $(function() {
            $('.table-scroll').scroll(function() {
                  $('.table-scroll table').width($('.table-scroll').width() 
                  + $('.table-scroll').scrollLeft());
                });

                var tableTdWidths = new Array();
            var tableWidth = 0;
            var tableTr0Width = 0;
            var tableThNum = 0;
            var tableTr1Width = 0;

                tableWidth = $('.table-scroll table').css('width').replace('px','');
                tableThNum = $('.table-scroll tr:eq(0)').children('th').length;            if ($('.table-scroll tr').length == 1) { // header only                if (tableWidth > tableTr0Width) {
                    $('.table-scroll tr:eq(0)').children('th').each(function(i){
                        $(this).width(parseInt(($(this).css('width').replace('px','')) 
                        + parseInt(Math.floor((tableWidth - tableTr0Width) / tableThNum))) + 'px');
                    });
                }
            } else { // header and body
                tableTr1Width = $('.table-scroll tr:eq(1)').css('width').replace('px','');
                    $('.table-scroll tr:eq(1)').children('td').each(function(i){
                    tableTdWidths[i]=$(this).css('width').replace('px','');
                });
                $('.table-scroll tr:eq(0)').children('th').each(function(i) {            if(parseInt($(this).css('width').replace('px', '')) >
                parseInt(tableTdWidths[i])) {
                tableTdWidths[i] = $(this).css('width').replace('px','');
                    }
                });                if (tableWidth > tableTr1Width) {
                    //set all th td width
                    $('.table-scroll tr').each(function(i){
                            $(this).children().each(function(j){
                                $(this).css('min-width',(parseInt(tableTdWidths[j]) 
                                + parseInt(Math.floor((tableWidth - tableTr1Width) / 
                                tableThNum))) + 'px');
                            });
                    });
                } else {
                    //method 1 : set all th td width
                    $('.table-scroll tr').each(function(i){
                            $(this).children().each(function(j){
                                $(this).css('min-width',tableTdWidths[j] + 'px');
                            });
                    });
                }
            }
            });

html

    
        

完成效果:1.固定表头 2.table横纵滚动条 3.table列宽自适应 4.table内容不换行

title1 title1 title1 title1 title1 title1 title1 title1 title1 title1 title1 title1 title1 title1 title1 title1
1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
2 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
3 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
4 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
5 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
6 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
7 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
8 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
9 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
10 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
11 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
12 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
13 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
14 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
15 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
16 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
17 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
18 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
19 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
20 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
21 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
22 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
23 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
24 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
25 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
26 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
27 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
28 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
28 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
29 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1
30 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1

网上也有很多的例子,但是实现的效果不是想要的,自己工作需要做了一下,画面不好看,重点看效果,如有问题请指正!

相关推荐:

HTML页面原生VIDEO标签隐藏下载按钮功能

实现在HTML页面加载完毕后运行js方法

相关文章

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

56

2026.01.21

三角洲入口地址合集
三角洲入口地址合集

本专题整合了三角洲入口地址合集,阅读专题下面的文章了解更多详细内容。

28

2026.01.21

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

389

2026.01.21

妖精漫画入口地址合集
妖精漫画入口地址合集

本专题整合了妖精漫画入口地址合集,阅读专题下面的文章了解更多详细内容。

116

2026.01.21

java版本选择建议
java版本选择建议

本专题整合了java版本相关合集,阅读专题下面的文章了解更多详细内容。

3

2026.01.21

Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

9

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

49

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
兄弟连高洛峰div+css视频教程
兄弟连高洛峰div+css视频教程

共21课时 | 4.7万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3万人学习

AngularJS教程
AngularJS教程

共24课时 | 2.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号