177-371-24501(同微信)

小程序如何制作商品列表流式布局

2022-04-21 管理员

很多商城小程序中,一般的商品列表是以流式布局方式展示,下面为大家介绍小程序如何制作商品列表流式布局?

流式布局概念

流式布局也叫百分比布局
把元素的宽,高,margin,padding不再用固定数值,改用百分比,
这样元素的宽,高,margin,padding会根据页面的尺寸随时
调整已达到适应当前页面的目的.

先看效果:

如上图所示,为了能够看的更直观一点我给布局设置了红色虚线边框,整体页面根据元素的百分比进行布局。
直接看代码:

xxx.wxml

<scroll-view scroll-y="true" style="height:{{scrollH}}px" bindscrolltolower="loadImages">
  <view class="goods" style="width:100%">
    <view class="mg_item">
      <view wx:for="{{col1}}" wx:key="id">
      <view class="item_info">
        <image src="{{item.imageurl}}" style="width:100%;height:{{item.height}}px"></image>
      </view>
            <view class="product-name">
              {{item.name}}
            </view>
            <view class="product-price-wrap">
              <p class="product-price-new">¥{{item.newprice}}</p>  
              <p class="product-price-old">¥{{item.oldprice}}</p> 
               <p class="discount">{{item.discount}}折</p>   
            </view>
      </view>
    </view>
    <view class="mg_item">
      <view wx:for="{{col2}}" wx:key="id">
      <view class="item_info">
        <image src="{{item.imageurl}}" style="width:100%;height:{{item.height}}px"></image>
      </view>
            <view class="product-name">
              {{item.name}}
            </view>
            <view class="product-price-wrap">
              <p class="product-price-new">¥{{item.newprice}}</p>  
              <p class="product-price-old">¥{{item.oldprice}}</p> 
               <p class="discount">{{item.discount}}折</p>   
            </view>
      </view>
    </view>
  </view>
</scroll-view>
 <view style="display:none">
  <image wx:for="{{images}}" wx:key="id" id="{{item.id}}" src="{{item.imageurl}}" bindload="onImageLoad"></image>
</view> 

通过查看class标签发现有两个img_item ,所以采取的是左右分别加载数据方式。

xxx.js


