• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
吃素的黑猫
帮助1

将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部分

  1.  
     
  2.  
     
  3.  
    <view class="comment" style="bottom:{{bottomHeight}}px;">
  4.  
    <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>
  5.  
    <button type="primary" class="send_out" style="width: 100rpx;" disabled="{{content?false:true}}" bindtap="sendOut" >发送</button>
  6.  
    </view>

 wxss部分

  1.  
    .comment {
  2.  
    margin: 0;
  3.  
    padding: 16rpx 24rpx;
  4.  
    width: 100%;
  5.  
    /* height: 92rpx; */
  6.  
    display: flex;
  7.  
    justify-content: space-between;
  8.  
    align-items: center;
  9.  
    border-top: 1rpx solid #cccccc;
  10.  
    border-bottom: 1rpx solid #cccccc;
  11.  
    /* padding和border被包含在定义的width和height之内。盒子的实际宽度=设置的width(padding和border不会影响实际宽度) */
  12.  
    box-sizing: border-box;
  13.  
    font-family: PingFangSC-Regular, PingFang SC;
  14.  
    font-size: 32rpx;
  15.  
    transition: all 2s inherit;
  16.  
    overflow: hidden;
  17.  
    /* 设置为固定定位 */
  18.  
    position: fixed;
  19.  
    left: 0;
  20.  
    }
  21.  
     
  22.  
    /* textarea输入框的样式 */
  23.  
    .textarea {
  24.  
    margin: 0;
  25.  
    padding: 11rpx 24rpx;
  26.  
    /* 宽度为 父容器的宽度 减去 发送按钮的宽度 减去 (左右内边距和左右边框宽度) 减去 右边外边距*/
  27.  
    width: calc(100% - 100rpx - 50rpx - 24rpx);
  28.  
    /* textarea 的高度随着文本的内容来改变的 设置一个最小高度60rpx*/
  29.  
    min-height: 40.8rpx;
  30.  
    /* 设置行高 */
  31.  
    line-height: 40.8rpx;
  32.  
    /* 取消默样式 */
  33.  
    outline: none;
  34.  
    border: 1rpx solid #cccccc;
  35.  
    border-radius: 15rpx;
  36.  
    background-color: #FFFFFF;
  37.  
    /* padding和border不被包含在定义的width和height之内。盒子的实际宽度=设置的width padding border */
  38.  
    box-sizing: content-box;
  39.  
    overflow: hidden;
  40.  
    }
  41.  
     
  42.  
    /* 发送按钮样式 */
  43.  
    .send_out {
  44.  
    margin: 0;
  45.  
    padding: 0;
  46.  
    width: 100rpx;
  47.  
    height: 60rpx;
  48.  
    text-align: center;
  49.  
    line-height: 60rpx;
  50.  
    border: 1rpx solid #cccccc;
  51.  
    border-radius: 10rpx;
  52.  
    /* 将发送按钮固定在底部 */
  53.  
    position: absolute;
  54.  
    right: 24rpx;
  55.  
    bottom: 16rpx;
  56.  
    }
学新通

js部分

  1.  
    const app = getApp()
  2.  
     
  3.  
    Page({
  4.  
    data: {
  5.  
    content:'',//文本类容
  6.  
    bottomHeight:0 //定义comment容器与page容器下边界之间的距离
  7.  
    },
  8.  
    // 获取焦点 唤起软键盘
  9.  
    bindfocus(e){
  10.  
    console.log(e, '键盘弹起')
  11.  
    console.log(e)
  12.  
    this.setData({
  13.  
    bottomHeight:e.detail.height //将键盘的高度设置为comment容器与page容器下边界之间的距离。
  14.  
    })
  15.  
     
  16.  
    },
  17.  
    // 输入内容
  18.  
    bindinput(e){
  19.  
    this.setData({
  20.  
    content:e.detail.value
  21.  
    })
  22.  
    },
  23.  
    // 失去焦点
  24.  
    bindblur(e){
  25.  
    console.log(e, '收起键盘')
  26.  
    this.setData({
  27.  
    bottomHeight:0
  28.  
    })
  29.  
    },
  30.  
    //
  31.  
    sendOut(){
  32.  
    let {content}=this.data //使用解构
  33.  
    //调用发送接口
  34.  
     
  35.  
    }
  36.  
    })
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgehjgk
系列文章
更多 icon
同类精品
更多 icon
继续加载