编写一套小程序商城的代码
时间: 2023-05-23 12:07:37 浏览: 630
A:以下是一套简单的小程序商城代码:
HTML部分:
```html
<view class="container">
<view class="goods-list">
<view class="goods" wx:for="{{goodsList}}" wx:key="{{item.id}}" bindtap="onGoodsTap">
<image class="img" src="{{item.imgUrl}}"></image>
<view class="name">{{item.name}}</view>
<view class="price">{{item.price}}</view>
</view>
</view>
<view class="footer">
<view class="shopping-cart" bindtap="onCartTap">
<image class="icon" src="/images/cart.png"></image>
<view class="count">{{cartCount}}</view>
</view>
<view class="buy-btn" bindtap="onBuyTap">
<view class="text">立即购买</view>
</view>
</view>
</view>
```
CSS部分:
```css
.container {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
height: 100%;
}
.goods-list {
flex: 1;
overflow-y: scroll;
width: 100%;
padding: 20rpx;
}
.goods {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 380rpx;
border-radius: 10rpx;
box-shadow: 0 0 10rpx 0 rgba(23, 23, 23, 0.1);
margin-bottom: 20rpx;
}
.img {
height: 280rpx;
width: 280rpx;
border-radius: 10rpx;
margin-bottom: 20rpx;
}
.name {
font-size: 30rpx;
color: #333;
margin-bottom: 10rpx;
}
.price {
font-size: 24rpx;
color: #f00;
}
.footer {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
height: 100rpx;
background-color: #fff;
box-shadow: 0 -2rpx 6rpx 0 rgba(23, 23, 23, 0.1);
position: fixed;
bottom: 0;
left: 0;
}
.shopping-cart {
display: flex;
justify-content: center;
align-items: center;
position: relative;
width: 140rpx;
height: 100%;
margin-right: 20rpx;
}
.icon {
height: 40rpx;
width: 40rpx;
}
.count {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0;
right: 0;
height: 30rpx;
width: 30rpx;
background-color: #f00;
border-radius: 15rpx;
color: #fff;
font-size: 18rpx;
}
.buy-btn {
display: flex;
justify-content: center;
align-items: center;
width: 280rpx;
height: 80rpx;
background-color: #f00;
border-radius: 40rpx;
}
.text {
color: #fff;
font-size: 28rpx;
}
```
JavaScript部分:
```JavaScript
// 初始化数据
const goodsList = [{
id: 1,
imgUrl: '/images/goods-1.jpg',
name: '商品1',
price: 99.00
}, {
id: 2,
imgUrl: '/images/goods-2.jpg',
name: '商品2',
price: 199.00
}, {
id: 3,
imgUrl: '/images/goods-3.jpg',
name: '商品3',
price: 299.00
}, {
id: 4,
imgUrl: '/images/goods-4.jpg',
name: '商品4',
price: 399.00
}, {
id: 5,
imgUrl: '/images/goods-5.jpg',
name: '商品5',
price: 499.00
}]
const cart = []
const cartCount = 0
Page({
data: {
goodsList: goodsList,
cartCount: cartCount
},
onGoodsTap: function(e) {
const goodsId = e.currentTarget.dataset.id
wx.navigateTo({
url: '/pages/goods-detail/goods-detail?id=' + goodsId
})
},
onCartTap: function() {
wx.navigateTo({
url: '/pages/shopping-cart/shopping-cart'
})
},
onBuyTap: function() {
if (cart.length === 0) {
wx.showToast({
title: '购物车没有商品',
icon: 'none'
})
} else {
wx.navigateTo({
url: '/pages/checkout/checkout'
})
}
}
})
```
以上代码包含了一个商品列表页面,可以点击商品进入商品详情页,也可以点击底部购物车按钮查看购物车列表,还可以点击底部立即购买按钮,跳转到结算页面。购物车和结算页面的代码还需根据需求开发。
阅读全文