Vue实现左侧菜单锚点、右侧滑动

该篇文章介绍了如何使用Vue.js构建一个带有动态颜色和标签的卡片式组件,当用户滚动页面时,根据滚动位置自动切换对应的卡片并使其内容居中显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>

  <div class="editBox">

    <el-tabs v-model="activeName" type="card" tab-position="left" @tab-click="handleTabClick">

      <el-tab-pane v-for="tab in tabs" :key="tab.name" :name="tab.name">

        <span slot="label">{{ tab.label }}</span>

      </el-tab-pane>

    </el-tabs>

    <div class="edit-cards">

      <div v-for="tab in tabs" :key="tab.name" :id="tab.name"

        :class="['edit-card', tab.color, { active: activeName === tab.name }]">

        {{ tab.label }}

      </div>

    </div>

  </div>

</template>

<script>

export default {

  data() {

    return {

      activeName: 'first', // 当前激活的标签名

      tabs: [

        { name: 'first', label: '用户管理', color: 'edit-red' },

        { name: 'second', label: '配置管理', color: 'edit-orange' },

        { name: 'third', label: '角色管理', color: 'edit-yellow' },

        { name: 'fourth', label: '定时任务补偿', color: 'edit-green' },

      ],

    };

  },

  mounted() {

    this.bindScrollEvent();

  },

  methods: {

    bindScrollEvent() {

      const editCards = document.querySelector('.edit-cards');

      const tabHeights = this.tabs.map(tab => {

        const card = document.getElementById(tab.name);

        return {

          tabName: tab.name,

          offsetTop: card.offsetTop,

        };

      });

      editCards.addEventListener('scroll', () => {

        const scrollPosition = editCards.scrollTop;

        tabHeights.forEach((item, index) => {

          if (index === tabHeights.length - 1) {

            if (scrollPosition + 200 >= item.offsetTop) { // 200 是一个偏移量,可以根据实际需要调整

              this.activeName = item.tabName;

            }

            return;

          }

          if (scrollPosition >= item.offsetTop &&

            scrollPosition < tabHeights[index + 1].offsetTop - 200) { // 200 是一个偏移量,可以根据实际需要调整

            this.activeName = item.tabName;

          }

        });

      });

    },

    scrollToTabContent(tabName) {

      const contentElement = document.getElementById(tabName);

      if (contentElement) {

        contentElement.scrollIntoView({ behavior: 'smooth', block: 'start' });

      }

    },

    handleTabClick(tab) {

      this.activeName = tab.name;

      this.scrollToTabContent(tab.name);

    },

  },

};

</script>

<style scoped>

.editBox {

  width: 100%;

  display: flex;

}

.edit-cards {

  height: 80vh;

  overflow-y: scroll;

}

.edit-card {

  width: 500px;

  font-size: 30px;

  padding: 20px;

}

.edit-red {

  height: 800px;

  background-color: red;

}

.edit-orange {

  height: 500px;

  background-color: orange;

}

.edit-yellow {

  height: 700px;

  background-color: yellow;

}

.edit-green {

  height: 400px;

  background-color: green;

}

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值