微信小程序实现容器图片流式布局功能,配合小程序原生框架使用。

news2025/2/24 2:57:59

小程序实现容器图片流式布局功能,因为目前论坛上也有很多博主出过类似的文章,这里我就以一个小白角度去讲一下如何实现的吧。给作者一点点鼓励,先点个赞赞吧👍,蟹蟹!!

目标

 实现下方效果图

技术栈 

微信小程序原生框架,wxml+wxss+js,因为后端是云开发的,所以网络请求是官方的api,比如wx.cloud.database().collection("community") .get()

这样做的好处

能根据图片的高度自适应容器高度,整体美观,易读

实现逻辑

1.页面布局wxml,一个大容器(宽度撑满屏幕高度)中,包含两列容器,左边一列,右边一列。如图所示,有图清晰一点。

2.首先获取一个数组dataList,用于渲染到页面上的数据,这里就用闲置帖子为例。

3.准备两个变量,一个是整型leftheight,作为左边容器实时高度,另一个是整型rightheight,作为右边容器实时高度。

4.获取数组后将回调值res中的data赋值给list局部变量,并循环这个list数组,同时判断左边的容器高度和右边的容器哪个更低,将子元素(子元素为对象)设置一个index属性标记属于左边容器,还是右边容器,这里的例子是index == 0 时候是左边容器,index==1时候是右边容器。每一次循环渲染子元素的时候判断左右容器高低,左边容器低则index设置为0标记到左边容器中去,否则亦反。

注意:picheight为数据库中数据已有的字段属性,为图片的高度

var that = this
var list = res.data //res.data为获取数据库中返回的数据数组 ,list为临时存储变量
var dataList = that.data.dataList //dataList为最终渲染到页面的数组数据
var leftheight = that.data.leftheight // 获取全局变量保存的左边容器高度
var rightheight = that.data.rightheight // 获取全局变量保存的右边容器高度

for (let i = 0; i < list.length; i++) {

//这里的picheight为存储到数据库时候的高度
  if (leftheight <= rightheight) {
      leftheight = leftheight + list[i].picheight + 120
      list[i].index = 0
  } else {
      rightheight = rightheight + list[i].picheight + 120
      list[i].index = 1
  }
          
   dataList.push(list[i])
}
    that.setData({
      dataList: dataList,
      leftheight: leftheight,
      rightheight: rightheight
  })

5.在wxml通过for循环渲染出来

部分关键代码:

 <view class="shop-big-box flex-row-center-x">
    <!-- 左列表 -->
    <view class='shop'>
      <block wx:for="{{dataList}}" wx:key="_id" wx:if="{{item.index == 0}}">
        <my-datalist item="{{item}}" index="{{index}}" data-src="{{item.pictures[0]}}" data-index="{{index}}" bind:getimage="getimage" />
      </block>
    </view>

    <!-- 右列表 -->
    <view class='shop'>
      <block wx:for="{{dataList}}" wx:key="_id" wx:if="{{item.index == 1}}">
        <my-datalist item="{{item}}" index="{{index}}" data-src="{{item.pictures[0]}}" data-index="{{index}}" bind:getimage="getimage" />
      </block>
    </view>
  </view>

my-datalist组件

<view class="shop-detail" bindtap="godetail" data-id="{{item._id}}">
  <view class='imagecont' style="height:{{item.picheight}}rpx;">
    <block wx:if="{{item.pictures.length > 0}}">
      <image src="{{item.pictures[0]}}" data-index="{{index}}" class="prodimg" style="height:{{item.picheight}}rpx;z-index: 3;"  mode="aspectFill" />
    </block>
    </view>
  </view>
  <view style="width: 100%;display: flex;flex-wrap: wrap;height: 120rpx;">
    <view class="shop-detail-text">{{item.text}}</view>
    <view class="shop-detail-user flex-row-center">
      <image src="{{item.user.imagavatares}}" style="border-radius: 50%;width: 30rpx;height: 30rpx;margin: 0 10rpx;background-color: rgb(247, 247, 247);" />
      <text>{{item.user.username}}</text>
    </view>
  </view>
</view>

完整代码

