在页面加载的vue js中根据父高度设置元素高度

时间:2018-04-22 09:06:07

标签: vue.js vuejs2 vue-component

我想设置一个vue组件的高度(具体就是这个 - > https://github.com/jbaysolutions/vue-grid-layout)。

其高度(在vue-grid-layout - > grid-item中)应与其父级相同。

这也应仅限于页面加载时间。那么如何在vue js中实现这一目标呢?

我不想使用CSS来做这件事。我需要像素高度。关于vue-grid-layout - >物品高度最初需要像素。因为它可以调整大小,所以可以在之后进行更改。

3 个答案:

答案 0 :(得分:0)

无需使用高级JavaScript来计算高度,只需使用样式:

height: 100%;

演示:

.parent {
    resize: both;
    overflow: auto;
    height: 100px;
    display: block;
    width: 100px;
    border: 1px solid black;
}

.child {
    height: 100%;
    background: pink;
}
<div class="parent">
</div>

<div class="parent">
    <div class="child">
         I'm a child!
    </div>
</div>

答案 1 :(得分:0)

通过一些示例html(您的实际代码)提供准确的答案会更容易,但以下内容应该可以正常使用。

export default {
    ...
    data() {
        return {
            parentHeight: 0
        }
    },
    mounted() {
        this.parentHeight = this.$parent.$el.offsetHeight;
    }
    ...
}

因此,在已安装的生命周期钩子中,您可以读取父级的高度,然后将其设置在您需要的位置。

答案 2 :(得分:0)

我找到了一种根据可用空间来修复网格布局高度的解决方案。为此,我使用了以下道具:max-rows、row-height、margins、autoSize=false 和 ResizeObserver 将根据窗口调整大小后的可用高度调整网格布局行高。另请注意,我使用了一些 Bootstrap 类来进行样式设置


<template>
  <div class="flex-grow-1 w-100">
    <grid-layout
      :layout="layout"
      :col-num="colCount"
      :maxRows="rowCount"
      :row-height="rowHeight"
      :autoSize="false"
      :is-draggable="true"
      :is-resizable="true"
      :is-mirrored="false"
      :preventCollision="true"
      :vertical-compact="false"
      :margin="margin"
      :use-css-transforms="true"
    >
      <grid-item
        v-for="item in layout"
        class="bg-primary"
        :x="item.x"
        :y="item.y"
        :w="item.w"
        :h="item.h"
        :i="item.i"
        :key="item.i"
      >
        <span class="remove" @click="removeItem(item.i)">x</span>
      </grid-item>
    </grid-layout>
  </div>
</template>

<script lang="ts">
import { defineComponent } from '@vue/runtime-core';

interface GridItem {
  x: number;
  y: number;
  w: number;
  h: number;
  i: string;
}

interface State {
  layout: GridItem[];
  index: number;
  colCount: number;
  rowCount: number;
  rowHeight: number;
  observer: null | ResizeObserver;
  margin: number[];
}

export default defineComponent({
  name: 'VideoWall',
  data(): State {
    return {
      layout: [
        { x: 0, y: 0, w: 2, h: 2, i: '0' },
        { x: 2, y: 0, w: 2, h: 4, i: '1' },
        { x: 4, y: 0, w: 2, h: 5, i: '2' },
        { x: 6, y: 0, w: 2, h: 3, i: '3' },
        { x: 8, y: 0, w: 2, h: 3, i: '4' },
      ],
      index: 0,
      colCount: 36,
      rowCount: 36,
      rowHeight: 40,
      margin: [5, 5],
      observer: null,
    };
  },
  mounted() {
    this.observer = new ResizeObserver(this.onResize);

    if (this.$el instanceof Element) {
      this.observer.observe(this.$el);
    } else {
      console.log('VideoWall: Failed to bind observer');
    }
  },
  unmounted() {
    if (this.observer) {
      this.observer.disconnect();
    }
  },
  methods: {
    onResize(entries: ResizeObserverEntry[]): void {
      this.rowHeight =
        Math.floor(entries[0].contentRect.height / this.rowCount) -
        this.margin[1];
    },
  },
});
</script>

相关问题