小程序小tips

文章介绍了在小程序中使用let替代var以避免变量提升问题,并展示了如何设置文本指定长度并隐藏超出部分的CSS方法。此外,还演示了行内内容指定宽度后隐藏的实现,以及使用flex布局和对话聊天布局的示例。同时,提到了图片按比例缩放的CSS处理方式。
摘要由CSDN通过智能技术生成

零碎知识

var命令存在变量提升效用,let命令没有这个问题。let优于var,完全可以替换。

设置文本指定长度

超出部分隐藏

在当前网页前面

<wxs module="intercept">
  var substr = function (val, len) {
    if (val.length == 0 || val == undefined) {
      return false;
    } else if (val.length > len) {
      return val.substring(0, len) + "...";
    } else {
      return val;
    }
  }
  module.exports.substr = substr
</wxs>

在网页中

<view class="invitation">
  <view class="invitation_1">
    <view class="invitation_1_1" wx:for="{{json.invitation.length / 2}}" wx:key="index" data-index="{{index}}">
      <view>
        <image style="border-radius: 50%;" src="{{json.invitation[index*2] == '' ? image_url : json.invitation[index*2]}}" mode=""/>
      </view>
      <view>
        <text style="font-size: small;">{{intercept.substr(json.invitation[index*2+1], 3)}}</text>
      </view>
    </view>
  </view>
</view>

效果图
在这里插入图片描述

一行内容指定宽度后面隐藏

CSS

.text_width {
  position: relative;
  width: 300px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

网页中

<view class="qa">
  <view class="qa_1">
    <view style="font-size: larger;width: 100%;height: 20px;">
      <text style="position: relative;float: left;width: 50%;">最新问答</text>
      <text style="position: relative;float: right;width: 50%;text-align: right;font-size: smaller;" bindtap="flash_qa">刷新</text>
    </view>
    <view class="qa_2 border" wx:for="{{json.qa}}" wx:if="{{index == random}}" wx:key="index" data-index="{{index}}" bindtap="aq">
      <view>
        <view class="qa_q">
          <text></text>
        </view>
        <view class="text_width qa_a">
          <text>{{item[1]}}</text>
        </view>
      </view>
      <view>
        <view class="qa_q">
          <text></text>
        </view>
        <view class="text_width qa_a">
          {{item[2]}}
        </view>
        <view>
          <text style="position: relative;font-size: smaller;color: grey;">>>>全部{{item.length-1}}条回答</text>
        </view>
      </view>
    </view>
  </view>
</view>

效果图
在这里插入图片描述

flex比例布局

css

.recommend {
  position: relative;
  margin: 2%;
  margin-top: 5%;
}
.recommend_1 {
  position: relative;
  margin: 2%;
  width: 96%;
  padding-bottom: 5px;
}
.recommend_1 view view {
  position: relative;
  padding: 2px 0;
}

网页中

<view class="recommend">
  <view class="recommend_1 border" wx:for="{{json_2}}" wx:key="index" bindtap="to_select_num" data-index="{{index}}">
    <view style="position: relative;display: flex;">
      <view style="position: relative;width: 70px;float: left;padding-top: 10px;flex: 2;">
        <image style="position: relative;width: 50px;height: 50px;left: 10px;" src="{{image_url}}" mode="" />
      </view>
      <view style="position: relative;float: left;flex: 8;">
        <view>{{item.other[1]}} {{item.other[2]}}</view>
        <view>{{item.other[3]}}</view>
        <view class="text_width" style="width: 100%;"><text wx:for="{{item.content}}" wx:key="index">{{item}} </text></view>
        <view>{{item.other[4]}}</view>
        <view style="position: relative;width: 100%;">
          <view style="position: relative;width: 25%;margin: 0 8%;float: left;text-align: center;background-color: rgb(102, 243, 125);border-radius: 40px;" bindtap="to_q">去提问</view>
          <view style="position: relative;width: 25%;margin: 0 8%;float: left;text-align: center;background-color: rgb(109, 184, 255);border-radius: 40px;" bindtap="to_c">合作</view>
        </view>
      </view>
    </view>
  </view>
</view>

效果图
在这里插入图片描述

对话聊天布局

css

image {
  width: 40px;
  height: 40px;
}
.border {
  border-radius: 20px;
  border-color: rgb(198, 228, 255);
  border-width: 1px;
  border-style: solid;
}
.details {
  position: relative;
  margin: 2%;
}
.left_chat {
  position: relative;
  float: left;
  background-color: rgb(101, 187, 245);
}
.right_chat {
  position: relative;
  float: right;
  background-color: rgb(109, 255, 72);
}
/* .chat {
  position: relative;
} */
.text_center {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}
.text_row {
  position: relative;
  max-width: 250px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  height: auto;
}

网页

<view class="details">
  <view class="border" style="height: 50px;border-radius: 0;border-width: 0 0 1px 0;">
    <view style="position: relative;float: left;width: 15%;">
      <image src="{{image_url}}" mode="" />
    </view>
    <view style="position: relative;float: left;">
      <text style="line-height: 50px;">不愿透露真名的帅哥</text>
    </view>
  </view>
  <view style="position: relative;width: 100%;top: 20px;display: flex;flex-direction: column;" wx:for="{{json[num].length}}" wx:if="{{index != 0}}" wx:key="index" data-index="{{index}}">
    <view style="position: relative;margin: 5px 0;">
      <view style="position: relative;width: 10%; {{index % 2 == 1 ? 'float: left;' : 'float: right;'}}">
        <image class="border" style="border-radius: 50%;width: 40px;height: 40px;" src="{{image_url}}" mode="" />
      </view>
      <view style="position: relative;width: 90%;">
        <view class="chat {{index % 2 == 1 ? 'left_chat' : 'right_chat'}}" style="position: relative;padding: 10px 10px; {{index % 2 == 1 ? 'float: left;left: 10px;' : 'float: right;right: 10px;'}}" class="border text_center text_row">
          {{json[num][index]}}
        </view>
      </view>
    </view>
  </view>
</view>

效果图
在这里插入图片描述

图片比例放缩

css

img{
	width: 50px;
	height: 50px;
	object-fit: contain;
}

效果图
在这里插入图片描述

图片放缩

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值