html 表格指定区域,js:table滚动到可视区域内时固定table头部在顶部

index.html部分如下:

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;

}

  • {{item.name}}

agenamesex
{{item.NAME}}{{item.ACTIVE_TYPE}}{{item.DATE}}
  • {{item.name}}

  • {{item.name}}

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"

}

}

}

})

混入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"

}

]

}

},

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值