微信小程序开发——评论功能

将comment容器设置为固定定位,高度设置为自适应,会根据textarea容器高度的变化来变化。textarea容器中最小高度,文本的行高都设置一致,这样就不会出现滚动条。当输入框聚焦时触发,可以通过定义的bindfocuse方法获取到键盘高度(event.detail.height )。将其值设置为comment容器的bottom值。

1. auto-height="true":将textarea设置为自动增高,但是style.height将不生效。

2. auto-foucs="true":自动聚焦,拉起键盘。

3. cursor-spacing='0':指定光标与键盘的距离。取`textarea`距离底部的距离和`cursor-spacing`指定的距离的最小值作为光标与键盘的距离。

4. adjust-position="{{false}}"  键盘弹起时,取消自动上推页面

5.  show-comfirm-bar="{{fasle}}": 取消键盘上方带有”完成“按钮那一栏,若不取消 唤起键盘后,在安卓中键盘会遮挡住 comment容器 ,而iOS则不会。

6. bindfocus:输入框聚焦时触发,event.detail.height 为键盘高度。

7.bindblur:输入框失去焦点时触发

8.bindlinechange:输入框行数变化时调用,event.detail.lineHeight: 可以获取一行文字的高度为 20.4px。

          

wxml部分



<view class="comment" style="bottom:{{bottomHeight}}px;">
  <textarea class="textarea" show-confirm-bar="{{false}}" auto-height="true" auto-focus="true" cursor-spacing='0' adjust-position="{{false}}"  	 placeholder="评论" maxlength="1000" value="{{content}}" bindfocus="bindfocus" bindinput="bindinput" bindblur="bindblur"></textarea>
  <button type="primary" class="send_out" style="width: 100rpx;" disabled="{{content?false:true}}" bindtap="sendOut" >发送</button>
</view>

 wxss部分

.comment {
  margin: 0;
  padding: 16rpx 24rpx;
  width: 100%;
  /* height: 92rpx; */
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1rpx solid #cccccc;
  border-bottom: 1rpx solid #cccccc;
  /* padding和border被包含在定义的width和height之内。盒子的实际宽度=设置的width(padding和border不会影响实际宽度) */
  box-sizing: border-box;
  font-family: PingFangSC-Regular, PingFang SC;
  font-size: 32rpx;
  transition: all 2s inherit;
  overflow: hidden;
  /* 设置为固定定位 */
  position: fixed;
  left: 0;
}

/* textarea输入框的样式 */
.textarea {
  margin: 0;
  padding: 11rpx 24rpx;
  /* 宽度为 父容器的宽度 减去 发送按钮的宽度 减去 (左右内边距和左右边框宽度) 减去 右边外边距*/
  width: calc(100% - 100rpx - 50rpx - 24rpx);
  /* textarea 的高度随着文本的内容来改变的 设置一个最小高度60rpx*/
  min-height: 40.8rpx;
  /* 设置行高 */
  line-height: 40.8rpx;
  /* 取消默样式 */
  outline: none;
  border: 1rpx solid #cccccc;
  border-radius: 15rpx;
  background-color: #FFFFFF;
  /* padding和border不被包含在定义的width和height之内。盒子的实际宽度=设置的width+padding+border */
  box-sizing: content-box;
  overflow: hidden;
}

/* 发送按钮样式 */
.send_out {
  margin: 0;
  padding: 0;
  width: 100rpx;
  height: 60rpx;
  text-align: center;
  line-height: 60rpx;
  border: 1rpx solid #cccccc;
  border-radius: 10rpx;
  /* 将发送按钮固定在底部 */
  position: absolute;
  right: 24rpx;
  bottom: 16rpx;
}

js部分

const app = getApp()

Page({
  data: {
    content:'',//文本类容
    bottomHeight:0 //定义comment容器与page容器下边界之间的距离
  },
  // 获取焦点 唤起软键盘
  bindfocus(e){
  console.log(e, '键盘弹起')
  console.log(e)
  this.setData({
    bottomHeight:e.detail.height //将键盘的高度设置为comment容器与page容器下边界之间的距离。
  })

  },
  // 输入内容
  bindinput(e){
    this.setData({
      content:e.detail.value
    })
  },
  // 失去焦点 
  bindblur(e){
    console.log(e, '收起键盘')
    this.setData({
      bottomHeight:0
    })
  },
  //
  sendOut(){
    let {content}=this.data //使用解构 
    //调用发送接口
    
  }
})

  • 5
    点赞
  • 91
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值