Hovered的图像出现了更多图像(类似图库)Html css Javascript

时间:2018-05-21 10:37:50

标签: javascript html css image hover

我遇到这个问题,我将鼠标悬停在图像上以显示另一个图像,但是第一个图像仍然出现,而新图像不会改变高度和宽度,而是与另一个图像重叠。我仍然是HTML / CSS的新手所以我可能错过了一些简单的东西。这里是示例代码:

<img src="LibraryTransparent.png" id="Library" />
<style>
#Library {
    height: 70px;
    width: 120px;
}

#Library:hover {
    background-image: url('LibraryHoverTrans.png');
    height: 70px;
    width: 120px;
}
</style>

1 个答案:

答案 0 :(得分:0)

不知道这是否太多,但这有效:

HTML:

<div class="container">
  <img src="https://placeimg.com/600/400/animals" alt="dog" />
  <img src="https://placeimg.com/600/400/people" alt="people" />
</div>

CSS:

.container {
  position: relative;
  width: 600px;
  height: 400px;
  margin-left: 50px;
}

.container:hover img:nth-of-type(2) {
  opacity: 1;
}
.container img {
  position: absolute;
  top: 0;
  left: 0;
  width: 500px;
  height: 300px;
  transition: opacity 1s ease-in-out 0s;
}
.container img:nth-of-type(1) {
  opacity: 1;
}
.container img:nth-of-type(2) {
  opacity: 0;
}

在此处观看:https://jsfiddle.net/billy_farroll/ajy5bm5f/