index.wxml 


  <view class="shop-big-box flex-row-center-x">
    <!-- 左列表 -->
    <view class='shop'>
      <block wx:for="{{dataList}}" wx:key="_id" wx:if="{{item.index == 0}}">
        <my-datalist item="{{item}}" index="{{index}}" data-src="{{item.pictures[0]}}" data-index="{{index}}" bind:getimage="getimage" />
      </block>
    </view>

    <!-- 右列表 -->
    <view class='shop'>
      <block wx:for="{{dataList}}" wx:key="_id" wx:if="{{item.index == 1}}">
        <my-datalist item="{{item}}" index="{{index}}" data-src="{{item.pictures[0]}}" data-index="{{index}}" bind:getimage="getimage" />
      </block>
    </view>
  </view>

 index.wxss

.shop-big-box{
  width: 100%;
}

.shop{
  width: 340rpx;
  margin: 0 10rpx;
}
/* 盒子水平摆放并水平居中 */
.flex-row-center-x{
  display: flex;
  flex-direction: row;
  justify-content: center;
}

index.js

// pages/index/index.js
const app = getApp()
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    leftheight: 0,
    rightheight: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

    this.getList()


  },


  getList() {
    var that = this
    wx.cloud.database().collection("community")
      .orderBy('top', 'desc')
      .orderBy('date', 'desc')
      .get({
        success(res) {
          console.log("出来的数据", res.data);
          if (res.data.length > 0) {
            var list = res.data
            var dataList = that.data.dataList
            var leftheight = that.data.leftheight
            var rightheight = that.data.rightheight

            for (let i = 0; i < list.length; i++) {
              if (leftheight <= rightheight) {
                leftheight = leftheight + list[i].picheight + 120
                list[i].index = 0
              } else {
                rightheight = rightheight + list[i].picheight + 120
                list[i].index = 1
              }
              dataList.push(list[i])
            }
            that.setData({
              dataList: dataList,
              leftheight: leftheight,
              rightheight: rightheight,
            })

          } 
        },
        fail(err) {
          wx.showToast({
            title: '网络出错啦!' + err,
            icon: 'none'
          })


        }
      })





  },


})

index.json

{
  "usingComponents": {
    "my-datalist": "../../components/datalist/datalist"
  },
  "navigationStyle": "custom",
  "enablePullDownRefresh": true
}

 components/datalist.js

const app = getApp()
const db = wx.cloud.database()
const _ =db.command
Component({
  properties: {
    item: JSON,
    index:String,
  },
  methods: {
     
      godetail(e) {
        wx.navigateTo({
          url: '/pages/Filecommunity/detail/detail?id=' + e.currentTarget.dataset.id,
        })
      },
      getimage() {
        this.triggerEvent('getimage');
      },
    
  },
});

 components/datalist.json

{
  "component": true,
  "usingComponents": {}
}

 components/datalist.wxml

<view class="shop-detail" bindtap="godetail" data-id="{{item._id}}">
  <view class='imagecont' style="height:{{item.picheight}}rpx;">
    <block wx:if="{{item.pictures.length > 0}}">
      <image src="{{item.pictures[0]}}" data-index="{{index}}" class="prodimg" style="height:{{item.picheight}}rpx;z-index: 3;" mode="aspectFill" />
    </block>

  </view>
  <view style="width: 100%;display: flex;flex-wrap: wrap;height: 120rpx;">
    <view class="shop-detail-text">{{item.text}}</view>

    <view class="shop-detail-user flex-row-center">
      <image src="{{item.user.imagavatares}}" style="border-radius: 50%;width: 30rpx;height: 30rpx;margin: 0 10rpx;background-color: rgb(247, 247, 247);" />
      <text>{{item.user.username}}</text>
    </view>
  </view>
</view>

 components/datalist.wxss


.shop-detail{
  border-radius: 10rpx;
  width: 340rpx;
  background: #fff;
  display: inline-block;
   font-size: 28rpx;
  margin: 10rpx 0;
}


