微信小程序商城开发之商城首页轮播图、商品分类导航以及新品特卖的实现代码
本篇文章给大家带来的内容是关于微信小程序商城开发之商城首页轮播图、商品分类导航以及新品特卖的实现代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
上一篇我们设置了小程序后台服务器https域名设置、搭建了小程序基础的https框架和实现了首页导航模块,今天我们一起来看一下电商首页轮播、分类导航和新品特卖模块的实现。
实现功能模块
主要实现2、3、4,用到的API数据服务如下图所示:
首页轮播模块实现
home.js
<!--首页轮播%20banner%20--> %20%20%20%20<swiper%20indicator-dots="{{indicatorDots}}"%20autoplay="{{autoplay}}"%20interval="{{interval}}"%20duration="{{duration}}"> %20%20%20%20<block%20wx:for="{{banners}}"> %20%20%20%20%20%20<swiper-item> %20%20%20%20%20%20%20%20<image%20src="{{item.imgUrl}}"%20mode="widthFix"/> %20%20%20%20%20%20</swiper-item> %20%20%20%20</block> %20%20</swiper>
home.wxss
/*%20直接设置swiper属性%20*/ swiper%20{%20%20 %20%20%20%20height:%20300rpx; } swiper-item%20image%20{%20%20 %20%20%20%20width:%20100%;%20%20height:%20100%; }
home.js
页面初始化轮播数据
data:%20{ %20%20%20%20navbars:null,//接上篇导航初始化数据 %20%20%20%20currentTab:%200,//接上篇导航初始化数据 %20%20%20%20banners:null, %20%20%20%20indicatorDots:%20true,%20//是否显示面板指示点 %20%20%20%20autoplay:%20true,%20//是否自动切换 %20%20%20%20interval:%203000,%20//自动切换时间间隔,3s %20%20%20%20duration:%201000,%20//%20%20滑动动画时长1s %20%20},
页面初始化加载轮播数据函数
/** %20%20%20*%20生命周期函数--监听页面加载 %20%20%20*/ onLoad:%20function%20(options)%20{%20%20%20%20 %20%20%20%20var%20that%20=%20this;%20%20%20%20//加载navbar导航条,接上篇导航数据 %20%20%20%20that.navbarShow();%20%20%20//加载banner轮播 %20%20%20%20that.bannerShow(); %20%20},
ajax获取轮播数据
bannerShow:%20function%20(success)%20{%20%20%20%20 %20%20%20%20var%20that%20=%20this; %20%20%20%20ajax.request({%20%20%20%20%20%20 %20%20%20%20%20%20%20%20method:%20'GET',%20%20%20%20%20%20 %20%20%20%20%20%20%20%20url:%20'home/banners?key='%20+%20utils.key,%20%20%20%20%20%20 %20%20%20%20%20%20%20%20success:%20data%20=>%20{ %20%20%20%20%20%20%20%20that.setData({%20%20%20%20%20%20%20%20%20%20 %20%20%20%20%20%20%20%20%20%20%20%20banners:%20data.result %20%20%20%20%20%20%20%20})%20%20%20%20%20%20%20%20 %20%20%20%20%20%20%20%20console.log(data.result) %20%20%20%20%20%20} %20%20%20%20}) %20%20},
首页分类导航实现
home.js
<!--%20分类导航%20--> %20<view> %20%20<view%20class="navy"> %20%20%20%20<block%20wx:for-items="{{menus}}"%20wx:key="name"> %20%20%20%20%20%20<view%20class="nav-item"%20%20data-type="{{item.menuName}}"%20data-typeid="{{item.id}}"> %20%20%20%20%20%20%20%20<image%20src="{{item.imgUrl}}"%20class="nav-image"%20/> %20%20%20%20%20%20%20%20<text>{{item.menuName}}</text> %20%20%20%20%20%20</view> %20%20%20%20</block> %20%20</view> %20</view>
home.wxss
/*=================分类导航====================*/ .navs%20{%20%20 %20%20%20%20display:%20flex;%20 %20%20%20%20justify-content:%20left;%20 %20%20%20%20flex-direction:%20row;%20%20 %20%20%20%20flex-wrap:%20wrap;%20%20 } .nav-item%20{%20%20 %20%20%20%20width:%2025%;%20%20 %20%20%20%20display:%20flex;%20%20 %20%20%20%20align-items:%20center;%20%20 %20%20%20%20flex-direction:%20column;%20 %20%20%20%20/*%20padding:%2020rpx;%20*/ %20%20%20%20padding-top:%2020rpx; } .nav-item%20.nav-image%20{%20%20 %20%20%20%20width:%2080rpx;%20%20 %20%20%20%20height:%2080rpx;%20%20/*%20border-radius:%2050%;设置边界圆角%20*/ } .nav-item%20text%20{%20%20 %20%20%20%20padding-top:%2020rpx;%20%20 %20%20%20%20font-size:%2025rpx; }
home.js
页面初始化分类导航数据
data:%20{ %20%20%20%20navbars:null,//导航数据 %20%20%20%20currentTab:%200, %20%20%20%20banners:null,%20//轮播数据 %20%20%20%20indicatorDots:%20true,%20//是否显示面板指示点 %20%20%20%20autoplay:%20true,%20//是否自动切换 %20%20%20%20interval:%203000,%20//自动切换时间间隔,3s %20%20%20%20duration:%201000,%20//%20%20滑动动画时长1s %20%20%20%20menus:%20null,%20//分类导航数据},
页面初始化加载分类导航数据函数
/** %20%20%20*%20生命周期函数--监听页面加载 %20%20%20*/ %20%20onLoad:%20function%20(options)%20{%20%20%20%20 %20%20%20%20var%20that%20=%20this;%20%20%20%20//加载navbar导航条 %20%20%20%20that.navbarShow();%20%20%20%20//加载banner轮播 %20%20%20%20that.bannerShow();%20%20%20%20//加载menu分类导航菜单 %20%20%20%20that.menuShow(); %20%20},
ajax获取分类导航数据
menuShow:%20function%20(success)%20{%20%20%20%20 %20%20%20%20var%20that%20=%20this; %20%20%20%20ajax.request({%20%20%20%20%20%20 %20%20%20%20method:%20'GET',%20%20%20%20%20%20 %20%20%20%20url:%20'home/menus?key='+%20utils.key,%20%20%20%20%20%20 %20%20%20%20success:%20data%20=>%20{ %20%20%20%20%20%20%20%20that.setData({%20%20%20%20%20%20%20%20%20%20 %20%20%20%20%20%20%20%20%20%20%20%20menus:%20data.result %20%20%20%20%20%20%20%20})%20%20%20%20%20%20%20%20 %20%20%20%20%20%20%20%20console.log(data.result) %20%20%20%20%20%20} %20%20%20%20}) %20%20},
首页新品特卖模块实现
home.js
<view%20class="separate"></view> <view%20class="cate-container">%20 %20%20%20%20<view%20class="category-title"> %20%20%20%20%20%20<text%20class="name">新品特卖</text> %20%20%20%20%20%20<view%20class="line_flag"></view> %20%20%20%20%20%20<block%20wx:for-items="{{brands}}"%20wx:key="id"> %20%20%20%20%20%20%20%20<navigator%20url="/pages/detail/detail"> %20%20%20%20%20%20%20%20<image%20class="head-img"%20src="{{item.imgUrl}}"%20mode="widthFix"></image> %20%20%20%20%20%20%20%20</navigator> %20%20%20%20%20%20%20%20<text%20class="brand-name">{{item.name}}</text>%20 %20%20%20%20%20%20%20%20<view%20class='pas'> %20%20%20%20%20%20%20%20<image%20class="activity-logo"%20src="../../images/activity_logo.png" mode="widthFix"></image> {{item.remark}} </view> </block> </view> </view>
home.wxss
/*=================新品特卖 样式====================*/ .category-title { display: flex; flex-direction: column; margin-top: 20rpx; margin-bottom: 0rpx; padding: 0px 10px; } .category-title .title{ font-size: 14px; font-weight:900; } .category-title .line_name{ font-size: 10px; color: #98989f; display: flex; justify-content:space-between; } /* 分割线 */ .separate { height: 15rpx; background-color: #f2f2f2; } .category-title { display: flex; flex-direction: column; margin-top: 25rpx; margin-bottom: 0rpx; } .category-title .name { font-size: 40rpx; text-align: center; margin: 10rpx auto; } .line_flag { width: 80rpx; height: 1rpx; display: inline-block; margin: 20rpx auto; background-color: gainsboro; text-align: center; } .line { width: 100%; height: 2rpx; display: inline-block; margin: 20rpx 0rpx; background-color: gainsboro; text-align: center; } .head-img { width: 100%; } .brand-name{ font-weight: 600; font-size: 32rpx; } .activity-logo { width:35rpx; height:35rpx; margin-right: 10rpx; /* position: absolute; */ } .pms{ font-size: 28rpx; margin-bottom: 20rpx; display: flex; justify-content: left; flex-direction: row; color: #5771a8; }
home.js
页面初始化新品特卖数据
data: { navbars:null,//导航数据 currentTab: 0, banners:null, //轮播数据 indicatorDots: true, //是否显示面板指示点 autoplay: true, //是否自动切换 interval: 3000, //自动切换时间间隔,3s duration: 1000, // 滑动动画时长1s menus: null, //分类导航数据 brands: null, //新品特卖数据},
页面初始化加载新品特卖数据函数
/** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var that = this; //加载navbar导航条 that.navbarShow(); //加载banner轮播 that.bannerShow(); //加载menu分类导航菜单 that.menuShow(); //加载新品特卖 that.brandShow(); },
ajax获取新品特卖数据
brandShow: function (success) { var that = this; ajax.request({ method: 'GET', url: 'activity/brands? key='+utils.key+'&type=temai&page=1&size=5', success: data => { that.setData({ brands: data.result.list }) console.log("brands:" + data.result.list) } }) },
实现效果预览
备注:本文是为了更好的让大家能够有模块化的思维来实现改电商案例,后续依然会采用这种方式,因为更贴近与实际工作场景,也让自己的编码更加的规范增加可阅读性。
相关推荐:
微信小程序商城开发之https框架的搭建以及顶部和底部导航的实现
微信小程序实例:轮播图的代码实现与分析
以上就是微信小程序商城开发之商城首页轮播图、商品分类导航以及新品特卖的实现代码的详细内容,更多请关注其它相关文章!