悬停时更改链接图像

changing link image on hover

本文关键字:链接 图像 悬停      更新时间:2023-09-26

我今天的问题可能有一个简单的答案,但我找到了一些工作示例,但似乎无法将其转移到我的网页上。

我正在尝试使用一个图像作为链接,并希望当你将鼠标悬停在它上面时,图像会发生变化。下面的链接是我试图实现的,但无论出于什么原因,当我将代码从页面替换到它时,它都不起作用。

示例http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_ev_onmouseover

我现在完全迷路了,只需要一点帮助。这是我的密码。

演示

function hoverImg(x) {
  x.style.backgroundImage = "url(image/arrowBtnHover.png)"
  x.style.transition = "ease 0.5s"
}
function normalImg(x) {
  x.style.backgroundImage = "url(image/arrowBtn.png)"
}
#header {
  background-color: #473D39;
  height: 100%;
  width: 100%;
  display: table;
  position: absolute;
  z-index: 10;
}
#wrapper {
  display: table-cell;
  vertical-align: middle;
}
#header h1 {
  text-align: center;
  margin: 0px;
  font-size: 80px;
  padding-top: 5%;
  font-weight: normal;
  color: #FFF;
  letter-spacing: 18px;
  text-transform: uppercase;
}
#header h5 {
  text-align: center;
  color: #FFF;
  margin: 15px 15px 50px;
  font-weight: normal;
  text-transform: uppercase;
  font-size: 14px;
  letter-spacing: 2px;
}
<div id="header">
  <div id="wrapper">
    <h1>Premier Webster</h1>
    <h5>Local Web Design For The Profesional In You</h5>
    <img onmouseover="hoverImg(this)" onmouseout="normalImg(this)" src="image/arrowBtn.png" />
  </div>
</div>

请查看https://jsfiddle.net/avzfdc2j/3/

它已经使用css与背景图像和转换完成

div.smile {
    background-image: url("http://images.clipartpanda.com/stupidity-clipart-1320682287266972230curius_face.svg.hi.png");
    background-size: 60px 60px;
    height: 60px;
    width: 60px;
    cursor: pointer;
}
div.smile:hover {
    background-image: url("http://images.clipartpanda.com/straight-face-clipart-black-and-white-smiley-face-hi.png");
    transition: ease 0.5s;
}
<a href="http://google.com/"><div class="smile"></div></a>

您应该改为更改src属性:

function hoverImg(x) {
    x.src = "image/arrowBtnHover.png"
    x.style.transition = "ease 0.5s"
}
function normalImg(x) {
    x.src = "image/arrowBtn.png"
}

但我认为这种过渡不会奏效。

因为它是图像,所以需要更改它的src属性,而不是CSS。

function hoverImg(x) {
    x.src = "image/arrowBtnHover.png"
    x.style.transition = "ease 0.5s"
}
function normalImg(x) {
    x.src = "image/arrowBtn.png"
}