微信小程序实现购物页面左右联动
网络编程 2021-07-04 15:50www.168986.cn编程入门
这篇文章主要为大家详细介绍了微信小程序实现购物页面左右联动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了微信小程序实现购物页面左右联动的具体代码,供大家参考,具体内容如下
效果图
wxml
<view class="pro-container"> <scroll-view class="left-menu" scroll-y scroll-with-animation="true" scroll-="{{leftMenuTop}}"> <view class="menu-item {{index===currentActiveIndex?'menu-active':''}}" wx:for="{{item}}" wx:key="{{item.index}}" id="{{index}}" catchtap='changeMenu' >{{item.typename}}</view> </scroll-view> <scroll-view v-if="item!=''" class="right-pro" bindscroll = "scroll" scroll-y scroll-with-animation="true" scroll-="{{rightProTop}}"> <view class="pro-item" wx:for="{{item}}"> <view class="item-header">{{item.typename}}</view> <view class="pro-item-container"> <view wx:for="{{item.shop_goods}}" wx:key="{{index}}" class="pro-item-item"> <image lazy-load="true" src="{{item}}"></image> <text>{{item.goodname}}</text> </view> </view> </view> </scroll-view> </view>
wxss
.left-menu{ position: absolute; : 0; left: 0; bottom: 0; right: 0; width: 180rpx; background-color: #f8f8f8; font-size: 32rpx; } ::-webkit-scrollbar{ width: 0; height: 0; color: transparent; } .left-menu .menu-item{ width: calc(100% - 16rpx); height: 80rpx; padding:0 8rpx; line-height: 80rpx; text-align: center; } .left-menu .menu-item.menu-active{ border-left:8rpx solid red; padding-left: 0; background-color: #fff; } / 右边商品区域 / .right-pro{ position: absolute; left: 180rpx; :0; bottom: 0; width:calc(100% - 180rpx); background-color: #fff; display: flex; overflow: hidden; } .right-pro .pro-item{ padding:20rpx; } .right-pro .item-header{ display: inline-block; font-size: 30rpx; line-height: 40rpx; color: #fff; background-color: red; padding:0rpx 30rpx; margin: 10rpx auto; } .right-pro .pro-item-container{ font-size: 28rpx; } .pro-item-item{ width: calc((100% - 180rpx) / 2 ); display:inline-block; } .right-pro .pro-item-container image{ width: 100rpx; height: 100rpx; display: block; margin: 0 auto; } .right-pro .pro-item-container text{ display: block; text-align: center; }
wxjs
let proListToTop = [], menuToTop = [], MENU = 0, windowHeight,timeoutId; // MENU ==> 是否为点击左侧进行滚动的,如果是,则不需要设置左侧的激活状态 Page({ data: { proList: [], item: [], item2:[ { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, { "typename": "服饰", }, ], currentActiveIndex: 0 }, onLoad: function (options) { // ---------------------------ajax---------------------- var utilMd5 = require('../../utils/md5.js'); var md5 = utilMd5.hexMD5; var timestamp = new Date().getTime(); var that = this; var jsonStr = JSON.stringify({ "shopid": "34" }); var token = md5(jsonStr + timestamp) + timestamp; wx.request({ url: "https://api.jvjiewang./Home/Shop/goods", data: { jsonStr: jsonStr, token: token }, method: 'POST', header: { "Content-Type": "application/x--form-urlencoded" }, suess: function (res) { console.log(res.data.respond); var items = res.data.respond; that.setData({ item: items }) } }) //----------------------------ajax---------------------- // 确保页面数据已经刷新完毕~ setTimeout(() => { this.getAllRects() }, 200) }, changeMenu(e) { // 改变左侧tab栏操作 if (Number(e.target.id) === this.data.currentActiveIndex) return MENU = 1 this.setData({ currentActiveIndex: Number(e.target.id), rightProTop: proListToTop[Number(e.target.id)] }) this.setMenuAnimation(Number(e.target.id)) }, scroll(e) { for (let i = 0; i < proListToTop.length; i++) { if (e.detail.scrollTop < proListToTop[i] && i !== 0 && e.detail.scrollTop > proListToTop[i - 1]) { return this.setDis(i) } } // 找不到匹配项,默认显示第一个数据 if (!MENU) { this.setData({ currentActiveIndex: 0 }) } MENU = 0 }, setDis(i) { // 设置左侧menu栏的选中状态 if (i !== this.data.currentActiveIndex + 1 && !MENU) { this.setData({ currentActiveIndex: i - 1 }) } MENU = 0 this.setMenuAnimation(i) }, setMenuAnimation(i){ // 设置动画,使menu滚动到指定位置。 let self = this if (menuToTop[i]) { console.log(11111) // 节流操作 if(timeoutId){ clearTimeout(timeoutId) } timeoutId = setTimeout(()=>{ console.log(12138) self.setData({ leftMenuTop: (menuToTop[i]. - windowHeight) }) },50) } else { if (this.data.leftMenuTop === 0) return this.setData({ leftMenuTop: 0 }) } }, getActiveReacts(){ wx.createSelectorQuery().selectAll('.menu-active').boundingClientRect(function (rects) { return rects[0]. }).exec() }, getAllRects() { // 获取商品数组的位置信息 wx.createSelectorQuery().selectAll('.pro-item').boundingClientRect(function (rects) { rects.forEach(function (rect) { proListToTop.push(rect.) }) }).exec() // 获取menu数组的位置信息 wx.createSelectorQuery().selectAll('.menu-item').boundingClientRect(function (rects) { wx.getSystemInfo({ suess: function (res) { windowHeight = res.windowHeight / 2 rects.forEach(function (rect) { menuToTop.push({ : rect., // animate:rect. > windowHeight }) }) } }) }).exec() }, // 获取系统的高度信息 getSystemInfo() { let self = this wx.getSystemInfo({ suess: function (res) { windowHeight = res.windowHeight / 2 } }) } })
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
上一篇:微信小程序实现商品属性联动选择
下一篇:微信小程序基于picker实现级联菜单
编程语言
- 甘肃哪有关键词排名优化购买方式有哪些
- 甘肃SEO如何做网站优化
- 河南seo关键词优化怎么做电话营销
- 北京SEO优化如何做QQ群营销
- 来宾百度关键词排名:提升您网站曝光率的关键
- 卢龙关键词优化:提升您网站排名的策略与技巧
- 山东网站优化的注意事项有哪些
- 四川整站优化怎样提升在搜索引擎中的排名
- 疏附整站优化:提升网站性能与用户体验的全新
- 海南seo主要做什么工作售后服务要做到哪些
- 荣昌百度网站优化:提升您网站的搜索引擎排名
- 河北seo网站排名关键词优化如何做SEO
- 江西优化关键词排名推广售后保障一般有哪些
- 古浪SEO优化:提升你的网站可见性
- 西藏网站排名优化怎么把网站排名在百度首页
- 如何提升阳东百度快照排名:详尽指南