今天给大家分享一下微信小程序中商品列表的流式布局方式,根据文章内容操作就可以看到效果哦~~~

流式布局概念

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

先看效果:

 
商品列表流式布局.gif

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

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: []
  },

  onLoad: 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: 'https://a3.vimage1.com/upload/merchandise/pdcvis/2017/08/21/142/fb2960bf8e074d029c24315279289c19-5_218x274_70.jpg',
        newprice: "86",
        oldprice: "88",
        discount: "8.8",
        height: 0,
      },
      {
        goodId: 1,
        name: '透无瑕矿物养护两用粉饼#03',
        url: 'bill',
        imageurl: 'https://a.appsimg.com/upload/brand/upcb/2017/10/26/77/ias_150899004322921_604x290_80.jpg!75.webp',
        newprice: "147.00",
        oldprice: "150.00",
        discount: "8.8",
        height: 0,
      },
      {
        goodId: 2,
        name: '川水水光面膜(5片盒装)',
        url: 'bill',
        imageurl: 'https://a2.vimage1.com/upload/merchandise/pdcvis/2017/08/21/86/7891361fdab348a1bc91aeca31fc77b1-5_218x274_70.jpg',
        newprice: "86.00",
        oldprice: "88.00",
        discount: "8.8",
        height: 0,
      },
      {
        goodId: 3,
        name: '蜜三色渐变咬唇膏3.2g 03蜜橙动心恋',
        url: 'bill',
        imageurl: 'http://a3.vimage1.com/upload/merchandise/pdcvis/2017/08/21/176/c3b9453a4d7f46c6a8fe78705f77352b-5_218x274_70.jpg',
        newprice: "97.00",
        oldprice: "99.00",
        discount: "8.8",
        height: 0,
      },
      {
        goodId: 4,
        name: '时焕颜亮采套装',
        url: 'bill',
        imageurl: 'https://a2.vimage1.com/upload/merchandise/pdcvis/2017/08/21/93/69a6bc1c11eb4be184b7dffb43b8565b-5_218x274_70.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;
}

好了,商品列表流式布局方式 代码示例已经讲完了,赶紧尝试一下吧!

作者:IT实战联盟Lin
链接:https://www.jianshu.com/p/72d57252b4ef
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

微信小程序电商实战-商品列表流式布局的更多相关文章

  1. 微信小程序电商实战-商品详情(上)

    先看一下今天要实现的小程序商品详情页吧!   商品详情.gif 本期我们要实现小程序商品详情页的头部标题.头部轮播.商品详情浮动按钮和商品内页布局. 一.设置头部标题 如上图所示,头部标题是商品详情 ...

  2. 微信小程序电商实战-首页(上)

    嗨,大家好!经过近两周的精心准备终于开始微信小程序电商实战之路喽.那么最终会做成什么样呢?当然可以肯定不会只做一个静态demo哦,先把我们小程序电商实战的整体架构发出来晒一下,请看下图:   架构图. ...

  3. 微信小程序电商实战(-)商城首页

    首先在app.json中配置页面和底部tabbar { "pages":[ "pages/index/index", "pages/kind/kind ...

  4. 微信小程序电商实战-入门篇

    小程序开发工具有新版本更新啦!开发体验更好了,详情可以查看微信公众平台-小程序https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.h ...

  5. 微信小程序电商实战-首页(下)

    好了,上一期我们把首页搜索.导航栏和广告轮播给做完了,那么接下来会继续完成我们首页的剩余部分,先看我们要实现的效果吧!   本期实现效果图.gif 本期我们要实现首页的实时热销榜.福利专场和左下方个人 ...

  6. 微信小程序云开发-数据库-商品列表数据排序

    一.wxml添加升序和降序 在商品列表的wxml文件中添加超链接a标签,分别用于升序和降序的点击.分别绑定升序和降序的点击事件. 二.js文件实现升序和降序 分别写对应的按价格升序函数sortByPr ...

  7. 微信小程序云开发-数据库-商品列表数据跳过N条数据

    一.WXML实现 在wxml文件中添加对应链接,点击链接绑定事件skipGoods. 二.js实现 js文件中写skipGoods()函数,实现跳过2条数据的功能.

  8. 微信小程序云开发-数据库-商品列表数据显示N条数据

    一.wxml文件 在wxml文件中,写页面和点击事件,添加绑定事件limitGoods  二.js文件 在js文件中写limitGoods(),使用.limit(3)表示只显示3条数据

  9. 《微信小程序商城开发实战》笔者的新书,欢迎各位粉丝上京东购买

    作者图书京东链接,请点击------>>>    **微信小程序商城开发实战** 附京东真实评价截图: 编辑推荐 在当今移动互联网大潮中,微信应用凭借其庞大的用户基数和极强的用户黏性 ...

随机推荐

  1. Unity---UGUI入门基础---更新中

    目录 1.UGUI介绍 2.UGUI基础 2.1 Canvas---画布 2.2 Text控件 2.3 Image控件 2.4 RawImage控件 2.5 Button控件 2.6 Toggle控件 ...

  2. CF567E President and Roads

    \(\color{#0066ff}{ 题目描述 }\) 给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0) \( ...

  3. Linq中DeferredLoadingEnabled,DataLoadOption的用法

    1.  基本的数据关系图 Student和Class之间是多对一关系,Student和Course之间是多对多关系. DataContext的DeferredLoadingEnabled属性指定是否需 ...

  4. List<T> 循环修改其中的数据

    List<MyObject> myObject = new List<MyObject>(); myObject.ForEach(w => { w.字段1=123: if ...

  5. C语言中Extern用法

    extern用在变量或函数的声明前,用来说明“此变量/函数是在别处定义的,要在此处引用”. extern修饰变量的声明. 举例:若a.c中需引用b.c中的变量int v,可以在a.c中声明extern ...

  6. Luogu P4404 [JSOI2010]缓存交换 优先队列

    细节题?...调了半天.... 可以发现,每一次从缓存中删除的主存一定是下次访问最晚的,可以用优先队列来处理...还有要离散化...还有链表末尾要多建一些点...否则会死的很惨... #include ...

  7. 从M进制转换为N进制

    /// <summary> /// 从M进制转换为N进制 /// </summary> internal class MBase2NBase { /// <summary ...

  8. spring boot mysql 事务

    mysql默认 事务自动提交.即:每条insert/update/delete语句,不需要程序手工提交事务,而是mysql自行提交了. 如果我们想实现程序事务提交,需要事先关闭mysql的自动提交事务 ...

  9. 读书笔记:编程小白的第一本python入门书

    书名:编程小白的第一本python入门书 作者:侯爵 出版社/出处:图灵社区 年份:2016年 封面: 感想: 本书短小精悍,精华部分在于给编程小白打了鸡血的同时输出了一种“高效学习法的思想”. 个人 ...

  10. find 根据时间查找,详解

    https://blog.csdn.net/u010181136/article/details/73322738 find可谓是aix/linux上使用较多的维护用命令,但很多时候需要用到针对时间的 ...