gpt4 book ai didi

javascript - 在vue js中设置div的高度

转载 作者:搜寻专家 更新时间:2023-10-30 22:54:59 24 4
gpt4 key购买 nike

我正在使用 vue js 。我想使用纯 javascript 根据另一个 div 高度设置一个 div。我面临的问题是我无法使用纯 Java 脚本设置高度。但我可以使用 jquery 来设置它。任何人都可以帮我把这个 jquery 改成 javascript 吗?我正在使用的代码已给出

 Vue.nextTick(function () {
var offsetHeight = document.getElementById('filterSection').offsetHeight;
$(".searchResultSection").css("max-height",`calc(100% - ${offsetHeight}px)`);
});

我需要将 jquery 部分更改为 java 脚本。

最佳答案

事实上,计算属性 ( https://v2.vuejs.org/v2/guide/computed.html#Computed-Properties ) 是解决您问题的完美选择:

声明一个计算属性,它会返回 filterSectionHeight

export default {
name: "App",

computed: {
filterSectionHeight() {
const filterSectionDOM = document.getElementById("filterSection");
return filterSectionDOM ? filterSectionDOM.offsetHeight : 0;
},
}
};

在您的组件(或 App 组件)中定义您的 div filterSectionsearchResultsSection,不要忘记添加一个 :style 属性 处理模板中提供给 .searchResultsSection 的动态 max-height

<div id="filterSection"></div>
<div class="searchResultsSection"
:style="{
'max-height': `calc(100% - ${filterSectionHeight}px)`
}">
</div>

在您的 css 中将每个 div 的高度设置为 100%

#app {
height: 600px;
width: 100%;
}

#filterSection {
height: 100%;
background: rebeccapurple; //https://en.wikipedia.org/wiki/Eric_A._Meyer
}

.searchResultsSection{
height: 100%;
background: rebeccapurple;
opacity: 0.6;
}

您将在此处找到完整的演示 > https://codesandbox.io/s/1rkmwo1wq

关于javascript - 在vue js中设置div的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51692031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com