微信小程序?qū)崿F(xiàn)購物頁面左右聯(lián)動(dòng)
更新時(shí)間:2019年02月15日 11:36:24 作者:qq_38674970
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)購物頁面左右聯(lián)動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)購物頁面左右聯(lián)動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

wxml
<view class="pro-container">
<scroll-view class="left-menu" scroll-y scroll-with-animation="true" scroll-top="{{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-top="{{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;
top: 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;
}
/* 右邊商品區(qū)域 */
.right-pro{
position: absolute;
left: 180rpx;
top: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 ==> 是否為點(diǎn)擊左側(cè)進(jìn)行滾動(dòng)的,如果是,則不需要再次設(shè)置左側(cè)的激活狀態(tài)
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.com/Home/Shop/goods",
data: {
jsonStr: jsonStr,
token: token
},
method: 'POST',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(res.data.respond);
var items = res.data.respond;
that.setData({
item: items
})
}
})
//----------------------------ajax----------------------
// 確保頁面數(shù)據(jù)已經(jīng)刷新完畢~
setTimeout(() => {
this.getAllRects()
}, 200)
},
changeMenu(e) {
// 改變左側(cè)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)
}
}
// 找不到匹配項(xiàng),默認(rèn)顯示第一個(gè)數(shù)據(jù)
if (!MENU) {
this.setData({
currentActiveIndex: 0
})
}
MENU = 0
},
setDis(i) {
// 設(shè)置左側(cè)menu欄的選中狀態(tài)
if (i !== this.data.currentActiveIndex + 1 && !MENU) {
this.setData({
currentActiveIndex: i - 1
})
}
MENU = 0
this.setMenuAnimation(i)
},
setMenuAnimation(i){
// 設(shè)置動(dòng)畫,使menu滾動(dòng)到指定位置。
let self = this
if (menuToTop[i]) {
console.log(11111)
// 節(jié)流操作
if(timeoutId){
clearTimeout(timeoutId)
}
timeoutId = setTimeout(()=>{
console.log(12138)
self.setData({
leftMenuTop: (menuToTop[i].top - 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].top
}).exec()
},
getAllRects() {
// 獲取商品數(shù)組的位置信息
wx.createSelectorQuery().selectAll('.pro-item').boundingClientRect(function (rects) {
rects.forEach(function (rect) {
proListToTop.push(rect.top)
})
}).exec()
// 獲取menu數(shù)組的位置信息
wx.createSelectorQuery().selectAll('.menu-item').boundingClientRect(function (rects) {
wx.getSystemInfo({
success: function (res) {
windowHeight = res.windowHeight / 2
rects.forEach(function (rect) {
menuToTop.push({
top: rect.top,
// animate:rect.top > windowHeight
})
})
}
})
}).exec()
},
// 獲取系統(tǒng)的高度信息
getSystemInfo() {
let self = this
wx.getSystemInfo({
success: function (res) {
windowHeight = res.windowHeight / 2
}
})
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 微信小程序?qū)崿F(xiàn)聯(lián)動(dòng)菜單
- 微信小程序?qū)崿F(xiàn)菜單左右聯(lián)動(dòng)
- 微信小程序scroll-view實(shí)現(xiàn)左右聯(lián)動(dòng)效果
- 微信小程序?qū)崿F(xiàn)左右聯(lián)動(dòng)
- 微信小程序scroll-view實(shí)現(xiàn)左右聯(lián)動(dòng)
- 微信小程序?qū)崿F(xiàn)左右列表聯(lián)動(dòng)
- 微信小程序?qū)崿F(xiàn)左右聯(lián)動(dòng)的實(shí)戰(zhàn)記錄
- 微信小程序?qū)崿F(xiàn)菜單左右聯(lián)動(dòng)效果
相關(guān)文章
js如何獲取訪問IP、地區(qū)、當(dāng)前操作瀏覽器
這篇文章主要介紹了js如何獲取訪問IP、地區(qū)、當(dāng)前操作瀏覽器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
深入解析Javascript閉包的功能及實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)解析Javascript閉包的功能及實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-07-07
使用requirejs模塊化開發(fā)多頁面一個(gè)入口js的使用方式
這篇文章主要介紹了使用requirejs模塊化開發(fā)多頁面一個(gè)入口js的使用方式,需要的朋友可以參考下2017-06-06

