美文网首页
微信小程序高度动态变化实现手风琴效果

微信小程序高度动态变化实现手风琴效果

作者: 踏雪_739a | 来源:发表于2022-11-03 15:42 被阅读0次

wxml

 <view bindtap="tapClick" >显示/隐藏</view>
<view class="collapse-wrap" style="height: {{contentHeight}};">
  <view class="collapse-item-content">
    <block wx:for="{{10}}">
      <view>wwww</view>
    </block>
  </view>
</view>

wxss

.collapse-wrap{
  background-color: paleturquoise;
  height: 0;
  overflow: hidden;
  transition: height .3s ease-in-out;
}

js


Page({
  data: {
    contentHeight: 0,
  },
  tapClick(){
    this.updateStyle(this.data.contentHeight)
  },
  updateStyle(expanded) {
    this.getRect('.collapse-item-content')
        .then((rect) => rect.height)
        .then((height) => {
          if (expanded) {
            this.setData({ contentHeight: 0 });
          }else{
            this.setData({ contentHeight: `${height}px` })
          }
      });
  },
  getRect(selector, all) {
    return new Promise(resolve => {
      wx.createSelectorQuery()
          .in(this)[all ? 'selectAll' : 'select'](selector)
          .boundingClientRect(rect => {
          if (all && Array.isArray(rect) && rect.length) {
              resolve(rect);
          }
          if (!all && rect) {
              resolve(rect);
          }
      })
      .exec();
  });
  }
})

好用给个赞哦~~

相关文章

网友评论

      本文标题:微信小程序高度动态变化实现手风琴效果

      本文链接:https://www.haomeiwen.com/subject/fsretdtx.html