根据屏幕高度调整显示位置,例如如果展示的高度距离屏幕底部还有很多位置则向下展开,如果展开后距离屏幕底部没有位置,则向上展开的功能(计算元素距离屏幕底部等高度)
computeBottom() {
// 可视区窗口高度
var documentHeight = document.documentElement.clientHeight;
//当前元素高度
var objHeight = this.$refs.searchInput.offsetHeight;
// 当前元素与页面顶部距离
var objOffSetHeight = this.$refs.searchInput.getBoundingClientRect().top;
// 计算距离底部的高
var useHeight = documentHeight - objHeight - objOffSetHeight;
// 展开栏的元素高度
var searchHeight = this.$refs.searchMain.offsetHeight;
if (useHeight > searchHeight) {
// 向下展示
this.tooLong = false;
} else {
// 向上展示
this.tooLong = true;
this.$refs.searchMain.style.marginTop = " (searchHeight + objHeight) + 'px'";
}
},
异步执行
setTimeout(() => {
this.computeBottom();
}, 10);
更多获取当前元素到页面底部的距离及其他获取距离问题参考 获取当前元素到页面底部的距离及其他获取距离问题_南极修炼千年的大冰块-CSDN博客_获取元素距离底部的距离