Page({

  data: {
    scrollH: 0,
    imgWidth: 0,
    loadingCount: 0,
    images: [],
    col1: [],
    col2: []
  },

  onl oad: function () {
    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let wh = res.windowHeight;
        let imgWidth = ww * 0.48;
        let scrollH = wh;

        this.setData({
          scrollH: scrollH,
          imgWidth: imgWidth
        });

        //加载首组图片
        this.loadImages();
      }
    })
  },

  onImageLoad: function (e) {
    let imageId = e.currentTarget.id;
    let oImgW = e.detail.width;         //图片原始宽度
    let oImgH = e.detail.height;        //图片原始高度
    let imgWidth = this.data.imgWidth;  //图片设置的宽度
    let scale = imgWidth / oImgW;        //比例计算
    let imgHeight = oImgH * scale;      //自适应高度

    let images = this.data.images;
    let imageObj = null;

    for (let i = 0; i < images.length; i++) {
      let img = images[i];
      if (img.id === imageId) {
        imageObj = img;
        break;
      }
    }

    imageObj.height = imgHeight;

    let loadingCount = this.data.loadingCount - 1;
    let col1 = this.data.col1;
    let col2 = this.data.col2;

    //判断当前图片添加到左列还是右列
    if (col1.length <= col2.length) {
      col1.push(imageObj);
    } else {
      col2.push(imageObj);
    }

    let data = {
      loadingCount: loadingCount,
      col1: col1,
      col2: col2
    };

    //当前这组图片已加载完毕,则清空图片临时加载区域的内容
    if (!loadingCount) {
      data.images = [];
    }

    this.setData(data);
  },

  loadImages: function () {
    let images = [
      {
        goodId: 0,
        name: '泊尔崖蜜蜜光面膜(5片盒装)',
        url: 'bill',
        imageurl: 'http://www.kesion.com/UploadFiles/2021-7/82/b3132703574748462334GRB.jpg',
        newprice: "86",
        oldprice: "88",
        discount: "8.8",
        height: 0, 
      },
      {
        goodId: 1,
        name: '透无瑕矿物养护两用粉饼#03',
        url: 'bill',
        imageurl: 'http://www.kesion.com/UploadFiles/2021-7/82/b4132703574752056107TSC.jpg!75.webp',
        newprice: "147.00",
        oldprice: "150.00",
        discount: "8.8",
        height: 0,
      },
      {
        goodId: 2,
        name: '川水水光面膜(5片盒装)',
        url: 'bill',
        imageurl: 'http://www.kesion.com/UploadFiles/2021-7/82/b5132703574756118633QSA.jpg',
        newprice: "86.00",
        oldprice: "88.00",
        discount: "8.8",
        height: 0, 
      },
      {
        goodId: 3,
        name: '蜜三色渐变咬唇膏3.2g 03蜜橙动心恋',
        url: 'bill',
        imageurl: 'http://www.kesion.com/UploadFiles/2021-7/82/b61327035747714312316IW.jpg',
        newprice: "97.00",
        oldprice: "99.00",
        discount: "8.8",
        height: 0,
      },
      {
        goodId: 4,
        name: '时焕颜亮采套装',
        url: 'bill',
        imageurl: 'http://www.kesion.com/UploadFiles/2021-7/82/b71327035747737749969ZZ.jpg',
        newprice: "398.00",
        oldprice: "459.00",
        discount: "8.8",
        height: 0,
      }
    ];

    let baseId = "img-" + (+new Date());

    for (let i = 0; i < images.length; i++) {
      images[i].id = baseId + "-" + i;
    }

    this.setData({
      loadingCount: images.length,
      images: images
    });
  }

})

如下代码:
**if (col1.length <= col2.length) {
col1.push(imageObj);
} else {
col2.push(imageObj);
}**
检索商品集合根据高度分别放入两个集合中。

xxx.wxss

page{  
  height: 100%;  
  background-color: #F3F4F6;
} 
/* 单个图片容器的样式 */
.img_item {
  width: 48.5%;
  margin: 2px;
  display: inline-block;
  vertical-align: top;
  background-color: #ffffff;
  font-size: 24rpx;

}
.item_info{
  border-top:1px dashed red;
}
.product-name{
  color: #000;
  /* height: 28px; */
  text-align:left;
   margin: 0px 5px; 
   margin-bottom: 5px; 
}
.product-price-wrap .product-price-new{
  color: #e80080;
  margin-left:5px;
  font-weight:900;
}
.product-price-wrap .product-price-old{
  color: #888;
  text-decoration: line-through;
  padding-left: 2px;
}
.product-price-wrap .discount{
  margin-left: 30px;
  background-color: #000;
  color: #fff;
}

小程序工具提供多类型商城/门店小程序制作,可视化编辑 1秒生成5步上线。通过拖拽、拼接模块布局小程序商城页面,所看即所得,只需要美工就能做出精美商城。更多小程序商店请查看:小程序商店

<link href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css" rel="stylesheet" />
上一篇:微信群聊天小程序入口在哪,有什么用
下一篇:小程序图片转成base64的实现方式
相关资讯 Releva ntnews
解决方案 Solutions
相关热点 Hot spot
快速提升网站排名的方法
  1. 我们的优势
  2. 我们的实力
  3. 选择我们的理由
咨询电话(微信同号)

177-371-24501(同微信)

豫ICP备17049932号

Copyright © 2017-2022 版权所有 追风建站 Rights Reserved 技术支持:酷微科技

电话咨询 在线咨询 服务项目 SEO优化