美文网首页
js:table滚动到可视区域内时固定table头部在顶部

js:table滚动到可视区域内时固定table头部在顶部

作者: 冰雪_666 | 来源:发表于2020-02-19 15:35 被阅读0次
    1. index.html部分如下:
    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover">
            <title></title>
            <style>
                html,body {
                    padding: 0;
                    margin: 0;
                }
                
                .table-head-fixed {
                    position: fixed;
                    top: 0;
                    left: 0;
                    width: 100%;
                }
                
                table {
                    width: 100%;
                }
                
                th {
                    background-color: #ccc;
                }
                
                td,th {
                    height: 2rem;
                    width: 33.3%;
                    padding: 0.5rem;
                    box-sizing: border-box;
                    border-bottom: 1px solid #ccc;
                    border-right: 1px solid #ccc;
                }
                
                ul,li {
                    margin: 0;
                    padding: 0;
                }
                
                ul li {
                    list-style: none;
                    border-bottom: 1px solid #ccc;
                    padding: 1rem;
                }
            </style>
            <script type="text/javascript" src="../echart-wx/js/vue.min.js"></script>
        </head>
    
        <body>
            <div id="app">
                <ul>
                    <li v-for="(item,index) in ulData" :key="index">
                        {{item.name}}
                    </li>
                </ul>
                <div class="table">
                    <div class="table-head" ref="tableHead">
                        <table cellpadding="0" cellspacing="0">
                            <tr>
                                <th>age</th>
                                <th>name</th>
                                <th>sex</th>
                            </tr>
                        </table>
                    </div>
                    <div class="table-body" ref="tableBody">
                        <table cellpadding="0" cellspacing="0">
                            <tr v-for="(item,index) in tableData" :key="index">
                                <td>{{item.NAME}}</td>
                                <td>{{item.ACTIVE_TYPE}}</td>
                                <td>{{item.DATE}}</td>
                            </tr>
                        </table>
                    </div>
                </div>
                <ul>
                    <li v-for="(item,index) in ulData" :key="index">
                        {{item.name}}
                    </li>
                </ul>
                <ul>
                    <li v-for="(item,index) in ulData" :key="index">
                        {{item.name}}
                    </li>
                </ul>
            </div>
        </body>
        <script type="text/javascript" src="js/table.js"></script>
        <script>
            var demo = new Vue({
                mixins: [table],
                el: "#app",
                data: function() {
                    return {}
                },
                watch: {},
                mounted: function() {
                    this.scroll()
                },
                created: function() {},
                methods: {
                    scroll: function() {
                        var that = this
                        window.addEventListener('scroll', that.throttle(that, that.fixedTable, 200))
    
                    },
                    //函数节流,在规定时间内才执行一次图片加载函数,解决耗费性能,浏览器卡顿问题。
                    throttle: function(that, fixedTable, wait) {
                        var lastTime = new Date().getTime()
                        return function() {
                            var curTime = new Date().getTime()
                            if(curTime - lastTime < wait) return
                            lastTime = curTime
                            fixedTable(that)
                        }
                    },
                    //固定table头部
                    fixedTable: function(that) {
                        var eleOffsetTop = that.$refs.tableBody.offsetTop - that.$refs.tableHead.clientHeight
                        var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                        var tableBodyH = that.$refs.tableBody.clientHeight
                        if(eleOffsetTop <= scrollTop && eleOffsetTop + tableBodyH > scrollTop) {
                            that.$refs.tableHead.className = "table-head table-head-fixed"
                        } else {
                            that.$refs.tableHead.className = "table-head"
                        }
                    }
                }
            })
        </script>
    
    </html>
    
    1. 混入js(table.js)部分如下:
    var table = {
        data: function() {
            return {
                ulData: [{
                    name: 1,
                }, {
                    name: 2,
                }, {
                    name: 3,
                }, {
                    name: 4,
                }, {
                    name: 5,
                }, {
                    name: 6,
                }, {
                    name: 4,
                }, {
                    name: 5,
                }, {
                    name: 6,
                }, {
                    name: 4,
                }, {
                    name: 5,
                }, {
                    name: 6,
                }],
                tableData: [{
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    },
                    {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    },
                    {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    },
                    {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    },
                    {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }, {
                        "NAME": "电影",
                        "TYPE": "兴趣",
                        "ACTIVE_TYPE": "社交",
                        "DATE": "2020-02-04",
                        "BEGIN_DATE": "09:30",
                        "END_DATE": "11:45",
                        "HOURS": "2小时15分钟",
                        "PERSON": 45,
                        "JOIN_PERSON": 30,
                        "SPONSOR": "独立区",
                        "LABEL": "",
                        "USE_PLACE": "2号楼多工能厅",
                        "SCOPE": "课程",
                        "SON_COURSE": "欣赏课",
                        "CLUB_NAME": "娱乐",
                        "TEACHER_NAME": "王xx",
                        "TEACHER_TYPE": "员工",
                        "DATA_UPDATE": "2020-02-04 13:50"
                    }
                ]
            }
    
        },
    }
    

    相关文章

      网友评论

          本文标题:js:table滚动到可视区域内时固定table头部在顶部

          本文链接:https://www.haomeiwen.com/subject/vguafhtx.html