舟山闪爸电子技术有限公司

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


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

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

流式布局概念

流式布局也叫百分比布局
把元素的寬,高,margin,padding不再用固定數(shù)值,改用百分比,
這樣元素的寬,高,margin,padding會根據(jù)頁面的尺寸隨時
調(diào)整已達到適應(yīng)當前頁面的目的.

先看效果:
小程序如何制作商品列表流式布局

如上圖所示,為了能夠看的更直觀一點我給布局設(shè)置了紅色虛線邊框,整體頁面根據(jù)元素的百分比進行布局。
直接看代碼:

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標簽發(fā)現(xiàn)有兩個img_item ,所以采取的是左右分別加載數(shù)據(jù)方式。

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;  //圖片設(shè)置的寬度
    let scale = imgWidth / oImgW;        //比例計算
    let imgHeight = oImgH * scale;      //自適應(yīng)高度

    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
    };

    //當前這組圖片已加載完畢,則清空圖片臨時加載區(qū)域的內(nèi)容
    if (!loadingCount) {
      data.images = [];
    }

    this.setData(data);
  },

  loadImages: function () {
    let images = [
      {
        goodId: 0,
        name: '泊爾崖蜜蜜光面膜(5片盒裝)',
        url: 'bill',
        imageurl: 'http://www.fluoresville.cn/UploadFiles/2021-7/82/b3132703574748462334GRB.jpg',
        newprice: "86",
        oldprice: "88",
        discount: "8.8",
        height: 0, 
      },
      {
        goodId: 1,
        name: '透無瑕礦物養(yǎng)護兩用粉餅#03',
        url: 'bill',
        imageurl: 'http://www.fluoresville.cn/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.fluoresville.cn/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.fluoresville.cn/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.fluoresville.cn/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);
}**
檢索商品集合根據(jù)高度分別放入兩個集合中。

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步上線。通過拖拽、拼接模塊布局小程序商城頁面,所看即所得,只需要美工就能做出精美商城。更多小程序商店請查看:小程序商店

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


【本站聲明】
  1、本站文章中所選用的圖片及文字來源于網(wǎng)絡(luò)以及用戶投稿,由于未聯(lián)系到知識產(chǎn)權(quán)人或未發(fā)現(xiàn)有關(guān)知識產(chǎn)權(quán)的登記,如有知識產(chǎn)權(quán)人并不愿意我們使用,如果有侵權(quán)請立即聯(lián)系。
  2、本網(wǎng)站不對文章中所涉及的內(nèi)容真實性、準確性、可靠性負責,僅系客觀性描述,如您需要了解該類商品/服務(wù)詳細的資訊,請您直接與該類商品/服務(wù)的提供者聯(lián)系。


KESION 科汛軟件

KESION 科汛軟件是國內(nèi)領(lǐng)先的在線教育軟件及私域社交電商軟件服務(wù)提供商,長期專注于為企業(yè)提供在線教育軟件及社交電商SaaS平臺解決方案。
公司核心產(chǎn)品云開店SaaS社交電商服務(wù)平臺、在線教育SaaS服務(wù)平臺、教育企業(yè)數(shù)字化SaaS云平臺、企微營銷助手、私有化獨立部署品牌網(wǎng)校和在線教育咨詢等。

KESION 不斷通過技術(shù)創(chuàng)新,提供產(chǎn)品和服務(wù),助力企業(yè)向數(shù)字化轉(zhuǎn)型,通過科技驅(qū)動商業(yè)革新,讓商業(yè)變得更智慧!



▼點擊進入科汛官網(wǎng)了解更多



熱門標簽
SaaS
上/下篇
換一換相關(guān)推薦
精選內(nèi)容
熱點精選