vue3左右联动分类页

js

完整代码

<template>
    <view class="content">
        <view class="header"></view>
        <view class="mainTopBox">
            <view class="mainTop">
                <uni-icons class="icon" type="bars" size="18"></uni-icons>
                <view class="history">历史订单</view>
            </view>
        </view>
        <view class="main">
            <view class="categoryLeft">
                <view class="menuItem" :class="{active:activeIndex === index}" v-for="(item,index) in list"
                    :key="item.id" @click="onClickItem(index)">
                    {{item.name}}
                </view>
            </view>
            <scroll-view :scroll-top="rightScrollValue" scroll-y @scroll="rightScrollEnt" class="categoryRight" scroll-with-animation>
                <view class="listBox" v-for="(item,index) in list" :key="item.id">
                    <view class="foodName">{{item.name}}</view>
                    <view class="itemBox" v-for="i in 5">
                        <image src="../../static/logo.png" mode=""></image>
                        <view class="itemBoxRight">
                            <view class="">白菜</view>
                            <uni-icons class="icon" type="person" size="30"></uni-icons>
                        </view>
                    </view>
                </view>
                <view class="noData">
                    没有更多了~
                </view>
            </scroll-view>
        </view>
        <view class="footer">
            <image src="../../static/cart.png" mode=""></image>
            <view class="btn">选好了</view>
        </view>
    </view>
</template>

<script setup>
    import {nextTick,ref} from "vue";
    import {onLoa} from "@dcloudio/uni-app"
    onLoad(() => {
        nextTick(()=>{
            getHeightArr()
        })
    })
    //滚动条最底部高度
    const bottomValue = ref(0)
    //滚动条最顶部高度
    const topValue = ref(0)
    //右边节点数组
    const rightHeightArr = ref([])
    //右边节点距离顶部的距离
    const rightScrollValue = ref(0)
    const list = ref([{
            id: 1,
            name: '荤菜'
        },
        {
            id: 2,
            name: '素菜'
        },
        {
            id: 3,
            name: '主食'
        },
    ])
    const activeIndex = ref(0)
    //分类点击事件
    const onClickItem = (e) => {
        if(activeIndex.value === e) return
        activeIndex.value = e
        rightScrollValue.value = rightHeightArr.value[e]
    }
    //获取右边节点信息
    const getHeightArr = ()=>{
        const query = uni.createSelectorQuery();
        query.select('.mainTop').boundingClientRect(data => {    //获取滚动条最顶部高度
            topValue.value = data.bottom 
        }).exec();
        query.select('.footer').boundingClientRect(data => {    //获取滚动条最底部高度
            bottomValue.value = data.top - topValue.value
        }).exec();
        query.selectAll('.listBox').boundingClientRect(data => {     //滚动区域最底部高度
            rightHeightArr.value = data.map(item=>item.top - 70)
        }).exec();

    }
    //右边滚动事件
    const rightScrollEnt = (e)=>{
        const { scrollTop,scrollHeight } = e.detail
        const idx = rightHeightArr.value.findIndex((item,index,arr)=>scrollTop>=item && scrollTop < arr[index+1])
        if(scrollTop + bottomValue.value >= scrollHeight - 35){ //scrollHeight是滚动区域最底部高度, - 35 是减去没有更多数据了(noData)的高度
            activeIndex.value = rightHeightArr.value.length-1
        }else{
            activeIndex.value = idx
        }
    }
</script>

<style lang="scss" scoped>
    .content {
        display: flex;
        flex-direction: column;
        width: 100%;
        height: 100vh;

        .header {
            background-color: #F5D463;
            height: 100rpx;
        }

        .mainTopBox {
            height: 80rpx;
            background-color: #f8f8f8;
            border-radius: 25rpx 25rpx 0 0;
            margin-top: -20px;

            .mainTop {
                display: flex;
                height: 100%;
                align-items: center;
                justify-content: flex-end;

                .history {
                    font-size: 28rpx;
                    margin-right: 20rpx;
                    color: #00aaff;
                }
            }

        }

        .main {
            flex: 1;
            display: flex;
            height: calc(100% - 280rpx);

            .categoryLeft {
                width: 25%;
                background-color: #f8f8f8;
                margin-right: 20rpx;

                .menuItem {
                    height: 100rpx;
                    line-height: 100rpx;
                    text-align: center;
                    color: #7D7C82;
                }

                .active {
                    background-color: #fff;
                    color: #000;
                }
            }

            .categoryRight {
                width: 75%;
                .listBox {

                    .foodName {
                        height: 100rpx;
                        line-height: 100rpx;
                        font-size: 30rpx;
                        color: #7D7C82;
                        position: sticky;
                        top: 0;
                        background-color: #fff;
                    }

                    .itemBox {
                        display: flex;
                        margin-bottom: 20rpx;

                        image {
                            width: 150rpx;
                            height: 150rpx;
                            border-radius: 10rpx;
                        }

                        .itemBoxRight {
                            flex: 1;
                            display: flex;
                            justify-content: space-between;
                            align-items: center;
                            margin: 0 30rpx;
                        }
                    }
                }

                .noData {
                    font-size: 30rpx;
                    color: #aaa;
                    text-align: center;
                    padding: 10rpx 0 20rpx;
                }
            }
        }

        .footer {
            height: 100rpx;
            background-color: #f8f8f8;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 0 20rpx 20px;
            border-radius: 50rpx;

            image {
                width: 120rpx;
                height: 120rpx;
            }

            .btn {
                width: 180rpx;
                height: 100%;
                line-height: 100rpx;
                text-align: center;
                font-size: 34rpx;
                font-weight: bold;
                background-color: #F5D463;
                border-radius: 0 50rpx 50rpx 0;
            }
        }
    }
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例,展示了如何在uniapp中使用vue3实现左右联动: ``` <template> <view class="container"> <view class="left-list"> <view class="list-item" :class="{ active: activeIndex === index }" v-for="(item, index) in leftList" :key="index" @click="handleClickLeft(index)" > {{ item }} </view> </view> <view class="right-list"> <view class="list-item" v-for="(item, index) in rightList" :key="index" v-show="activeIndex === index" > {{ item }} </view> </view> </view> </template> <script> import { reactive } from 'vue'; export default { setup() { const state = reactive({ leftList: ['A', 'B', 'C', 'D'], rightList: [ ['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'], ['C1', 'C2', 'C3'], ['D1', 'D2', 'D3'], ], activeIndex: 0, }); const handleClickLeft = (index) => { state.activeIndex = index; }; return { state, handleClickLeft, }; }, }; </script> <style> .container { display: flex; flex-direction: row; justify-content: space-between; height: 100vh; } .left-list { width: 200rpx; background-color: #eee; overflow-y: scroll; } .right-list { flex: 1; background-color: #fff; overflow-y: scroll; } .list-item { height: 100rpx; line-height: 100rpx; text-align: center; } .list-item.active { background-color: #f00; color: #fff; } </style> ``` 在这个示例中,我们使用了`reactive`函数创建了一个响应式对象`state`,其中包含了左侧列表和右侧列表的数据以及当前选中的索引。我们通过`v-for`指令渲染了左侧列表和右侧列表,并使用`v-show`指令控制右侧列表的显示。当左侧列表的某一项被点击时,我们更新了`activeIndex`的值,从而实现了左右联动的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值