小程序文本展开与收起功能组件

一.wxml文件

<!-- 实现文本展开与收起功能组件 -->
<view class="loadmore">
  <view>
    <text class="title">{{title}}</text>
  </view>
  <view class="info">
    <text class="show_info {{isshow?'clamp':''}}">{{info}}</text>
    <text class="hide_info">{{info}}</text>
    <view style="padding: 0 0 5px 0;"></view>
    <view class="{{effect=='1'?'add_btn':'add_btn2'}}" wx:if="{{showBtn}}">
      <view wx:if="{{isshow}}" class="{{effect=='1'?'':'btn'}}" bindtap="showInfo">
        <text>展开 </text>
        <van-icon name="arrow-down" />
      </view>
      <view wx:if="{{!isshow}}" class="{{effect=='1'?'':'btn'}}" bindtap="hideInfo">
        <text>收起 </text> 
        <van-icon name="arrow-up" />
      </view>
    </view>
    <!-- borders -->
    <view style="border-bottom: 1px dashed lightgray;padding: 1px 0;"></view>
  </view>
</view>

二.css

/* components/loadmore/loadmore.wxss */
.loadmore {
 padding: 8px 25px 0px 25px;
}

.title {
 font-size: 16px;
 padding-left: 3px;
}

.info {
 position: relative;
 margin-top: 5px;
 font-size: 14px;
 color: #434344;
}

.show_info {
 width: 100%;
 line-height: 1.4rem;
}

.clamp {
 overflow: hidden;
 display: -webkit-box;
 text-overflow: ellipsis;
 -webkit-line-clamp: 3;
 -webkit-box-orient: vertical;
 word-break: break-all;
}

.hide_info {
 display: -webkit-box;
 position: absolute;
 width: 100%;
 left: -100vw;
 overflow: hidden;
 line-height: 1.4rem;
}

.add_btn {
 color: #4EB8ED;
 display: flex;
 justify-content: flex-end;
 padding-bottom: 5px;
}

.add_btn2 {
 color: #4EB8ED;
 display: flex;
 justify-content: flex-end;
 padding-bottom: 5px;
 margin-top: -25px;
}

.btn {
 width: 60px;
 text-align: center;
 background: linear-gradient(to right,transparent,rgba(255,255,255,0.9),#fff,#fff,#fff,#fff,#fff,#fff,#fff,#fff,#fff,#fff,#fff,#fff);
}

三.js文件

// components/loadmore/loadmore.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: ""
    },
    info:{
      type:String,
      value:"",
    },
    effect:{
      type:String,
      value:"1"
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
      isshow:true,
      showBtn:false,  //是否展示底部  展开和隐藏按钮
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.judgeBtn();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //判断按钮是否展示
    judgeBtn(){
      const query = wx.createSelectorQuery().in(this);
      query.selectAll(".show_info,.hide_info").boundingClientRect(res => {
        this.setData({
          showBtn: res[0].height < res[1].height
        })
      }).exec()
    },
    //展示全部信息
    showInfo(){
      this.setData({isshow:false})
    },
    //隐藏信息
    hideInfo(){
      this.setData({isshow:true})
    }
  }
})

四.引用

  <v-loadmore title="擅长方向" info="{{scfx}}" effect="2"></v-loadmore>

5.效果图(effect:样式类型)

image.png

image.png

image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容