js写法
function computeHeight(){
const element = document.querySelector('.my-table');
const rect = element.getBoundingClientRect();
const tablebody = document.querySelector('.el-table__body-wrapper')
const h = window.innerHeight - rect.top - 42 - 44 - 20;
tablebody.style.minHeight = h+'px';
tablebody.style.maxHeight = h+'px';
tablebody.style.overflow = "auto";
}
computeHeight();
window.onresize = function(){
computeHeight();
}
vue2指令写法
function changeTableHeight(el, binding) {
const rect = el.getBoundingClientRect();
let tablebody = '';
Array.from(el.children).forEach(item => {
if (item.className.includes('el-table__body-wrapper')) {
tablebody = item;
}
})
let h = window.innerHeight - rect.top - 42 - 44 - 20;
if (binding.value?.h) {
h = window.innerHeight - rect.top - binding.value.h;
}
tablebody.style.minHeight = h + 'px';
tablebody.style.maxHeight = h + 'px';
tablebody.style.overflow = "auto";
}
export default {
bind(el, binding, vnode) {
vnode.context.$nextTick(() => {
changeTableHeight(el, binding);
})
const resizeHandler = () => {
vnode.context.$forceUpdate();
};
window.addEventListener('resize', resizeHandler);
vnode.context.$on('hook:unbind', () => {
window.removeEventListener('resize', resizeHandler);
});
},
}