[英]HTML - Display image on hover
So I have this div class:所以我有这个 div class:
<div id="object1">
<div class="title" onmouseover="hoverImage(event)">SPT 3</div>
</div>
And outside of that div, I have another div with a image:在那个 div 之外,我还有另一个带有图像的 div:
<div class="hover_img" id="hover_img">
<img src="img/sptgoal.PNG">
</div>
CSS code to center the image: CSS 代码使图像居中:
.hover_img {
position:relative;
height: 100%;
width:100%;
z-index: 99;
}
.hover_img img {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display:none;
}
I only want the image to be visible when hovering the div.我只希望图像在悬停 div 时可见。 This is the Javascript code I have:这是我拥有的 Javascript 代码:
function hoverImage() {
var x = document.getElementById("hover_img");
x.style.display = "block";
}
But when hovering the mouse over, nothing shows up.但是当鼠标悬停在上面时,什么也没有出现。 What am I doing wrong here?我在这里做错了什么?
With document.getElementById("hover_img")
you don't show the image, but the container around it ( hover_img
).使用document.getElementById("hover_img")
您不会显示图像,而是显示图像周围的容器( hover_img
)。 But you have hidden the image ( .hover_img img
) in your CSS.但是您已在 CSS 中隐藏了图像( .hover_img img
)。
Use document.querySelectorAll("#hover_img>img")
for selecting your image.使用document.querySelectorAll("#hover_img>img")
选择您的图像。 querySelectorAll will return a NodeList, so you have to use x[0]
for selecting the first element in list. querySelectorAll将返回一个 NodeList,因此您必须使用x[0]
来选择列表中的第一个元素。
function hoverImage(event) { var x = document.querySelectorAll("#hover_img>img"); var sDisplay = (event.type === "mouseout")? "none": "block"; x[0].style.display = sDisplay; }
.hover_img { position:relative; height: 100%; width:100%; z-index: 99; }.hover_img img { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); display:none; }
<div id="object1"> <div class="title" onmouseover="hoverImage(event)" onmouseout="hoverImage(event)">SPT 3</div> </div> <div class="hover_img" id="hover_img"> <img src="https://picsum.photos/200/300"> </div>
problem is on element selector you need to select image element for change display property not div tag.问题出在元素选择器上,您需要 select 图像元素来更改显示属性而不是 div 标签。
i hope it's help full我希望它对你有帮助
Blockquote块引用
function hoverImage() { var x = document.getElementById("hover_img"); x.children[0].style.display = "block"; }
.hover_img { position:relative; height: 100%; width:100%; z-index: 99; }.hover_img img { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); display:none; }
<div id="object1"> <div class="title" onmouseover="hoverImage(event)">SPT 3</div> </div> <div class="hover_img" id="hover_img"> <img src="img/sptgoal.PNG"> </div>
you must select another id for img:你必须 select img 的另一个 id:
<div class="hover_img" id="hover_img">
<img src="img/sptgoal.PNG" id="image1">
</div>
and use this id in your java script:并在您的 java 脚本中使用此 ID:
function hoverImage() {
var x = document.getElementById("image1");
x.style.display = "block";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.