【快应用】list案例分享

 

背景描述:

快应用中list组件中两个子组件,如何在一个list子组件高度固定的情形下,让另一个子组件可以铺满list组件剩余的高度?

 

解决方案:

可以通过getBoundingClientRect方法分别获取到list和第一个子组件的高度,然后相减得到剩余的高度,再设置给第二个子组件,这样即可将list组件剩余的高度都铺满。

 

示例代码:

<template>

  <!-- Only one root node is allowed in template. -->

  <div class="container">

    <list id="main" class="list">

      <list-item id="item" class="listitem" type="text">

        <text class="title">first listitem content</text>

      </list-item>

      <list-item type="list-item" style="height: {{seconditemheight}}px;background-color:green">

        <text class="title">second listitem content</text>

      </list-item>

    </list>

    <text class="title" onclick="custom">set height</text>

  </div>

</template>

 

<style>

  .container {

    flex-direction: column;

    justify-content: center;

    align-items: center;

  }

  .list {

    height: 80%;

    border: 1px solid #000000;

  }

  .title {

    font-size: 50px;

  }

  .listitem {

    height: 200px;

    background-color: #00ced1;

  }

</style>

 

<script>

  module.exports = {

    data: {

      componentData: {},

      seconditemheight: 0,

      listheight: 0,

      firstitemheight: 0,

    },

    onInit() {

      this.$page.setTitleBar({

        text: 'menu',

        textColor: '#ffffff',

        backgroundColor: '#007DFF',

        backgroundOpacity: 0.5,

        menu: true

      });

    },

    custom() {

      var that = this

      that.$element('main').getBoundingClientRect({

        success: function (data) {

          that.listheight = data.height;

        },

      });

      that.$element('item').getBoundingClientRect({

        success: function (data) {

          that.firstitemheight = data.height;

        },

      });

      setTimeout(() => {

        that.seconditemheight = that.listheight - that.firstitemheight

        console.log("this.seconditemheight ", that.seconditemheight)

      }, 50);

 

    }

  }

</script> 

​欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

posted @   华为开发者论坛  阅读(47)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示