盒子的大小和数据的高度自适应

需求:当数据内容不够,那么盒子的高度就是屏幕的剩余部分
如果数据超出屏幕就出现滚动条

步骤:
1-在请求数据列表的父盒子加上
:style="[{'min-height':wrapperHeight},{height:'inherit'}]"
项目代码:

<div class="item-box" ref="wrap" :style="[{'min-height':wrapperHeight},{height:'inherit'}]">
        <div>
          <p>我是渲染的动态列表...</p>
        </div>
   </div>
专门建立的学习Q-q-u-n: 731771211,分享学习方法和需要注意的小细节,不停更新最新的教程和学习技巧
(从零基础开始到前端项目实战教程,学习工具,全栈开发学习路线以及规划)

2-在mounted 加上监听数据列表顶部的距离

    this.wrapperHeight = document.documentElement.clientHeight - this.$refs.wrap.getBoundingClientRect().top + 'px';

3-在方法methods中加上方法

 getHeight () {
      let wrapperHeight =
        document.documentElement.clientHeight -
        this.$refs.wrap.getBoundingClientRect().top;
      let windowHeight = document.documentElement.offsetHeight;
      let warpHeight = this.$refs.wrap.offsetHeight;

      if (wrapperHeight >= warpHeight) {
        this.$refs.wrap.style.height = wrapperHeight + "px";
      } else {
        this.$refs.wrap.style.height = "inherit";
      }
    },

发布了263 篇原创文章 · 获赞 15 · 访问量 3908

猜你喜欢

转载自blog.csdn.net/weixin_45974707/article/details/103978196