⼩程序外卖订单界⾯的⽰例代码1.效果界⾯
2.涉及功能
*左侧商品类型、右侧商品可以相互控制;
*商品列表加减及购物车商品加减icon消失、显⽰;
*商品每⼀次加减,页⾯视图变化(数量、价格变化、购物车置灰);
3.贴上所有代码
1.wxml
<view class="container">
<view class="index-cont">
<!-- 左边类型 -->
<view class="index-left">
<view wx:for="{{foodsList}}" wx:key="index" class="item {{curId === 'item'+index?'on':''}}" data-id="item{{index}}" bindtap="scrollToViewFn">{{item.name}}</view>  </view>
<!-- 右边产品 -->
<scroll-view class="index-right" scroll-y="{{true}}" scroll-into-view="{{initView}}" scroll-with-animation="true" bindscroll="onPageScroll">
<view class="boxs">
<block wx:for="{{foodsList}}" wx:key="index">
<view class="index-title" id="item{{index}}">{{item.name}}</view>
<view class="item" wx:for="{{item.list}}" wx:key="ind" wx:for-item="itm" wx:for-index="ind" bindtap="showGoodDetail(itm)">
<view class="pic"><image src="{{itm.pic}}" mode="aspectFill"></image></view>
<view class="main">
<view class="tit">{{itm.title}}</view>
<view class="desc">{{itm.info}}</view>
<view class="money">¥{{itm.price}}</view>
</view>
<view class="box">
<view wx:if="{{itm.num !== 0}}" class="icon" catchtap="reduceNum(index, ind, itm)"><image src="../../../static/images/reduce-icon.png" alt=""></image></view>        <input wx:if="{{itm.num !== 0}}" type="text" disabled wx:model="{{itm.num}}"/>
<view class="icon" catchtap="addNum(index, ind, itm)"><image src="../../../static/images/add-icon.png" alt=""></image></view>
</view>
</view>
</block>
</view>
</scroll-view>
</view>
<view class="index-cart">
<view class="left">
<view class="cart-num" wx:if="{{cartList.length === 0}}">
<image src="../../../static/images/cart.png"></image>
</view>
<view class="cart-num on" wx:else bindtap="showCartMask">
<image src="../../../static/images/cart.png"></image>
<text>{{totalNum}}</text>
</view>
<view class="cart-money">¥{{totalMoney}}</view>
</view>
<view class="order-btn" bindtap="submitOrder">去结算</view>
</view>
<!--购物车弹窗-->
<view class="dialog" wx:if="{{isShowCartMask && cartList.length !== 0}}" bindtap="hiddenCartMak()">
<view class="boxs" catchtap="stopMaopao()">
<view class="title-block">
<text>已选商品</text>
<view class="clear" bindtap="clearCart"><image src="../../../static/images/del.png"></image>清空</view>
</view>
<scroll-view class="content" scroll-y="{{true}}" scroll-with-animation="true">
<block wx:for="{{cartList}}" wx:key="index">
<view class="item" id="{{item.view}}">
<view class="tit">{{item.name}}</view>
<view class="right">
<text>¥{{item.price}}</text>
<view class="box">
<view class="icon" bindtap="reduceCart(index, item)"><image src="../../../static/images/reduce-icon.png" alt=""></image></view>
<input type="text" disabled wx:model="{{item.num}}"/>
<view class="icon" bindtap="addCart(index, item)"><image src="../../../static/images/add-icon.png" alt=""></image></view>
</view>
</view>
</view>
</block>
</scroll-view>
</view>
</view>
<!--商品详情弹窗-->
<view class="dialog1" wx:if="{{isShowDetail}}">
<scroll-view class="detbox" scroll-y="{{true}}" scroll-with-animation="true">
<image class="img" src="{{goodDetail.pic}}" mode="aspectFit"></image>
<view class="box">
<view class="tit">{{goodDetail.title}}</view>
<view class="money">¥{{goodDetail.price}}</view>
<view class="desc">{{goodDetail.info}}</view>
</view>
<view class="close" bindtap="hideDetail"><image src="../../../static/images/close_ico.png"></image></view>
</scroll-view>
</view>
</view>
2.script
createPage({
data: {
foodsList: [], // 商品数据
cartList: [], // 购物车数据
isShowCartMask: false,
totalNum: 0,
totalMoney: 0,
initView: 'item0', // 根据此变量的变化,控制左侧选中状态、右侧滑动  curId: 'item0',
isShowDetail: false,
goodDetail: {},
screenWidth: 0, // ⼿机屏幕宽度
heightArray: [0] // 右侧每⼀个类型的⾼度区间数组
},
onLoad() {
},
methods: {
async getGoodsData() {
const that = this
const res = await getGoodsInfo({})
this.foodsList = res
success: (ress) => {
that.screenWidth = ress.windowWidth
}
})
},
// 设置⾼度区间所有单位转化为rpx
getHeightSection() {
const that = this
let hg = 0
for (let index = 0; index < that.foodsList.length - 1; index++) {
hg += 70 + that.foodsList[index].list.length * 212
that.heightArray.push(hg)
}
},
// 获取⾼度区间的下标
getHeightIndex(arr, hg) {
const that = this
arr.forEach((item, index) => {
if (hg >= item) {
javascript淘宝购物车代码
that.setData({
curId: 'item' + index
})
}
})
},
// 左边菜单控制右边
scrollToViewFn(e) {
this.setData({
initView: e.target.dataset.id,
curId: e.target.dataset.id
})
},
// 右边滚动控制左边
onPageScroll(e) {
const that = this
let scrollTop = e.detail.scrollTop * 750 / that.screenWidth
},
// 商品列表的减号点击
reduceNum(index, ind, item) {
const that = this
let val = 'foodsList[' + index + '].list[' + ind + '].num'
this.setData({
[val]: item.num - 1
})
// 如果商品为0,就把当前商品在购物车清除
// 如果不为0,就将当前商品数量减1
if (that.foodsList[index].list[ind].num === 0) {
} else {
that.cartList.forEach((itm, i) => {
if (itm.id === item.id) {
let value = 'cartList[' + i + '].num'
that.setData({
[value]: itm.num - 1
})
}
})
}
thisputed()
},
// 商品列表的加号点击
addNum(index, ind, item) {
const that = this
let val = 'foodsList[' + index + '].list[' + ind + '].num'
this.setData({
[val]: item.num + 1
})
// 如果商品为1,就把当前商品加⼊购物车
// 否则,就将当前商品数量加1
if (that.foodsList[index].list[ind].num === 1) {
let val = { id: item.id, name: item.title, price: item.price, num: 1, index: index, ind: ind, pic: item.pic }    that.cartList.push(val)
} else {
that.cartList.forEach((itm, i) => {
if (itm.id === item.id) {
let value = 'cartList[' + i + '].num'
that.setData({
[value]: itm.num + 1
})
}
})
}
thisputed()
},
// 购物车的减号点击
reduceCart(index, item) {
const that = this
let val = 'foodsList[' + item.index + '].list[' + item.ind + '].num'
let val1 = 'cartList[' + index + '].num'
this.setData({
[val]: item.num - 1,
[val1]: item.num - 1
})
// 如果商品为0,就把当前商品在购物车清除
// 如果不为0,就将当前商品数量减1
if (that.cartList[index].num === 0) {
}
thisputed()
},
// 购物车的加号点击
addCart(index, item) {
const that = this
let val = 'cartList[' + index + '].num'
that.setData({
[val]: item.num + 1
})
thisputed()
},
// 清空购物车
clearCart() {
const that = this
wx.showModal({
title: '提⽰',
content: '清空购物车?',
success: function (res) {
if (firm) {
that.setData({
cartList: []
})
that.foodsList.forEach((item, i) => {
item.list.forEach((itm, j) => {
let value = 'foodsList[' + i + '].list[' + j + '].num'
that.setData({
[value]: 0
})
})
})
thatputed()
}
}
})
},
// 计算选择商品总价格和总数量
computed() {
const that = this
let num = 0
let money = 0
that.cartList.forEach(item => {
num += item.num
money += parseFloat(item.price) * item.num
})
that.setData({
totalNum: num,
totalMoney: money

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。