【微信小程序】 实现购物车

这篇具有很好参考价值的文章主要介绍了【微信小程序】 实现购物车。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

【微信小程序】 实现购物车

原生实现,不使用任何框架,网上没有啥好看的购物车,而且都是抄来抄去的,我来写一个好点的作为参考吧,拿来就能用。

接口自行添加

链接:购物车代码片段。图片太大了没办法上传到代码片段,自己找几张图片就行。

购物车功能包括:

  • 显示默认地址,或选择地址(需定向到你自己的页面去选择,这里不做演示
  • 显示店铺,点击店铺可全选该店铺的所有商品
  • 显示商品,可选择商品,增加或减少数量,可左滑删除
    • 左滑删除:如果有一个已经显示出删除按钮,未操作,此时滑动另一个商品,之前的显示将回归原位
  • 选择商品下方显示总价并结算订单
  • 选择全部购物车商品
  • 显示已失效商品

效果图片

微信小程序购物车如何写,微信小程序,小程序,javascript,css,html5,es6
微信小程序购物车如何写,微信小程序,小程序,javascript,css,html5,es6
微信小程序购物车如何写,微信小程序,小程序,javascript,css,html5,es6
微信小程序购物车如何写,微信小程序,小程序,javascript,css,html5,es6
微信小程序购物车如何写,微信小程序,小程序,javascript,css,html5,es6文章来源地址https://www.toymoban.com/news/detail-771336.html

cart.wxml

<!-- 选择地址 -->
<view class="choose-address">
    <view class="address">配送至:<text>{{address}}</text></view>
    <image class="img" src="/images/arrow_right_grey.svg"></image>
</view>
<!-- 生效商品区域 -->
<view class="cart-effect">
    <block wx:for="{{cartEffectList}}" wx:key="key" wx:for-item="item">
        <checkbox-group class="check-group" wx:if="{{item.merchandises.length > 0}}">
            <checkbox class="check-store-all" data-store="{{item.store}}" bind:tap="checkStoreAll" checked="{{item.checked || item.merchandiseChecked}}">
                <view class="store">{{item.store}}</view>
            </checkbox>
            <block wx:for="{{item.merchandises}}" wx:key="key" wx:for-item="merchandise">
                <movable-area class="move-area">
                    <movable-view class="move-view" x="{{merchandise.x}}" data-store="{{item.store}}" data-id="{{merchandise.id}}" direction="horizontal" out-of-bounds="true" damping="50" inertia="true" bind:touchstart="touchStart" bind:touchmove="touchMove" bind:touchend="touchEnd" bindchange="touchChange">
                        <view class="info">
                            <checkbox class="merchandise-check" data-store="{{item.store}}" data-merchandise="{{merchandise}}" bind:tap="checkSingle" checked="{{merchandise.checked}}"></checkbox>
                            <view class="merchandise">
                                <image class="merchandise-img" src="{{merchandise.imgUrl}}"></image>
                                <view class="merchandise-name">{{merchandise.name}}</view>
                                <view class="merchandise-price">¥{{merchandise.price}}/瓶</view>
                                <view class="merchandise-amount">
                                    <view class="minus" data-store="{{item.store}}" data-id="{{merchandise.id}}" bind:tap="minusAmount">-</view>
                                    <view class="amount">{{merchandise.amount}}</view>
                                    <view class="plus" data-store="{{item.store}}" data-id="{{merchandise.id}}" bind:tap="plusAmount">+</view>
                                </view>
                            </view>
                        </view>
                    </movable-view>
                    <view class="delete" data-store="{{item.store}}" data-id="{{merchandise.id}}" bind:tap="deleteMerchandise">删除</view>
                </movable-area>
            </block>
        </checkbox-group>
    </block>
</view>
<!-- 失效商品 -->
<view class="cart-lapse">
    <block wx:for="{{cartLapseList}}" wx:key="key">
        <checkbox-group class="check-group" bindchange="checkStoreAll">
            <view class="store">已失效商品({{cartLapseList.length}})</view>
            <movable-area class="move-area">
                <movable-view class="move-view" x="{{item.x}}" data-id="{{item.id}}" direction="horizontal" out-of-bounds="true" damping="50" inertia="true" bind:touchstart="touchStart" bind:touchmove="touchMove" bind:touchend="touchEnd" bindchange="touchChange">
                    <view class="info">
                        <checkbox class="merchandise-check" disabled="true"></checkbox>
                        <view class="merchandise">
                            <view class="sold-out">
                                <image class="merchandise-img" src="{{item.imgUrl}}"></image>
                                <view class="status">{{item.info}}</view>
                            </view>
                            <view class="merchandise-name">{{item.name}}</view>
                            <view class="merchandise-price">¥{{item.price}}/瓶</view>
                            <view class="merchandise-amount">
                                <view class="minus" style="color: gray;">-</view>
                                <view class="amount" style="color: gray;">{{item.amount}}</view>
                                <view class="plus" style="color: gray;">+</view>
                            </view>
                        </view>
                    </view>
                </movable-view>
                <view class="delete" data-id="{{item.id}}" bind:tap="deleteMerchandise">删除</view>
            </movable-area>
        </checkbox-group>
    </block>
</view>
<!-- 结算 -->
<view class="count">
    <checkbox class="check-all" bind:tap="checkAll" checked="{{checkedAll}}">全选</checkbox>
    <view class="grand">合计:¥{{total}}</view>
    <button class="lapse {{totalCount > 0 ? 'settle-btn' : ''}}" bind:tap="settleBill" hover-class="settle-bill" disabled="{{totalCount == 0}}" loading="{{showLoading}}">结算({{totalCount}})</button>
</view>

cart.wxss

page {
    padding: 16rpx;
    padding-bottom: 172rpx;
    box-sizing: border-box;
}

/* 选择地址 */
.choose-address {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    text-align: left;
}

.choose-address .address {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.choose-address .img {
    width: 32rpx;
    height: 32rpx;
}
/* 选择地址 */

/* 生效商品 */
.cart-effect {
    display: flex;
    flex-direction: column;
}

.cart-effect .check-group {
    background-color: #fbfbfd;
    padding: 16rpx;
    box-sizing: border-box;
    border-radius: 32rpx;
    margin-top: 16rpx;
    z-index: 11;
}

.cart-effect .check-group .check-store-all {
    display: flex;
    justify-content: flex-start;
    align-items: center;
}

.cart-effect .check-group .store {
    font-size: 36rpx;
    font-weight: bolder;
    margin-left: 8rpx;
}

.cart-effect .check-group .move-area{
    /* 减去删除部分的宽度 */
    width: calc(100% - 128rpx);
    height: 280rpx;
    position: relative;
    display: flex;
}

.cart-effect .check-group .move-area .move-view {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    /* 上面减了多少宽度,下面就要加多少,不然无法起到遮挡作用 */
    width: calc(100% + 128rpx);
    background-color: #fbfbfd;
    box-sizing: border-box;
    z-index: 10;
    padding-right: 12rpx;
}

.cart-effect .check-group .move-area .delete {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 100%;
    /* 删除部分的宽度 */
    width: 128rpx;
    text-align: center;
    z-index: 9;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f43838;
    color: #fbfbfd;
}

.cart-effect .check-group .move-area .move-view .info {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100vw;
    overflow: hidden;
    box-sizing: border-box;
}

.cart-effect .check-group .move-area .move-view .info .merchandise {
    display: grid;
    grid-template-columns: 1fr 2fr 2fr;
    grid-template-rows: 110rpx 60rpx;
    grid-column-gap: 12rpx;
    grid-row-gap: 12rpx;
}

.cart-effect .check-group .move-area .move-view .info .merchandise .merchandise-img {
    width: 180rpx;
    height: 180rpx;
    border-radius: 50%;
    grid-column: 1 / 2;
    grid-row: 1 / 3;
}
.cart-effect .check-group .move-area .move-view .info .merchandise .merchandise-name {
    grid-column: 2 / 4;
    grid-row: 1 / 2;
    font-size: 32rpx;
}

.cart-effect .check-group .move-area .move-view .info .merchandise .merchandise-price {
    grid-column: 2 / 3;
    grid-row: 2 / 3;
    font-size: 32rpx;
    font-weight: 550;
}

.cart-effect .check-group .move-area .move-view .merchandise .merchandise-amount {
    grid-column: 3 / 4;
    grid-row: 2 / 3;
    text-align: right;
    

    display: grid;
    grid-template-columns: repeat(3, 1fr);
    align-items: center;
    text-align: center;
    line-height: 50rpx;
}

.cart-effect .check-group .move-area .move-view .merchandise .merchandise-amount .minus {
    font-size: 42rpx;
    background-color: #e5e5e5;
    border-radius: 16rpx;
}

.cart-effect .check-group .move-area .move-view .merchandise .merchandise-amount .amount {
    font-size: 36rpx;
}

.cart-effect .check-group .move-area .move-view .merchandise .merchandise-amount .plus {
    background-color: #e5e5e5;
    border-radius: 16rpx;
    font-size: 42rpx;
}

/* 失效商品 */
.cart-lapse {
    display: flex;
    flex-direction: column;
}

.cart-lapse .check-group {
    background-color: #fbfbfd;
    padding: 16rpx;
    box-sizing: border-box;
    border-radius: 32rpx;
    margin-top: 16rpx;
    z-index: 11;
}

.cart-lapse .check-group .check-store-all {
    display: flex;
    justify-content: flex-start;
    align-items: center;
}

.cart-lapse .check-group .store {
    font-size: 36rpx;
    font-weight: bolder;
}

.cart-lapse .check-group .move-area{
    /* 减去删除部分的宽度 */
    width: calc(100% - 128rpx);
    height: 280rpx;
    position: relative;
    display: flex;
}

.cart-lapse .check-group .move-area .move-view {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    /* 上面减了多少宽度,下面就要加多少,不然无法起到遮挡作用 */
    width: calc(100% + 128rpx);
    background-color: #fbfbfd;
    box-sizing: border-box;
    z-index: 10;
    padding-right: 12rpx;
}

.cart-lapse .check-group .move-area .delete {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 100%;
    /* 删除部分的宽度 */
    width: 128rpx;
    text-align: center;
    z-index: 9;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f43838;
    color: #fbfbfd;
}

.cart-lapse .check-group .move-area .move-view .info {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100vw;
    overflow: hidden;
    box-sizing: border-box;
}

.cart-lapse .check-group .move-area .move-view .info .merchandise {
    display: grid;
    grid-template-columns: 1fr 2fr 2fr;
    grid-template-rows: 110rpx 60rpx;
    grid-column-gap: 12rpx;
    grid-row-gap: 12rpx;
}

.cart-lapse .check-group .move-area .move-view .info .merchandise .sold-out {
    position: relative;
    width: 180rpx;
    height: 180rpx;
    border-radius: 50%;
}

.cart-lapse .check-group .move-area .move-view .info .merchandise .sold-out .merchandise-img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    grid-column: 1 / 2;
    grid-row: 1 / 3;
}

.cart-lapse .check-group .move-area .move-view .info .merchandise .sold-out .status{
    width: 100%;
    height: 100%;
    background-color: #1a1a1a90;
    position: absolute;
    top: 0;
    left: 0;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 50%;
    color: #fbfbfb;
    font-size: 36rpx;
    font-weight: 500;
}

.cart-lapse .check-group .move-area .move-view .info .merchandise .merchandise-name {
    grid-column: 2 / 4;
    grid-row: 1 / 2;
    font-size: 32rpx;
}

.cart-lapse .check-group .move-area .move-view .info .merchandise .merchandise-price {
    grid-column: 2 / 3;
    grid-row: 2 / 3;
    font-size: 32rpx;
    font-weight: 550;
}

.cart-lapse .check-group .move-area .move-view .merchandise .merchandise-amount {
    grid-column: 3 / 4;
    grid-row: 2 / 3;
    text-align: right;

    display: grid;
    grid-template-columns: repeat(3, 1fr);
    align-items: center;
    text-align: center;
    line-height: 60rpx;
}

.cart-lapse .check-group .move-area .move-view .merchandise .merchandise-amount .minus {
    font-size: 42rpx;
    background-color: #e5e5e5;
    border-radius: 16rpx;
}

.cart-lapse .check-group .move-area .move-view .merchandise .merchandise-amount .amount {
    font-size: 36rpx;
}

.cart-lapse .check-group .move-area .move-view .merchandise .merchandise-amount .plus {
    background-color: #e5e5e5;
    border-radius: 16rpx;
    font-size: 42rpx;
}

.count {
    width: calc(100% - 32rpx);
    box-sizing: border-box;
    margin-top: 16rpx;
    padding: 16rpx;
    background-color: #fbfbfd;
    border-radius: 120rpx;
    position: fixed;
    bottom: 42rpx;
    z-index: 99;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1rpx solid #e5e5e5;
}

.count .check-all{
    font-size: 34rpx;
    font-weight: 550;
}

.count .grand {
    width: 50%;
    text-align: right;
    font-size: 32rpx;
    font-weight: 550;
    color: #d1362f;
}

.count .lapse {
    width: 200rpx;
    height: 80rpx;
    color: #fdfdfd;
    border-radius: 120rpx;
    margin: 0;
    text-align: center;
    background-color: #e5e5e5;
    padding: 0;
    line-height: 80rpx;
}

.count .settle-btn {
    background-color: #d1362f;
}

.count .settle-bill {
    background-color: #d1362f80
}

cart.js

Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: "XX省XX市XX区XX街道XX号XX室",
        cartEffectList: [],
        // 生效商品
        example: [
            {
                store: "龙门客栈",
                checked: false,
                merchandiseChecked: false,
                merchandises: [
                    {
                        id: "1",
                        imgUrl: "/images/wine1.jpg",
                        name: "宫廷玉液酒",
                        price: 180,
                        amount: 1,
                        status: 0,
                        x: 0,
                        checked: false,
                    },
                    {
                        id: "2",
                        imgUrl: "/images/wine2.png",
                        name: "群英荟萃",
                        price: 98,
                        amount: 99,
                        status: 1,
                        x: 0,
                        checked: false,
                    },
                    {
                        id: "3",
                        imgUrl: "/images/wine3.png",
                        name: "二锅头",
                        price: 28,
                        amount: 1,
                        status: 1,
                        x: 0,
                        checked: false,
                    },
                ]
            },
            {
                store: "八马茶叶",
                checked: false,
                merchandiseChecked: false,
                merchandises: [
                    {
                        id: "1",
                        imgUrl: "/images/wine3.png",
                        name: "黑普洱茶饼",
                        price: 1800,
                        amount: 1,
                        status: 1,
                        x: 0,
                        checked: false,
                    },
                ]
            },
        ],
        // 失效商品
        cartLapseList: [
            {
                id: "3",
                imgUrl: "/images/wine2.png",
                name: "威士忌",
                info: "已售罄",
                price: 148,
                amount: 1,
                x: 0,
                status: -1,
                checked: false,
            }
        ],
        startX: 0,
        moveStore: '',
        delBtnW: 128,
        isLeft: 0,
        total: 0,
        totalCount: 0,
        checkedAll: false,
        billLoading: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getCartList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 获取用户购物车
     */
    getCartList() {
        let cartEffectList = this.data.example;
        wx.setStorageSync('cartEffectList', cartEffectList);
        this.setData({
            cartEffectList: cartEffectList,
        });
    },

    /**
     * 店铺全选
     * @param {*} e 
     */
    checkStoreAll(e) {
        let storeName = e.currentTarget.dataset.store;
        let cartEffectList = this.data.cartEffectList;
        let updatedCart = cartEffectList.map(store => {
            if (store.store === storeName) {
                // 切换商店的已勾选标记
                store.checked = !store.checked;
                // 重置商店的商品已勾选标记
                store.merchandiseChecked = false;
                store.merchandises = store.merchandises.map(merch => {
                    // 根据商店的已检查标记更新商品的已选择标记
                    merch.checked = store.checked;
                    return merch;
                });
            }
            return store;
        });
        this.setData({
            cartEffectList: updatedCart
        });
        this.grand();
    },

    /**
     * 单个选择
     * @param {*} e 
     */
    checkSingle(e) {
        let storeName = e.currentTarget.dataset.store;
        let merchandiseData = e.currentTarget.dataset.merchandise;
        let cartEffectList = this.data.cartEffectList;
    
        let updatedCart = cartEffectList.map(store => {
            if (store.store === storeName) {
                store.merchandises = store.merchandises.map(merch => {
                    if (merch.id === merchandiseData.id) {
                        // 更改商品的选择状态
                        merch.checked = !merch.checked;
                        // 更改店铺的选中状态
                        store.merchandiseChecked = merch.checked;
                    }
                    return merch;
                });
            }
            return store;
        });
    
        this.setData({
            cartEffectList: updatedCart
        });
        this.grand();
    },

    /**
     * 减少数量,下限为1
     * @param {*} e 
     */
    minusAmount(e) {
        let storeName = e.currentTarget.dataset.store;
        let id = e.currentTarget.dataset.id;
        let cartEffectList = this.data.cartEffectList;
        let updatedCart = cartEffectList.map(store => {
            if (store.store === storeName) {
                // 更新商店的已选择商品标记
                store.merchandiseChecked = true;
                store.merchandises = store.merchandises.map(merch => {
                    if (merch.id === id) {
                        // 更新商品的选择状态
                        merch.checked = true;
                        if (merch.amount > 1) {
                            merch.amount--;
                        } else {
                            wx.showModal({
                                content: '宝贝数量不能再减少了',
                                showCancel: false,
                            });
                        }
                    }
                    return merch;
                });
            }
            return store;
        });
    
        this.setData({
            cartEffectList: updatedCart
        });
        this.grand();
    },

    /**
     * 增加数量,上限为99
     * @param {*} e 
     */
    plusAmount(e) {
        let storeName = e.currentTarget.dataset.store;
        let id = e.currentTarget.dataset.id;
        let cartEffectList = this.data.cartEffectList;
        let updatedCart = cartEffectList.map(store => {
            if (store.store === storeName) {
                // 更新店铺的选中状态
                store.merchandiseChecked = true;
                store.merchandises = store.merchandises.map(merch => {
                    if (merch.id === id) {
                        // 更新商品的选择状态
                        merch.checked = true; 
                        if (merch.amount < 99) {
                            merch.amount++;
                        } else {
                            wx.showModal({
                                content: '宝贝数量不能再增加了',
                                showCancel: false,
                            });
                        }
                    }
                    return merch;
                });
            }
            return store;
        });
        this.setData({
            cartEffectList: updatedCart
        });
        this.grand();
    },

    /**
     * 删除商品
     * @param {*} e 
     */
    deleteMerchandise(e) {
        let storeName = e.currentTarget.dataset.store;
        let id = e.currentTarget.dataset.id;
        let cartEffectList = this.data.cartEffectList;
        let cartLapseList = this.data.cartLapseList;
        // 遍历每个商店
        let updatedEffectCart = cartEffectList.map(store => {
            if (store.store === storeName) {
                // 如果是目标商店,过滤掉指定ID的商品
                store.merchandises = store.merchandises.filter(merch => merch.id !== id);
            }
            return store;
        });
        cartLapseList = cartLapseList.filter(item => item.id !== id);
        this.setData({
            cartEffectList: updatedEffectCart,
            cartLapseList: cartLapseList
        });
    },

    /**
     * 全选
     */
    checkAll() {
        let cartEffectList = this.data.cartEffectList;
        let checkedAll = this.data.checkedAll;
        // 使用map方法更新每个商店和商品的选中状态
        let updatedCart = cartEffectList.map(store => {
            return {
                ...store,
                checked: !checkedAll,
                merchandiseChecked: !checkedAll,
                merchandises: store.merchandises.map(merch => ({
                    ...merch,
                    checked: !checkedAll
                }))
            };
        });
        this.setData({
            cartEffectList: updatedCart,
            checkedAll: !checkedAll,
        });
        this.grand();
    },

    /**
     * 合计
     */
    grand() {
        let cartEffectList = this.data.cartEffectList;
        // 初始化总价和总数量
        let total = 0;
        let totalCount = 0;
        cartEffectList.forEach(store => {
            store.merchandises.forEach(merch => {
                if (merch.checked) {
                    total += merch.amount * merch.price;
                    totalCount += merch.amount;
                }
            });
        });
        this.setData({
            total: total,
            totalCount: totalCount,
        });
    },

    /**
     * 结算
     */
    settleBill() {
        this.setData({
            showLoading: true,
        });
        // 进行深拷贝
        let cartEffectList = JSON.parse(JSON.stringify(this.data.cartEffectList));

        // 筛选出未选中的商店或没有选中商品的商店
        let filteredStores = cartEffectList.filter(store => {
            if (store.checked) {
                // 如果商店被选中,保留它
                return true; 
            }
            // 从商店中筛选出未选中的商品
            store.merchandises = store.merchandises.filter(merch => merch.checked);
            // 如果筛选后,商店有选中的商品,保留该商店
            return store.merchandises.length > 0;
        });

        wx.navigateTo({
            url: `/pages/index/settle-bill/settle-bill?chosenList=${JSON.stringify(filteredStores)}`,
            complete: () => {
                this.setData({
                    showLoading: false,
                });
            }
        });
    },

    /**
     * 开始滑动
     * @param {*} e 
     */
    touchStart (e) {
        let index = e.currentTarget.dataset.id;
        let store = e.currentTarget.dataset.store;
        let cartEffectList = this.data.cartEffectList;
        let cartLapseList = this.data.cartLapseList;
        // 复位,这样子就能保证一次显示一个删除按钮
        for (let i in cartEffectList) {
            for (let j in cartEffectList[i].merchandises) {
                cartEffectList[i].merchandises[j].x = 0
            }
        }
        for (let i in cartLapseList) {
            cartLapseList[i].x = 0;
        }
        // 判断是否为多触点
        if (e.touches.length == 1) {
            // 记录开始触摸的位置
            this.setData({
                startX: e.touches[0].clientX,
                cartEffectList: cartEffectList,
                cartLapseList: cartLapseList
            });
        }
    },
    /**
     * 开始移动
     * @param {*} e 
     */
    touchMove (e) {
        let id = e.currentTarget.dataset.id;
        let store = e.currentTarget.dataset.store;
        if (e.touches.length == 1) {
            // 记录移动的距离
            let disX = e.touches[0].clientX - this.data.startX;
            // 大于0则时向右滑,复位
            if (disX >= 0) {
                // 向右滑
                this.setData({
                    isLeft: 0
                });
            } else {
                // 小于则是向左滑
                this.setData({
                    isLeft: 1
                });
            }
        }
    },
    /**
     * 滑动终点
     * @param {*} e 
     */
    touchEnd (e) {
        let id = e.currentTarget.dataset.id;
        let store = e.currentTarget.dataset.store;
        let delw = this.data.delBtnW;
        if (e.touches.length == 1) {
            let endX = e.touches[0].clientX - this.data.startX;
            if (endX < 0) {
                this.setXmove(id, store, - delw);
            } else {
                this.setXmove(id, store, 0);
            }
        }
    },
    /**
     * 滑动事件
     * @param {*} e 
     */
    touchChange (e) {
        let delw = this.data.delBtnW;
        let store = e.currentTarget.dataset.store;
        let id = e.currentTarget.dataset.id;
        if (this.data.isLeft) {
            if (e.detail.source == 'friction') {
                if (e.detail.x < 0) {
                    this.setXmove(id, store, -delw);
                } else {
                    this.setXmove(id, store, 0);
                }
            }
        } else {
            if (e.detail.source == 'friction') {
                this.setXmove(id, store, 0);
            }
        }
    },
    /**
     * 设置起始位置
     * @param {*} id 
     * @param {*} store 
     * @param {*} x 
     */
    setXmove(id, store, x) {
        let that = this;
        if (store) {
            let cartEffectList = this.data.cartEffectList;
            for (let i in cartEffectList) {
                if (cartEffectList[i].store == store) {
                    for (let j in cartEffectList[i].merchandises) {
                        if (cartEffectList[i].merchandises[j].id == id) {
                            cartEffectList[i].merchandises[j].x = x;
                        }
                    }
                }
            }
            that.setData({
                cartEffectList: cartEffectList,
            });
        } else {
            let cartLapseList = this.data.cartLapseList;
            for (let i in cartLapseList) {
                if (cartLapseList[i].id == id) {
                    cartLapseList[i].x = x;
                }
            }
            that.setData({
                cartLapseList: cartLapseList,
            });
        }
    }

})

