data(){
return {
topHeight: 300,
clientHeight: 0
}
}
mounted(){
this.clientHeight=`${document.documentElement.clientHeight}`
let that=this
window.onresize=function(){
that.clientHeight=`${document.documentElement.clientHeight}`
if(that.clientHeight>=1080){
that.topHeight=400
}else{
that.topHeight=300
}
}
}
<div class="m-top" :style="{'height' : topHeight+'px'}"></div>
相关语法:1.`${}` 模板字符串 2.window.onresize当浏览器被重置大小时执行Javascript代码
3. :style使用方法:https://www.cnblogs.com/weichenzhiyi/p/8406021.html
tip:其他动态样式问题也可以考虑通过此方案解决

本文介绍了一个基于Vue的组件,通过监听窗口尺寸变化,动态调整顶部导航的高度。当屏幕高度大于或等于1080px时,将顶部高度设为400px,否则保持300px。使用了模板字符串和window.onresize事件来实现动态样式更新。
1211

被折叠的 条评论
为什么被折叠?



