You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Vue.js面包屑组件多问题修复求助

Vue.js面包屑组件多问题修复求助

Hey Han Solo,我仔细看了你遇到的面包屑组件的几个小问题,咱们一步步来逐个解决,先梳理下你的核心痛点:

  • 鼠标悬停最后一个面包屑时,分隔符">"也被加上了下划线,不想出现这个情况
  • 悬停分隔符">"时,鼠标变成指针,希望取消这个交互
  • 只显示首页时,末尾会多出一个">",比如显示"Startseite >"
  • 悬停"Startseite"文本时,图标和文本之间出现下划线,想去掉这个多余的下划线

先来看你的现有代码,然后给出针对性的修改方案:

你的现有组件代码(breadcrumb.vue)

<template>
  <nav class="breadcrumb">
    <router-link v-for="(crumb, index) in breadcrumbs" :key="index" :to="crumb.path">
      <template v-if="crumb.name === 'Startseite'">
        <img :src="homeIcon" alt="Home" class="breadcrumb-icon" />
        {{ crumb.name }}
      </template>
      <template v-else>
        {{ crumb.name }}
      </template>
    </router-link>
  </nav>
</template>

<script>
import homeIcon from '@/assets/img/Home.svg'; // Pfad zur SVG-Datei

export default {
  data() {
    return {
      homeIcon,
    };
  },
  computed: {
    breadcrumbs() {
      let pathArray = this.$route.path.split("/");
      pathArray.shift(); // Remove the first empty element
      let breadcrumbArray = [{ name: "Startseite", path: "/" }];
      pathArray.forEach((path, index) => {
        breadcrumbArray.push({
          name: path.charAt(0).toUpperCase() + path.slice(1),
          path: "/" + pathArray.slice(0, index + 1).join("/"),
        });
      });
      return breadcrumbArray;
    },
  },
};
</script>

<style scoped>

.breadcrumb-icon {
  width: 1.4rem;
  height: 1.4rem;
  
}
nav a.router-link-exact-active {
  color: black;
}

</style>

你的组件CSS文件

@media (min-width: 1024px) {
  .Veranstaltungen {
    min-height: 100vh;
    display: flex;
    align-items: center;
  }
}


html, body {
    overflow: hidden; /* Hide scroll bars */
    margin: 0; /* Remove default margin */
    padding: 0; /* Remove default padding */
    width: 100vw; /* Ensure full viewport width */
    height: 100vh; /* Ensure full viewport height */
}

/* upper Rectangle */

.ver_background {
    position: absolute;
    width: 100vw; /* 100% of the viewport width */
    height: 100vh; /* 100% of the viewport height */
    left: 0;
    top: 80px;
    background: #E4E4E4; /* White background */
    border-radius: 20px 20px 0 0;
}


/* lower Rectangle Copy */

.ver_window {

position: absolute;
width: 100vw; /* 100% of the viewport width */
height: 100vh; /* 100% of the viewport height */
left: 0px;
top: 244px;
background: #F9FAFA;
border-radius: 20px 20px 0px 0px;
}

/* Neue Veranstaltung */

.headline {
position: absolute;
width: 333px;
height: 40px;
left: 40px;
top: 132px;

/* Headline_32 */

font-style: normal;
font-weight: 800;
font-size: 2rem;
line-height: 3.0rem;
/* identical to box height, or 125% */

color: #000000;

}

.breadcrumb {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
  font-size: 1rem;
  color: #000000;
  position: absolute;
  width: auto;
  height: auto;
  left: 2.5rem;
  top: 6rem;

}

  .breadcrumb router-link a {
    text-decoration: none;
    color: black; /* Set the text color to black */
    padding: 5px;
  }
  
  .breadcrumb router-link:hover,
  .breadcrumb router-link a:hover {
    text-decoration: underline;
    color: darkgoldenrod; /* Change color on hover */
  }
  
  .breadcrumb router-link::after,
  .breadcrumb router-link a::after {
    content: ' / '; /* Add separator */
    color: #999; /* Separator color */
  }
  
  .breadcrumb router-link:last-child::after,
  .breadcrumb router-link a:last-child::after {
    content: ''; /* Remove separator after the last breadcrumb */
  }
  
  .breadcrumb a {
    text-decoration: none;
    color: #000000;
    padding: 0.2rem;
  
  }
  
  .breadcrumb a:hover {
    text-decoration: underline;
    background-color: #ff000000;
  }
  
  .breadcrumb a + a::before {
    content: ">";
    padding: 0 0.5rem;
    color: #6c757d;
    text-decoration: none; /* Ensure the ">" does not get underlined */
   
  }
  
  .breadcrumb-icon {
    width: 1.4rem;
    height: 1.4rem;
    margin-right: 0.2rem;
    position: relative;
    top: 0.25rem; /* Adjust this value to move the image up or down */
  
  }

你的base.css文件