checkbox样式更改

checkbox .wx-checkbox-input{
    width: 40rpx; 
    height: 40rpx; 
    border-radius: 50%;
}

/* 选中后的背景样式 */
checkbox .wx-checkbox-input.wx-checkbox-input-checked{
    background: #b0474c;
}

/* 选中后的勾子样式 */
checkbox .wx-checkbox-input.wx-checkbox-input-checked::before{
    width: 40rpx;
    height: 40rpx;
    line-height: 40rpx;
    border-radius: 50%;
    text-align: center;
    font-size:32rpx; 
    color:#fbfbfd; 
    background: transparent;
    transform:translate(-50%, -50%) scale(1);
    -webkit-transform:translate(-50%, -50%) scale(1);
}

到了这里,关于【微信小程序】 实现购物车的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序 - 商城项目 - 购物车

      引入 WeUI:

    2023年04月22日
    浏览(33)
  • 微信小程序商城开发-商品详情页跳转购物车

    微信小程序商城开发中商品详情页中购物车页面的跳转(仅限于tabbar页面的跳转)   点击商品详情页中的购物车图标跳转不到tabbar及导航栏中的购物车页面,总是报错 微信小程序开发需要跳转tabbar页面的话,有固定的跳转方式,不是开发中所有的跳转方式都适用于tabbar跳转

    2024年02月17日
    浏览(40)
  • 【微信小程序】-- uni-app 项目-- 购物车 -- 首页 - 轮播图效果(五十二)

    💌 所属专栏:【微信小程序开发教程】 😀 作  者:我是夜阑的狗🐶 🚀 个人简介:一个正在努力学技术的CV工程师,专注基础和实战分享 ,欢迎咨询! 💖 欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信 😘 😘 😘   大家好,又见面了,

    2023年04月18日
    浏览(42)
  • 【JavaScript】实现简易购物车

    💻【JavaScript】实现简易购物车 🏠专栏:有趣实用案例 👀个人主页:繁星学编程🍁 🧑个人简介:一个不断提高自我的平凡人🚀 🔊分享方向:目前主攻前端,其他知识也会阶段性分享🍀 👊格言:☀️没有走不通的路,只有不敢走的人!☀️ 👉让我们一起进步,一起成

    2024年02月04日
    浏览(40)
  • 【学习笔记46】JavaScript购物车的实现

    1、将通过数据重构页面 查询数据, 渲染页面 2、全选 选中全选按钮后, 根据全选按钮的选中状态, 修改所有商品的选中状态 重新渲染视图 3、清空购物车 清空商品数据 重新渲染视图 4、结算 找到所有选中的商品 计算所有选中商品各自的总价 计算所有选中商品的总价之和 5、

    2024年02月08日
    浏览(32)
  • HTML+CSS+JavaScript实现网络购物车

    1 页面布局 购物车由一个table标签和一个div标签构成。案例在Chrome浏览器运行效果,如图所示。 table标签共有5行6列,第1行是表头,第2-5行的每一行都代表一个商品,依次用td标签存放商品的勾选框、商品缩略图及名称、商品单价、商品增减操作按钮以及小计、删除按钮等。

    2024年02月05日
    浏览(36)
  • 使用JavaScript实现动态生成并管理购物车的深入解析

    在当前的互联网时代,电子商务已成为我们日常生活的重要组成部分。购物车作为电子商务网站的核心功能之一,其实现方式对于用户体验至关重要。本文将深入探讨如何使用JavaScript实现一个动态生成并管理购物车的功能,并详细介绍其实现细节,同时附上相关代码。 购物

    2024年01月24日
    浏览(70)
  • 微信电商小程序购买/加入购物车组件设计

    作为一名常常摆烂,一蹶不振的大学生,最近接到了开发电商小程序的小任务,既然是电商,总得有购买加车功能吧?经过n个坤年的拜读微信小程序开发者文档还有别的大佬的指点,奉上我自己的理解,欢迎各位大佬指点改正,学习交流,共同进步。该文章适合微信小程序初

    2024年02月09日
    浏览(34)
  • 购物车程序实现教程

    在本教程中,我们将实现一个购物车程序,实现在界面中以列表的形式显示购物车的商品信息。商品信息包含商品名称,价格和数量,并能实现对应的增删改查操作。我们将使用 Android Studio 和 SQLite 数据库来完成这个任务。 我们的购物车程序由以下四个主要类组成: MainAct

    2024年02月04日
    浏览(33)
  • 使用JavaScript和Vue.js框架开发的电子商务网站,实现商品展示和购物车功能

    引言: 随着互联网的快速发展和智能手机的普及,电子商务行业正迎来一个全新的时代。越来越多的消费者选择网上购物,而不再局限于传统的实体店。这种趋势不仅仅是改变了消费者的习惯购物,也给企业带来了巨大的商机。为了不断满足消费者的需求,电子商务网站需要

    2024年02月15日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包