vue3.0+ts-左右联动

<template>
    <div className='switch-Box'>
        <div className="switch-left" ref="left">
            <div v-for="(item, index) in arr" :key="index" @touchstart="fnS(index)"
                :class="index === num.setnum ? 'ac' : ''">{{ item }}</div>
        </div>
        <div className="switch-right" ref="right" @scroll="fnScroll" @touchstart="overFn">
            <div v-for="(item, index) in arr" :key="index">{{ item }}</div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue';
const arr = ref<number[]>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
let num = reactive({ setnum: 0, setflag: true });
let left = ref<HTMLDivElement | null>(null);
let getleft = () => left.value as HTMLDivElement;
let right = ref<HTMLDivElement | null>(null);
let getright = () => right.value as HTMLDivElement;
let rightnum: number = 0;
onMounted(() => {
    rightnum = (getright().children[0] as HTMLDivElement).offsetHeight;
})
const fnS = (index: number) => {
    num.setnum = index;
    num.setflag = false;
    getright().scrollTop = index * rightnum;
    getleft().scrollTop = index > 4 ? (index - 3) * 93 : 0;
}
const fnScroll = () => {
    if (num.setflag === false) return false
    let Index = Math.trunc(getright().scrollTop / rightnum + 0.01);
    num.setnum = Index;
    getleft().scrollTop = Index > 4 ? (Index - 3) * 93 : 0;
}

const overFn = ()=>num.setflag = true;
</script>


<style lang="less">
.switch-Box {
    width: 100vw;
    height: 100vh;
    display: flex;
    overflow: hidden;


    .switch-left::-webkit-scrollbar {
        display: none;
    }


    .switch-left {
        width: 25vw;
        background: linear-gradient(to bottom, skyblue, pink, rebeccapurple);
        overflow-y: scroll;
        scroll-behavior: smooth;


        >div {
            width: 100%;
            height: (100vh / 8);
            text-align: center;
            line-height: (100vh / 8);
            border-bottom: 0.5px solid white;

        }
    }


    .switch-right::-webkit-scrollbar {
        display: none;
    }


    .switch-right {
        width: 75vw;
        background: linear-gradient(to bottom, skyblue, pink, rebeccapurple);
        overflow-y: scroll;
        scroll-behavior: smooth;



        >div {
            width: 100%;
            height: 110vh;
            border-bottom: 0.5px solid white;
            font-size: 30px;
            text-align: center;
            line-height: 110vh;
            color: white;
        }
    }
}

.ac {
    background: linear-gradient(to bottom, pink, skyblue, powderblue);
}
</style>

ts

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值