/* color palette from <https://github.com/vuejs/theme> */
:root {
  --vt-c-white: #ffffff;
  --vt-c-white-soft: #f8f8f8;
  --vt-c-white-mute: #f2f2f2;

  --vt-c-black: #2d3a42;
  --vt-c-black-soft: #222222;
  --vt-c-black-mute: #282828;

  --vt-c-indigo: #2c3e50;

  --vt-c-divider-light-1: rgba(60, 60, 60, 0.29);
  --vt-c-divider-light-2: rgba(60, 60, 60, 0.12);
  --vt-c-divider-dark-1: rgba(84, 84, 84, 0.65);
  /* --vt-c-divider-dark-2: rgba(84, 84, 84, 0.48); */

  --vt-c-text-light-1: var(--vt-c-indigo);
  --vt-c-text-light-2: rgba(60, 60, 60, 0.66);
  --vt-c-text-dark-1: var(--vt-c-white);
  --vt-c-text-dark-2: rgba(235, 235, 235, 0.64); 
}

/* semantic color variables for this project */
:root {
  --color-background: var(--vt-c-white);
  --color-background-soft: var(--vt-c-white-soft);
  --color-background-mute: var(--vt-c-white-mute);

  --color-border: var(--vt-c-divider-light-2);
  --color-border-hover: var(--vt-c-divider-light-1);

  --color-heading: var(--vt-c-text-light-1);
  --color-text: var(--vt-c-text-light-1);

  --section-gap: 160px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-background: var(--vt-c-black);
    --color-background-soft: var(--vt-c-black-soft);
    --color-background-mute: var(--vt-c-black-mute);

    --color-border: var(--vt-c-divider-dark-2);
    --color-border-hover: var(--vt-c-divider-dark-1);

    --color-heading: var(--vt-c-text-dark-1);
    --color-text: var(--vt-c-text-dark-2);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  font-weight: normal;
}

body {
  min-height: 100vh;
  /*color: var(--color-text);*/
  background: var(--color-background);
  transition:
    color 0.5s,
    background-color 0.5s;
  line-height: 1.6;
  font-family:
    Inter,
    -apple-system,
    BlinkMacSystemFont,
    'Segoe UI',
    Roboto,
    Oxygen,
    Ubuntu,
    Cantarell,
    'Fira Sans',
    'Droid Sans',
    'Helvetica Neue',
    sans-serif;
  font-size: 15px;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

你尝试过的修改

.breadcrumb a:hover + a::before {
  color: #007bff; /* Change this to your desired hover color */
}

问题修复方案

核心思路

你之前的问题主要源于分隔符和链接元素绑定在一起,导致hover状态互相影响,还有重复的分隔符样式冲突。我们的解决方案是把面包屑项拆成独立的容器,让分隔符成为独立的非交互元素,彻底分离链接和分隔符的状态。

1. 修改面包屑组件模板

把每个面包屑项用<li>包裹,分隔符做成独立的<span>,只在非最后一项显示:

<template>
  <nav class="breadcrumb">
    <ul>
      <li v-for="(crumb, index) in breadcrumbs" :key="index">
        <router-link :to="crumb.path">
          <template v-if="crumb.name === 'Startseite'">
            <img :src="homeIcon" alt="Home" class="breadcrumb-icon" />
            <span class="crumb-text">{{ crumb.name }}</span>
          </template>
          <template v-else>
            <span class="crumb-text">{{ crumb.name }}</span>
          </template>
        </router-link>
        <!-- 仅在非最后一项显示分隔符 -->
        <span class="breadcrumb-separator" v-if="index !== breadcrumbs.length - 1">></span>
      </li>
    </ul>
  </nav>
</template>

2. 重构面包屑相关CSS

删除原来重复的分隔符样式(router-link::aftera + a::before相关代码),替换为新的样式:

.breadcrumb {
  position: absolute;
  width: auto;
  height: auto;
  left: 2.5rem;
  top: 6rem;
  font-size: 1rem;
  color: #000000;
}

.breadcrumb ul {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
}

.breadcrumb li {
  display: flex;
  align-items: center;
}

/* 独立分隔符样式 */
.breadcrumb-separator {
  color: #6c757d;
  margin: 0 0.5rem;
  /* 取消鼠标交互,避免指针变化 */
  pointer-events: none;
  /* 确保不会出现下划线 */
  text-decoration: none;
}

/* 链接基础样式 */
.breadcrumb router-link a {
  text-decoration: none;
  color: black;
  padding: 5px;
  display: flex;
  align-items: center;
}

/* 仅给文本加hover下划线,图标不受影响 */
.breadcrumb router-link:hover .crumb-text {
  text-decoration: underline;
  color: darkgoldenrod;
}

.breadcrumb-icon {
  width: 1.4rem;
  height: 1.4rem;
  margin-right: 0.2rem;
  position: relative;
  top: 0.25rem;
  /* 让图标成为块级元素,避免下划线穿过 */
  display: inline-block;
}

nav a.router-link-exact-active {
  color: black;
}

3. 问题逐个解决说明

  • 分隔符下划线/指针问题:独立的<span class="breadcrumb-separator">设置了pointer-events: none,不会响应鼠标事件,自然不会有指针变化;同时它不属于链接元素,hover链接时只会影响文本的下划线,分隔符完全不受影响。
  • 首页多余分隔符问题:通过v-if="index !== breadcrumbs.length - 1"控制,当只有首页时,面包屑数组长度为1,不会渲染分隔符。
  • 图标和文本间下划线问题:给文本单独包裹了<span class="crumb-text">,hover时只给这个span加下划线,图标和文本之间的空间不会被下划线覆盖;同时图标设置了display: inline-block,进一步避免下划线穿过图标区域。

备注:内容来源于stack exchange,提问作者Han Solo

火山引擎 最新活动