当前位置: 首页 > Web前端 > JavaScript

动态设置元素表高度

时间:2023-03-27 00:44:09 JavaScript

1新建jssetHeight.js//用于计算页面element表格高度,动态设置表格高度exportfunctionsetHeight(){constconfigHeight=window.innerHeight-this.$refs.tableRef.$el.offsetTop-50constbody=document.getElementsByClassName('el-table__body')[0].offsetHeight常量头=document.getElementsByClassName('el-table__header')[0].offsetHeightconsttotal=body+head+2console.log('total',total)如果(configHeight<总计){this.tableHeight=configHeight>300?configHeight:300}else{this.tableHeight=total>300?total:300}//constcard=document.getElementsByClassName('base-table-card')//card[0].style.height=configHeight+50+'px'//监听窗口大小化constself=thiswindow.onresize=function(){constconfigHeight=window.innerHeight-self.$refs.tableRef.$el.offsetTop-50constbody=document.getElementsByClassName('el-table__body')[0].offsetHeightconsthead=文档。getElementsByClassName('el-table__header')[0].offsetHeightconsttotal=body+head+2console.log('total1',total)if(configHeight300?configHeight:300}else{self.tableHeight=total>300?total:300}//constcard=document.getElementsByClassName('base-table-card')//card[0].style.height=configHeight+50+'px'}}2页使用流程<1><2><3>**!!!注意:[1]记得在页面导入上面的js[2]一定要在页面加载数据后调用钩子函数,否则第一次加载无法抓取到表格数据的高度,会有显示问题**