Vue3 +Ts 实现美团二级联动效果

  • index.Vue
<template slot-scope="scope">
  <div class="Linkage-Box">
    <div class="Linkage-Box-left" ref="leftlink">
      <div @click="handelchange(index)" :class="selectIndex === index ? 'action' : ''" v-for="(item, index) in carList"
        :key="index">
        {{ item }}
      </div>
    </div>
    <div class="Linkage-Box-right" @scroll="Fnscroll" ref="rightlink" @touchstart="rihandelscroll">
      <div v-for="(item, index) in carList" :key="index">{{ item }}</div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from "vue";
const selectIndex = ref<number>(0);
const flag = ref<boolean>(true);
const rightlink = ref<HTMLDivElement | null>(null);
const leftlink = ref<HTMLDivElement | null>(null);
const getleftlink = () => leftlink.value as HTMLDivElement;
const getrightlink = () => rightlink.value as HTMLDivElement;
const carList = ref<number[]>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
const selectarr = ref<number[]>([]);
const height = ref<number>(0);
const Riheight = ref<number>(0);
onMounted(() => {
  Riheight.value = (getrightlink().children[0] as HTMLDivElement).offsetHeight
  selectarr.value.push(height.value);
  getrightlink().childNodes.forEach(item => {
    height.value += Riheight.value
    selectarr.value.push(height.value);
  });
})
const handelchange = (index: number) => {
  flag.value = false;
  selectIndex.value = index;
  getrightlink().scrollTop = index * Riheight.value
  getleftlink().scrollTop = index >= 4 ? (index - 3) * 84 : 0
};
const Fnscroll = () => {
  if (flag.value === false) return
  selectarr.value.forEach((item, index) => {
    if (getrightlink().scrollTop >= item - 50) {
      selectIndex.value = index;
      getleftlink().scrollTop = index >= 4 ? (index - 3) * 84 : 0
    }
  });
};
const rihandelscroll = () => {
  flag.value = true
}
</script>
<style lang="less" scoped="scoped">
.Linkage-Box {
  width: 100vw;
  height: 100vh;
  background: red;
  display: flex;
  overflow: hidden;

  .Linkage-Box-left::-webkit-scrollbar {
    display: none;
  }

  .Linkage-Box-left {
    width: 25vw;
    height: 100vh;
    background: skyblue;
    overflow-y: scroll;
    scroll-behavior: smooth;

    >div {
      width: 100%;
      height: 150px;
      border-bottom: 0.5px solid #000;
      font-size: 30px;
      text-align: center;
      line-height: 150px;
    }
  }

  .Linkage-Box-right::-webkit-scrollbar {
    display: none;
  }

  .Linkage-Box-right {
    width: 75vw;
    height: 100vh;
    background: pink;
    overflow-y: scroll;
    scroll-behavior: smooth;

    >div {
      width: 100%;
      height: 90vh;
      border-bottom: 0.5px solid #000;
      font-size: 80px;
      text-align: center;
    }
  }
}

.action {
  background: pink;
}
</style>

大致思路和我之前写的React+Hook的思路一致  就是语法实现的转换;

文章标题:React + Ts + Hook实现美团左右二级联动

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值