.shop-detail-text{
  font-size: 28rpx;
  width: 100%;
  margin: 10rpx 0;
  overflow:hidden;
  white-space:nowrap;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
.shop-detail-user{
  display: flex;
  flex-direction: row;
  align-items: center;
  overflow:hidden;
  white-space:nowrap;
  font-size: 24rpx;
  height: 26rpx;
  margin: 10rpx 0 10rpx 0;
  text-overflow: ellipsis;
  width: 100%;
}


.imagecont{
  width: 100%;
  font-size: 0;
  position: relative;
}
.prodimg {
  width: 100% !important;
  vertical-align: middle !important;
  border-radius: 10rpx !important;
  position: absolute !important;
  font-size:0 !important;

}
/* 盒子水平摆放并垂直居中 */
.flex-row-center{
  display: flex;
  flex-direction: row;
  align-items: center;
}

这篇对您有所帮助的话,来个点赞或关注吧❀❀~,另外要预览效果的可以搜索邑学宝微信小程序呦~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1691368.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基金/证券项目如何进行非交易日数据补全(实战)

一些大数据开发的项目&#xff0c;特别是基金/证券公司的项目&#xff0c;都经常会涉及到交易日与非交易日的概念。 如果要让你对一张交易日跑批的主表&#xff0c;怎么去补全非交易日的数据呢&#xff1f; 在遇到这种情况的时候&#xff0c;我们要去怎么处理&#xff1f;来&…

模板编译之入口分析

Vue 是一个渐进式 JavaScript 框架&#xff0c;提供了简单易用的模板语法&#xff0c;帮助开发者以声明式的方式构建用户界面。Vue 的模板编译原理是其核心之一&#xff0c;它将模板字符串编译成渲染函数&#xff0c;并在运行时高效地更新 DOM。本文将深入探讨 Vue 模板编译的原…

图片分类模型训练及Web端可视化预测(下)——Web端实现可视化预测

Web端实现可视化预测 基于Flask搭建Web框架&#xff0c;实现HTML登录页面&#xff0c;编写图片上传并预测展示页面。后端实现上一篇文章所训练好的模型&#xff0c;进行前后端交互&#xff0c;选择一张图片&#xff0c;并将预测结果展示到页面上。 文章目录 Web端实现可视化预测…

前端基于word模板导出word文档

项目环境 vue2 js vue-cli等 依赖包都可以在npm官网找到对应文档 npm官网(英文) 1、依赖 安装依赖 docxtemplater npm i docxtemplaterfile-saver npm i file-saverjszip-utils npm i jszip-utilsjszip npm i jszip在对应页面或模块中引入依赖 import Docxtemplater …

探索AI写作工具:五款推荐

在现实生活中&#xff0c;除了专业的文字工作者&#xff0c;各行各业都避免不了需要写一些东西&#xff0c;比如策划案、论文、公文、讲话稿、总结计划……等等。而随着科技的进步&#xff0c;数字化时代的深入发展&#xff0c;AI已经成为日常工作中必不可少的工具了&#xff0…

智慧之选:开源与闭源大模型的未来探索

✨✨ 欢迎大家来访Srlua的博文&#xff08;づ&#xffe3;3&#xffe3;&#xff09;づ╭❤&#xff5e;✨✨ &#x1f31f;&#x1f31f; 欢迎各位亲爱的读者&#xff0c;感谢你们抽出宝贵的时间来阅读我的文章。 我是Srlua小谢&#xff0c;在这里我会分享我的知识和经验。&am…

有一个3x4的矩阵,要求编写程序求出其中值最大的那个元素,以及其所在的行号和列号

解题思路&#xff1a; 先考虑解此问题的思路。从若干数中求最大数的方法很多&#xff0c;现在采用"打擂台"的算法。如果有若干人比武&#xff0c;先有一人站在台上&#xff0c;再上去一人与其交手&#xff0c;败者下台&#xff0c;胜者留在台上。第3个人再上…

selenium安装出错

selenium安装步骤&#xff08;法1&#xff09;&#xff1a; 安装失败法1 第一次实验&#xff0c;失败 又试了一次&#xff0c;失败 安装法2-失败&#xff1a; ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: c:\\programdata\\a…

【C++题解】1697. 请输出n~1之间所有的整数

问题:1697. 请输出n~1之间所有的整数 类型&#xff1a;循环 题目描述&#xff1a; 从键盘读入一个整数 n &#xff0c;请输出 n∼1 之间所有的整数&#xff0c;每行输出 1 个。 比如&#xff0c;假设读入 n5 &#xff0c;输出结果如下&#xff1a; 5 4 3 2 1 输入&#xff1…

UE5 像素流与web 交互

总结下虚幻与网页的交互&#xff0c;这里将ue5 与js 交互传递参数记录下&#xff0c;其它的博主写的就是缺胳膊少腿的要么就是封闭收费&#xff0c;这个是在官方可以查询到。这里记录下&#xff1a; 点个关注不迷路&#xff1a; 具体的使用如下&#xff1a; 在你的游戏玩家类…

推荐几款新手学习编程的网站

免费在线开发平台 介绍一款编程平台&#xff0c;专为学生和开发者量身打造&#xff01;平台拥有近4000道编程题目&#xff0c;支持多种编程语言&#xff08;包括C、C、JavaScript、TypeScript、Go、Rust、PHP、Java、Ruby、Python3和C#&#xff09;&#xff0c;为您提供全面的学…

【Oracle篇】rman标准化全库备份策略:完整备份or增量备份(第三篇,总共八篇)

&#x1f4ab;《博主介绍》&#xff1a;✨又是一天没白过&#xff0c;我是奈斯&#xff0c;DBA一名✨ &#x1f4ab;《擅长领域》&#xff1a;✌️擅长Oracle、MySQL、SQLserver、阿里云AnalyticDB for MySQL(分布式数据仓库)、Linux&#xff0c;也在扩展大数据方向的知识面✌️…

YoloV8改进策略:蒸馏改进|CWDLoss|使用蒸馏模型实现YoloV8无损涨点|特征蒸馏

摘要 在本文中&#xff0c;我们成功应用蒸馏策略以实现YoloV8小模型的无损性能提升。我们采用了CWDLoss作为蒸馏方法的核心&#xff0c;通过对比在线和离线两种蒸馏方式&#xff0c;我们发现离线蒸馏在效果上更为出色。因此&#xff0c;为了方便广大读者和研究者应用&#xff…

Kubernetes Service 之原理与 ClusterIP 和 NodePort 用法

Kubernetes Service 之原理与 ClusterIP 和 NodePort 用法 Service 定义 在 Kubernetes 中&#xff0c;由于Pod 是有生命周期的&#xff0c;如果 Pod 重启它的 IP 可能会发生变化以及升级的时候会重建 Pod&#xff0c;我们需要 Service 服务去动态的关联这些 Pod 的 IP 和端口…

六个免费的AI制图网站的介绍

六个免费的AI制图网站的介绍 以下是六个免费的AI制图网站的介绍&#xff0c;包括官网、特点、缺点以及使用时的注意事项&#xff1a; 海鲸AI 官网&#xff1a;AI绘画创作平台 - 海鲸AI | 智能艺术生成器特点&#xff1a;支持PC和移动端&#xff0c;集成了Midjourney AI模型&am…

【Spring】SSM介绍_SSM整合

1、SSM介绍 1.1简介 SSM&#xff08;Spring SpringMVC MyBatis&#xff09;整合是一种流行的Java Web应用程序框架组合&#xff0c;它将Spring框架的核心特性、SpringMVC作为Web层框架和MyBatis作为数据访问层框架结合在一起。这种整合方式提供了从数据访问到业务逻辑处理再…

jmeter之测试计划

一、测试计划作用 测试计划是jmeter的默认控件所有线程组都是测试计划的下级控件测试计划可以配置用户自定义的变量测试计划可以配置线程组的串行或并行 二、查看界面 名称&#xff1a;可以修改自定义的名称注释&#xff1a;解释测试计划是用来做什么的用户自定义的变量&…

【Jmeter】使用Jmeter进行接口测试、跨线程组获取参数

Jmeter接口测试 Jmeter设置成中文实操练习-跨线程组提取参数&#xff0c;使用值HTTP请求默认值&HTTP信息头管理器 相信打算从事测试工程师的同学们&#xff0c;肯定对Jmeter是耳熟能详的。使用Jmeter可以进行接口测试、性能测试、压力测试等等&#xff1b;这个章节介绍如何…

在使用LabVIEW控制多个串口设备进行数据读取时,读取时间过长

在使用LabVIEW控制多个串口设备进行数据读取时&#xff0c;如果发现数据更新时间超过5秒&#xff0c;可以从以下几个方面进行分析和解决&#xff1a; 1. 串口配置与通信参数 确保每个串口的通信参数&#xff08;波特率、数据位、停止位、校验位等&#xff09;配置正确&#x…

百度软件测试面试经历,期望薪资27K

一面 1、 请为百度搜索框设计测试用例&#xff1f; 2、百度设计框上线前需要进行那些测试&#xff1f; 界面测试&#xff0c;功能测试&#xff0c;性能测试&#xff0c;安全性测试&#xff0c;易用性测试&#xff0c;兼容性测试&#xff0c;UI测试。 3、如何查看http状态码…