当我将鼠标悬停在 link 上时希望切换图像
Looking to switch image when I hover over a link
我已经为这个问题苦苦挣扎了一段时间,我一直在寻找其他类似的问题,但还没有找到我的问题的答案。当我将鼠标悬停在图片下方的 link 上时,我希望更改导航栏中的图片。
例如,当我没有将鼠标悬停在 link“主页”上时,这是我的导航栏。
当我将鼠标悬停在 link“主页”
上时,这就是我想要发生的事情
如有任何帮助,我们将不胜感激!
* {
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>
将鼠标悬停在 link 上时,您可以使用 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"
}
我已经为这个问题苦苦挣扎了一段时间,我一直在寻找其他类似的问题,但还没有找到我的问题的答案。当我将鼠标悬停在图片下方的 link 上时,我希望更改导航栏中的图片。
例如,当我没有将鼠标悬停在 link“主页”上时,这是我的导航栏。
当我将鼠标悬停在 link“主页”
上时,这就是我想要发生的事情如有任何帮助,我们将不胜感激!
* {
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>
将鼠标悬停在 link 上时,您可以使用 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"
}