在鼠标悬停处隐藏图像
Hide the image onmouseover
我希望iframe出现,但图像继续显示如果这有道理的话?我更喜欢在javasript 中完成
<html>
<head>
<script language="JavaScript" type="text/javascript">
function makeFrame(src) {
ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src","http://www.google.com");
ifrm.style.display = "block";
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
document.body.appendChild(ifrm);
}
function closeFrame(){
ifrm.style.display = "none";
}
</script>
</head>
<body>
<img src="images/largepic.jpg" onmouseover=makeFrame() onmouseout=closeFrame() height="100px" width="100px" />
</body>
</html>
`
我建议:
function makeFrame(src) {
ifrm = document.createElement("iframe");
ifrm.setAttribute("src","http://www.google.com");
ifrm.style.display = "block";
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
document.body.replaceChild(ifrm, document.getElementsByTagName('img')[0]);
}
尽管我建议,在这种情况下,给img
元素一个id
或以其他方式唯一引用它),以便准确地知道要删除哪个img
元素。
相反,如果您只是想在插入iframe
后隐藏图像:
function makeFrame(src) {
ifrm = document.createElement("iframe");
ifrm.setAttribute("src","http://www.google.com");
ifrm.style.display = "block";
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
document.body.insertBefore(ifrm, document.getElementsByTagName('img')[0]);
}
使用CSS:
iframe + img {
display: none;
}
您还可以将"this"参数传递到closeFrame函数调用中,该函数将为您提供图像元素。然后,您可以在该元素上设置display: none
:
<html>
<head>
<script language="JavaScript" type="text/javascript">
function makeFrame(image_element) { <!-- Take the image_element as a parameter -->
image_element.style.display = "none"; <!-- Set image element to not display -->
ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src","http://www.google.com");
ifrm.style.display = "block";
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
document.body.appendChild(ifrm);
}
function closeFrame(){
ifrm.style.display = "none";
}
</script>
</head>
<body>
<!-- Pass this into makeFrame where this is the image element -->
<img src="images/largepic.jpg" onmouseover=makeFrame(this) onmouseout=closeFrame() height="100px" width="100px" />
</body>
</html>
正如Shmiddty所说,一旦将display: none
设置为,就会调用closeframe()
。您可能希望在iframe本身而不是img元素上设置onmouseout事件。
根据OP的评论更新我的答案:
这是将鼠标移出IFrame时关闭IFrame所需的代码。这也可以恢复原始图像。
<html>
<head>
<script language="JavaScript" type="text/javascript">
function makeFrame(image_element) { <!-- Take the image_element as a parameter. -->
image_element.style.display = "none"; <!-- Set image element to not display. -->
ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src","http://www.google.com");
ifrm.style.display = "block";
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
ifrm.onmouseout=closeFrame; <!-- Close the IFrame when the mouse moves out of it. -->
document.body.appendChild(ifrm);
}
function closeFrame(){
ifrm.style.display = "none";
var image_element = document.getElementById("myImg"); <!-- Show the image that was hidden when showing the iframe. -->
image_element.style.display = "inline";
}
</script>
</head>
<body>
<!-- Pass this into makeFrame where this is the image element -->
<img id="myImg" src="images/largepic.jpg" onmouseover=makeFrame(this) height="100px" width="100px" />
</body>
</html>
这是通过将onmouseout
事件绑定到IFrame来实现的。这告诉浏览器在将鼠标移出IFrame时调用closeFrame
函数。在closeFrame
函数中,我们将恢复以前隐藏的图像。
相关文章:
- mouseOver上的隐藏图像问题
- 如何在运行时使用javascript隐藏图像
- 在鼠标悬停处隐藏图像
- 如果没有历史记录,我如何隐藏图像或按钮onclick=;历史.go(-1);
- ImageMapster可以动态显示和隐藏图像选择崩溃
- 当锚点HREF源不存在时隐藏图像
- jQuery UI选项卡基于活动选项卡显示/隐藏图像
- 砖石隐藏图像
- 如何在提交空白表单时隐藏图像
- 循环后如何删除/隐藏图像
- 如果没有src,则使用jquery隐藏图像
- 如何使jquery显示和隐藏图像
- 砌体隐藏图像,直到完全加载
- 在 HTML 表格中显示/隐藏图像
- 点击显示/隐藏图像.没有Javascript
- 使用 jquery 过滤后正确隐藏图像
- 需要按钮显示/隐藏图像旋转 href 添加到 javascript 代码中
- 使用图像滚动达到页面中的最大内容,然后应该使用窗口百分比隐藏图像
- 使用jQuery随机显示/隐藏图像
- 如果没有链接,如何自动隐藏图像