<template>
<view class="row navBox" :style="scrollHeiht">
<!-- 左侧一级分类列表 -->
<scroll-view :scroll-top="scrollTopLeft" scroll-y class="leftNavBox col20-5 height-100">
<view class="leftNavItem py-20 text-center" v-for="(item,index) in leftNavData" :key="index">
<view @tap="leftTap(index)" :class="leftActiveIndex===index?'leftActive':''" class="leftNavContent py-10">
{{item.title}}
</view>
</view>
</scroll-view>
<!-- 右侧二级分类列表 -->
<scroll-view @scroll="rightScroll" :scroll-top="scrollTop" scroll-y class="rightNavBox col20-15 height-100" scroll-with-animation>
<view class="rightNavItem row justify-content-space-around align-items-center" v-for="(item,index) in rightNavData" :key="index">
<view class="font-bold text-center col-10">
商品分类{{index+1}}
</view>
<view class="rightNavContent display-flex align-items-center justify-content-center flex-direction-column" v-for="(item1,index1) in item" :key="index1">
<image :src="item1.url"></image>
{{item1.title}}
</view>
</view>
</scroll-view>
</view>
</template>
<script>
export default {
data() {
return {
leftActiveIndex: 0,
leftNavData: [],
rightNavData: [],
scrollTop:0,
scrollHeiht:"",
leftScrollTop:[],
rightScrollTop:[],
leftNavItemHeight:0,
scrollTopLeft:0
}
},
onLoad() {
let height=uni.getSystemInfoSync().windowHeight
this.scrollHeiht=`height:${height}px`
for (var i = 0; i < 20; i++) {
let data = {
title: `分类${i+1}`
};
this.leftNavData.push(data);
this.rightNavData.push ([
{
url: "../../static/images/cate/cate_01.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_02.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_03.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_04.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_05.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_06.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_07.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_08.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_09.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_01.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_02.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_03.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_04.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_05.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_06.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_07.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_08.png",
title: `分类${i+1}:商品`
},
{
url: "../../static/images/cate/cate_09.png",
title: `分类${i+1}:商品`
}
])
}
},
onReady() {
let query = uni.createSelectorQuery();
query.selectAll('.rightNavItem').boundingClientRect(data => {
this.rightScrollTop=data.map(item=>item.top)
}).exec();
query.selectAll(".leftNavItem").boundingClientRect(data=>{
this.leftScrollTop=data.map(item=>item.top)
}).exec(res=>{
this.leftNavItemHeight=this.leftScrollTop[1]-this.leftScrollTop[0];
})
},
methods: {
leftTap(index) {
this.leftActiveIndex = index;
this.scrollTop=this.rightScrollTop[index]
},
rightScroll(event){
this.rightScrollTop.forEach((item,index)=>{
if(item<=event.detail.scrollTop){
this.leftActiveIndex=index
}
})
}
},
watch:{
leftActiveIndex(newValue,oldValue){
uni.createSelectorQuery().select(".leftNavBox").fields({
size:true,
scrollOffset:true
},res=>{
let H=res.height
if(this.leftScrollTop[newValue]+ this.leftNavItemHeight>H + res.scrollTop){
console.log("需要左侧向上滚动");
this.scrollTopLeft=this.leftScrollTop[newValue] + this.leftNavItemHeight-H;
}
if(res.scrollTop>this.leftScrollTop[newValue]){
this.scrollTopLeft=this.leftScrollTop[newValue];
}
}).exec()
}
}
}
</script>
<style>
.navBox {
border-top: 1px solid #F1F1F1;
}
.leftNavBox{
border-right: 1px solid #F1F1F1;
}
.leftNavItem {
border-bottom: 1px solid #F1F1F1;
}
.leftNavContent {
border-left: 3px solid transparent;
}
.leftActive {
border-left: 3px solid #df6c1c;
}
.rightNavContent{
margin:10upx;
}
.rightNavContent image{
width: 120upx;
height: 120upx;
display: block;
}
</style>