欢迎来到懒人模板!我们专注移动互联网,所有模板永久免费下载!
  • 首 页
  • 当前位置:主页 > 小程序教程 >

    微信小程序商城电商购物车页面制作与设计

    2018-05-15 09:25 来源/作者:懒人模板 分类:小程序教程  « »
    微信小程序商城电商整套系列相关推荐:
    1、微信小程序开发页面顶部导航栏制作设计 [图]
    2、微信小程序开发底部导航菜单栏的制作设计 [图]
    3、微信小程序开发广告轮播幻灯图制作设计 [图]
    4、微信小程序开发商城电商首页整体布局制作设计 [图]
    5、微信小程序开发分类页面栅格布局制作与设计 [图]
    6、微信小程序开发商品详情页中sku的弹出属性筛选选择框 [图]
    7、微信小程序商品正文详情页整体布局制作与设计 [图]
    8、微信小程序商城电商购物车页面制作与设计 [图]
    9、微信小程序商城电商个人会员中心页面制作设计 [图]

    今天主要微信小程序商城电商购物车页面制作与设计,其中包含wxss  js   wxml三个页面代码。做好以后实现效果如下图:


    一、wxss样式文件代码如下:
    /* pages/cart/cart.wxss */
    .cart_container {
      display: flex;
      flex-direction: row;
    }
    .scroll {
      margin-bottom: 120rpx;
    }
    .column {
      display: flex;
      flex-direction: column;
    }
    .row {
      display: flex;
      flex-direction: row;
      align-items: center;
    }
    .sku {
      margin-top: 60rpx;
      margin-left: 100rpx;
    }
    .sku-price {
      color: red;
      position: relative;
      margin-top: 70rpx;
    }
    .price {
      color: red;
      position: relative;
    }
    .title {
      font-size: 38rpx;
      margin-top: 40rpx;
    }
    .small_text {
      font-size: 28rpx;
      margin-right: 40rpx;
      margin-left: 10rpx;
    }
    .item-select {
      width: 40rpx;
      height: 40rpx;
      margin-top: 90rpx;
      margin-left: 20rpx;
    }
    .item-allselect {
      width: 40rpx;
      height: 40rpx;
      margin-left: 20rpx;
    }
    .item-image {
      width: 180rpx;
      height: 180rpx;
      margin: 20rpx;
    }
    .bottom_line {
      width: 100%;
      height: 2rpx;
      background: lightgray;
    }
    .bottom_total {
      position: fixed;
      display: flex;
      flex-direction: column;
      bottom: 0;
      width: 100%;
      height: 120rpx;
      line-height: 120rpx;
      background: white;
    }
    .button-red {
      background-color: #f44336; /* 红色 */
    }
    button {
      position: fixed;
      right: 0;
      color: white;
      text-align: center;
      display: inline-block;
      font-size: 30rpx;
      border-radius: 0rpx;
      width: 30%;
      height: 120rpx;
      line-height: 120rpx;
    }
    二、js测试数据文件代码如下:
    // pages/cart/cart.js
    var Temp = require('../../template/contract.js');
    Page(Object.assign({}, Temp.Quantity, {
      data: {
        isAllSelect:false,
        totalMoney:0,
        // 商品详情介绍
        carts: [
          {
            pic: "http://www.lanrenmb.com/files/product/20161201/148058328876.jpg",
            name:"日本资生堂洗颜",
            price:200,
            isSelect:false,
            // 数据设定
            count: {
              quantity: 2,
              min: 1,
              max: 20
            },
          },
          {
            pic: 'http://www.lanrenmb.com/files/product/20161201/148058301941.jpg',
            name: "倩碧焕妍活力精华露",
            price: 340,
            isSelect: false,
            // 数据设定
            count: {
              quantity: 1,
              min: 1,
              max: 20
            },
          },
          {
            pic: 'http://www.lanrenmb.com/files/product/20161201/14805828016.jpg',
            name: "特效润肤露",
            price: 390,
            isSelect: false,
            // 数据设定
            count: {
              quantity: 3,
              min: 1,
              max: 20
            },
          },
          {
            pic: 'http://www.lanrenmb.com/files/product/20161201/148058228431.jpg',
            name: "倩碧水嫩保湿精华面霜",
            price: 490,
            isSelect: false,
            // 数据设定
            count: {
              quantity: 1,
              min: 1,
              max: 20
            },
          },
          {
            pic: 'http://www.lanrenmb.com/files/product/20161201/148057953326.jpg',
            name: "兰蔻清莹柔肤爽肤水",
            price: 289,
            isSelect: false,
            // 数据设定
            count: {
              quantity: 10,
              min: 1,
              max: 20
            },
          },
          {
            pic: "http://www.lanrenmb.com/files/product/20161201/148057921620_middle.jpg",
            name: "LANCOME兰蔻小黑瓶精华",
            price: 230,
            isSelect: false,
            // 数据设定
            count: {
              quantity: 1,
              min: 1,
              max: 20
            },
          },
        ],
      },
    
      //勾选事件处理函数  
      switchSelect: function (e) {
        // 获取item项的id,和数组的下标值  
        var Allprice = 0,i=0;
        let id = e.target.dataset.id,
    
        index = parseInt(e.target.dataset.index);
        this.data.carts[index].isSelect = !this.data.carts[index].isSelect;
        //价钱统计
        if (this.data.carts[index].isSelect) {
          this.data.totalMoney = this.data.totalMoney + this.data.carts[index].price;
        }
        else {
          this.data.totalMoney = this.data.totalMoney - this.data.carts[index].price;
        }
       //是否全选判断
        for (i = 0; i < this.data.carts.length; i++) {
          Allprice = Allprice + this.data.carts[i].price;
        }
        if (Allprice == this.data.totalMoney)
        {
          this.data.isAllSelect=true;
        }
        else 
        {
          this.data.isAllSelect = false;
        }
        this.setData({
          carts: this.data.carts,
          totalMoney: this.data.totalMoney,
          isAllSelect: this.data.isAllSelect,
        })
      },
      //全选
      allSelect: function (e) {
        //处理全选逻辑
        let i = 0;
        if (!this.data.isAllSelect)
        {
          for (i = 0; i < this.data.carts.length; i++) {
            this.data.carts[i].isSelect = true;
            this.data.totalMoney = this.data.totalMoney + this.data.carts[i].price;
          }
        }
        else
        {
          for (i = 0; i < this.data.carts.length; i++) {
            this.data.carts[i].isSelect = false;
          }
          this.data.totalMoney=0;
        }
        this.setData({
          carts: this.data.carts,
          isAllSelect: !this.data.isAllSelect,
          totalMoney: this.data.totalMoney,
        })
      },
      // 去结算
      toBuy() {
        wx.showToast({
          title: '去结算',
          icon: 'success',
          duration: 3000
        });
        this.setData({
          showDialog: !this.data.showDialog
        });
      },
      //数量变化处理
      handleQuantityChange(e) {
        var componentId = e.componentId;
        var quantity = e.quantity;
        this.data.carts[componentId].count.quantity = quantity;
        this.setData({
          carts: this.data.carts,
        });
      }
    }));
    以上js文件需要注意如下两点:
    allSelect 全选按钮的逻辑处理 
    1. 全选就把每个item勾选图标点亮,然后统计总价钱,不全选就置为灰色,总价钱为0 
    2. this.setData更新数据
     
    switchSelect 勾选按钮需要做的逻辑处理 
    1. 判断是否达到全部勾选,如果全部勾选,底部的全选按钮要点亮,判断依据是,价钱是否等于总价,当然这只是一种判断方式,读者也可以通过勾选的数量判断, 
    2. 对勾选或取消的按钮,进行总价的加减法计算 
    3. this.setData,更新数据,这个是重点,每次处理完数据,都要记得更新数据
    三、wxml页面代码如下:
    <import src="/template/quantity/index.wxml" />
    <scroll-view class="scroll" scroll-y="true">
      <view class="separate"></view>
      <view wx:for="{{carts}}">
        <view class="cart_container">
          <image class="item-select" bindtap="switchSelect" data-index="{{index}}" data-id="{{index}}" src="{{item.isSelect?'../../images/cart/comment_select.png':'../../images/cart/comment_normal.png'}}" />
    
          <image class="item-image" src="{{item.pic}}"></image>
    
          <view class="column">
            <text class="title">{{item.name}}</text>
            <view class="row">
              <text class="sku-price">¥</text>
              <text class="sku-price">{{item.price}}</text>
              <view class="sku">
                <template is="quantity" data="{{ ...item.count, componentId: index }}" />
              </view>
            </view>
    
          </view>
        </view>
        <view class="separate"></view>
      </view>
    </scroll-view>
    <view class="bottom_total">
      <view class="bottom_line"></view>
    
      <view class="row">
        <image class="item-allselect" bindtap="allSelect" src="{{isAllSelect?'../../images/cart/comment_select.png':'../../images/cart/comment_normal.png'}}" />
        <text class="small_text">全选</text>
        <text>合计:¥ </text>
        <text class="price">{{totalMoney}}</text>
        <button class="button-red" bindtap="toBuy" formType="submit">去结算</button>
      </view>
    </view>

    点击扫描效果预览免费下载免登陆网盘下载
    标签:
    * 懒人模板承诺:本站所有资源免费下载,无病毒,无弹窗,无干扰链接! 提点建议