代码之家  ›  专栏  ›  技术社区  ›  Sebastien D

vuejs:根据窗口中的剩余空间自动调整div

  •  0
  • Sebastien D  · 技术社区  · 6 年前

    在vuejs(2.5.16)中,我试图设置div的style属性,以便根据窗口大小自动调整窗口的剩余空间:

    enter image description here

    我打算使用一个计算值,该值将为我提供正确的高度,并将其绑定到所需div的style属性中:

    Vue.component('menus', {
        template: '<div id="menu-div">MY MENU</div>'
    });
    
    Vue.component('expandable', {
        template: '<div id="expandable-div" :style="{height:expandableHeight}">EXPANDABLE<div>{{expandableHeight}}</div></div>',
        computed: {
            expandableHeight() {
    
                return ($(window).height() - $("#expandable-div").position().top) + 'px'
    
            }
        }
    });
    
    var vm = new Vue({
        el: '#app'
    });
    

    fiddle

    使用 $(window).height() $("#expandable-div").position().top 从jquery中,我认为我可以实现一个结果,因为它在我的控制台中工作。

    不幸的是,我有一个错误:

    类型错误:无法读取未定义的属性“top”

    1 回复  |  直到 6 年前
        1
  •  1
  •   Vucko    6 年前

    为什么不使用css flexbox 为了达到这个目的?

    Vue.component('menus', {
      template: '<div id="menu-div">MY MENU</div>'
    });
    
    Vue.component('expandable', {
      template: '<div id="expandable-div">EXPANDABLE<div>foo</div></div>',
      computed: {}
    });
    
    var vm = new Vue({
      el: '#app'
    });
    body {
      margin: 0;
    }
    
    #app {
      display: flex;
      flex-direction: column;
      height: 100vh;
    }
    
    #menu-div {
      height: 50px;
      background: #768ffb;
      margin-bottom: 5px;
    }
    
    #expandable-div {
      background: #76fbc1;
      flex-grow: 1;
    }
    <script src="https://unpkg.com/vue"></script>
    
    <div id="app">
      <menus></menus>
    
      <expandable></expandable>
    </div>