1。实现数据左右联动,页面如下:

3.实现代码
<template>
<view class="content">
<view v-if="showOrder" class="category-wrap">
<scroll-view scroll-y scroll-with-animation class="category-bar">
<view
v-for="(item, index) in cateData"
:key="index"
class="category-item"
:class="[current === index ? 'current' : '']"
@click="switchCate(index)"
>
<text class="line-1">{{ item.name }}</text>
</view>
</scroll-view>
<scroll-view
scroll-y
scroll-with-animation
class="category-content"
:scroll-top="scrollRightTop"
@scroll="rightScroll"
>
<view style="padding: 16rpx; background-color: #f9f9f9">
<view
v-for="(item, index) in cateData"
:key="index"
class="category-right-item"
:class="{ 'category-right-item-last': cateData.length == index + 1 }"
>
<view class="item-title">
<text>{{ item.name }}</text>
</view>
<view class="item-container">
<view
v-for="(item1, index1) in item.foods"
:key="index1"
class="thumbnail-box"
@click="toPage('/packagesA/pages/angsi/webview', true)"
>
<c-image class="item-cate-image" :src="item1.icon" resize="_r60xp"></c-image>
<view class="item-cate-name">{{ item1.name }}</view>
</view>
</view>
</view>
</view>
</scroll-view>
</view>
<no-data v-else></no-data>
</view>
</template>
<script>
import noData from '@/components/no-data';
export default {
components: {
noData,
},
data() {
return {
cateData: [],
current: 0,
scrollLeftTop: 0,
cateItemHeight: 0,
cateBarHeight: 0,
rightScrollArr: [],
scrollRightTop: 0,
showOrder: true,
};
},
onReady() {
this.cateData = [
{
name: '热门推荐',
foods: [
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '话费慢充',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
],
},
{
name: '视频会员',
foods: [
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '话费慢充',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
],
},
{
name: '生活服务',
foods: [
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '话费慢充',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
],
},
{
name: '音乐会员',
foods: [
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '话费慢充',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
],
},
{
name: '美食餐饮',
foods: [
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '话费慢充',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
],
},
{
name: '读书会员',
foods: [
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '话费慢充',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
],
},
{
name: '健康权益',
foods: [
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '话费慢充',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
{
icon: 'https://livestreaming-prod.oss-cn-shenzhen.aliyuncs.com/product/2022/5/23/1528621303328800768.png',
name: '美团外卖',
},
],
},
];
this.getCateItemTop();
},
methods: {
getCateTreeByCateLevel1() {
const that = this;
const params = {
type: that.cateType,
code: that.cateLevel1[that.cateLevel1Index].code,
};
return that.apiUrl
.productGetCategoryByTreeByCode({ data: params })
.then((res) => {
if (res.data.status == 1) {
that.cateData = res.data.data;
that.getCateItemTop();
}
})
.finally(() => {
if (this.cateData && this.cateData.length > 0) {
this.showOrder = true;
} else {
this.showOrder = false;
}
});
},
getCateItemTop() {
let query = uni.createSelectorQuery();
query
.selectAll('.category-right-item')
.boundingClientRect((rects) => {
rects.forEach((rect) => {
this.rightScrollArr.push(rect.top);
});
})
.exec(function () {});
},
async switchCate(index) {
if (this.rightScrollArr.length == 0) {
await this.getCateItemTop();
}
if (index === this.current) return;
this.scrollRightTop = this.rightScrollArr[index];
this.cateDataStatus(index);
},
cateDataStatus(index) {
this.current = index;
if (this.cateBarHeight == 0 || this.cateItemHeight == 0) {
this.getElRect('.category-bar', 'cateBarHeight');
this.getElRect('.category-item', 'cateItemHeight');
}
this.scrollLeftTop =
index * this.cateItemHeight + this.cateItemHeight / 2 - this.cateBarHeight / 2;
},
getElRect(elClass, dataVal) {
const query = uni.createSelectorQuery();
query
.select(elClass)
.fields(
{
size: true,
},
(res) => {
this[dataVal] = res.height;
}
)
.exec(function () {});
},
async rightScroll(e) {
if (this.rightScrollArr.length == 0) {
await this.getCateItemTop();
}
if (!this.cateBarHeight) {
await this.getElRect('.category-bar', 'cateBarHeight');
}
let scrollHeight = e.detail.scrollTop;
for (let i = 0; i < this.rightScrollArr.length; i++) {
let height1 = this.rightScrollArr[i];
let height2 = this.rightScrollArr[i + 1];
if (!height2 || scrollHeight <= height1) {
this.cateDataStatus(i);
return;
}
}
},
},
};
</script>
<style lang="less" scoped>
page {
font-size: 28rpx;
color: #303133;
}
.content {
display: flex;
flex-direction: column;
height: calc(100vh - var(--window-top));
}
.category-wrap {
flex: 1;
display: flex;
overflow: hidden;
}
.category-bar {
width: 170rpx;
background-color: #f6f6f6;
.category-item {
text-align: center;
height: 92rpx;
line-height: 92rpx;
&.current {
color: #e93b3d;
background-color: #ffffff;
}
}
}
.search-box {
display: flex;
align-items: center;
margin: 24rpx;
background-color: #eaeaea;
border-radius: 50rpx;
padding: 10rpx 16rpx;
font-size: 26rpx;
color: #909399;
}
.line-1 {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
.category-right-item {
margin-bottom: 30rpx;
background-color: #fff;
padding: 16rpx;
border-radius: 8rpx;
}
.category-right-item-last {
// height: calc(100vh - 84rpx - var(--window-top));
height: calc(100vh - 114rpx - var(--window-top));
}
.item-title {
font-size: 26rpx;
font-weight: bold;
color: #303133;
text-align: center;
}
.item-container {
display: flex;
flex-wrap: wrap;
}
.thumbnail-box {
width: 33.33%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin-top: 20rpx;
}
.item-cate-name {
font-weight: normal;
font-size: 24rpx;
color: #303133;
}
.item-cate-image {
width: 120rpx;
height: 120rpx;
}
</style>