【问题标题】:Looking to switch image when I hover over a link当我将鼠标悬停在链接上时希望切换图像
【发布时间】:2021-03-04 09:20:46
【问题描述】:

我一直在努力解决这个问题,我一直在寻找其他类似的问题,但还没有找到我的问题的答案。当我将鼠标悬停在图片下方的链接上时,我希望更改导航栏中的图片。

例如,当我没有将鼠标悬停在“主页”链接上时,这是我的导航栏。

当我将鼠标悬停在“主页”链接上时,这就是我想要发生的事情

任何帮助将不胜感激!

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-size: 18px;
  font-family: 'Poppins';
}

li, a {
  text-decoration: none;
  color: white;
}

ul {
  list-style: none;
}

.navbar {
  display: flex;
  flex-direction: row;
  width: 100%;
  justify-content: center;
  align-items: center;
  margin-left: 2em;
  padding-top: 2em;
}

.navlink::before {
  content: "";
  position: absolute;
  width: 50px;
  height: 5px;
  background-color: #703FFF;
  margin-top: 120px;
  margin-left: 2px;
  border-radius: 10px;
}

.navlink:nth-last-child(2)::before {
  margin-left: 20px;
}

.navlink:last-child::before {
  margin-left: 15px;
}

.navlink {
  display: flex;
  flex-direction: column;
  margin-right: 2.5em;
}

.navlink img {
  width: 3em;
  align-self: center;
}

/* Portfolio link */
.navlink:nth-last-child(3) {
  margin-right: 2em;
}

/* contact link */
.navlink:last-child {
  margin-left: -1em;
}

.navlink a{
  align-self: center;
  margin-top: 2em;
  text-transform: uppercase;
  font-weight: 600;
  font-size: 16px;
}

.new-wave {
  background: url("../images/Nav_Wave.png") center center/cover no-repeat;
  height: 110vh;
}
<div class="new-wave">
    <div class="navbar">
        <div class="navlink">
          <img src="images/home_dark.png" alt="">                                                                                   
          <a href="#">home</a>

        </div>
          <div class="navlink">
          <img src="images/about_dark.png"  alt="">
            <a href="#">about</a>
        </div>
        <div class="navlink">
          <img src="images/skills_dark.png"  alt="">
          <a href="#">skills</a>
        </div>
        <div class="navlink">
          <img src="images/portfolio_dark.png"  alt="">
          <a href="#">portfolio</a>
        </div>
        <div class="navlink">
          <img src="images/contact_dark.png"  alt="">
          <a href="#">contact</a>
        </div>
      </div>
    </div>

【问题讨论】:

    标签: css image hyperlink navigation hover


    【解决方案1】:

    当您将鼠标悬停在链接上时,您可以使用 javascript 更改 img src。离开时再放回去。

    首先给你的元素一些 ID:

    <img  id ="home-img"src="images/home_dark.png" alt="">                                                                                   
    <a id ="home" href="#">home</a>
    

    然后监听事件:

    document.getElementById("home").onmouseenter = function() {
      document.getElementById("home-img").src = "other/img.png"
    }
    document.getElementById("home").onmouseleave = function() {
      document.getElementById("home-img").src = "images/home_dark.png"
    }
    

    【讨论】:

    • 如果我想在鼠标悬停链接时在图像上添加过渡效果,我该怎么做?
    • @Greger 这有点棘手,但是您可以为您的元素创建一个具有所需效果的 css 类,然后在进入时添加该类并在离开时删除。这将导致元素显示动画。查看更多here.
    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 2015-03-16
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多