【问题标题】:Change (color of) image along with link on hover/click在悬停/单击时更改图像(颜色)以及链接
【发布时间】:2019-09-06 07:59:57
【问题描述】:

所以自昨天以来我一直在使用 Javascript (Vue) 和 html(所以请原谅我的菜鸟),我不知道如何更改图像的颜色与链接同时位于“主页”链接说明旁边。

我尝试过使用 CSS 类,它可以工作,但只会在您将鼠标悬停在图像上而不是整个链接行上时更改图像颜色。除了 CSS,我还尝试通过“onmouseover”将图像更改为彩色版本,但不知何故立即将图标更改为彩色版本(?)。我猜问题是我通过 a:hover 控制链接颜色,通过单独的类控制图像颜色,所以图像颜色没有连接到链接,但我不知道如何解决这个问题?

您可以找到白色here 和红色here 的文档图标。

非常感谢您!


下面只是侧边栏“主页”部分的 sn-p:

<template>
  <div>
    <div v-if="currentUser" class="vertical-nav bg-dark" id="sidebar">
      <div class="py-1 px-3 mb-4 bg-dark">
      </div>
      <ul class="nav flex-column bg-dark mb-0">
        <li class="nav-item ">
          <a href="#" class="nav-link text-white font-bold ">
            <div class="document"></div>
                    <i class="fa mr-3 text-primary fa-fw"></i>
                     Home
                </a>
        </li>
      </ul>
    </div>
  </div>
</template>

和 CSS:

/* unvisited link */
a:link {
  color: white;
}

/* visited link */
a:visited {
  color: white;
}

/* mouse over link */
a:hover {
  color: rgb(202, 95, 95) !important;
}

/* selected link */
a:active {
  color: rgb(202, 95, 95) !important;

.document {
  width: 10px;
  height: 12px;
  background-size: cover;
  background: url("assets/documentWhiteSmall.png") no-repeat;
  display: inline-block;
}

.document:hover {
  width: 10px;
  height: 12px;
  background-size: cover;
  background: url("assets/otherDocRedSmall.png") no-repeat;
}


【问题讨论】:

  • 您可以为此使用someparent:hover actualelementjsfiddle.net/khrismuc/dj7pxch0
  • 由于类.document的div在&lt;a&gt;超链接内,类似于:` a:hover .document { url("assets/otherDocRedSmall.png"); }` 应该可以工作,但我无法让它在堆栈 sn-p 中工作,因为我没有看到实际的文档图标。我猜这是因为这里没有包含很棒的字体?
  • @ChrisG 感谢您的意见!当我使用它时,图像以某种方式在正常模式 i.imgur.com/3y6usuC.png 完全消失,然后在悬停模式 i.imgur.com/e9ABesL.png 的链接上方显示 :( 尽管它显然适用于小提琴。
  • 你能把你目前拥有的代码放在问题中吗?理想情况下为minimal reproducible example

标签: html css


【解决方案1】:

您可以根据 a 与 css 的悬停来定位您的 .document div:

a .document {
  width: 10px;
  height: 12px;
  background-size: cover;
  background: url("assets/documentWhiteSmall.png") no-repeat;
  display: inline-block;
}

a:hover .document {
  width: 10px;
  height: 12px;
  background-size: cover;
  background: url("assets/otherDocRedSmall.png") no-repeat;
}

【讨论】:

  • 感谢您的建议!不知何故,我最终在正常模式下完全没有符号,在悬停模式下又出现凹痕,只是在这里它看起来也没有正确调整图标的大小? i.imgur.com/VuACHNM.png
  • 我们需要您的更多代码来检查导致您看到的结果的原因。作为显示的简短指南,您可以使用 flex 组织导航链接,例如 a.nav-link { display: flex; align-items: center; }。您也可以将 ::before 伪类用于您的符号,因此请显示更多您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
  • 1970-01-01
相关资源
最近更新 更多