我想在 JavaScript 中将鼠标移到图像上时显示文本
I want to display text when I move mouse over image in JavaScript
我想在
JavaScript中将鼠标移到图像上时显示文本。我不知道我要去哪里,因为我是新手。有一个表格,其中包含我想在将鼠标移到图像上时显示其内容的图像。我想在这里使用 JavaScript。
<script>
function show()
{
var welcome = document.getElementById('sub1');
welcome.style.display = 'block';
}
function hide()
{
var welcome = document.getElementById('sub1');
welcome.style.display = 'none';
}
</script>
<div id="sub1" onmouseover="show();" onmouseout="hide();"> </div>
//This is the image where I want to display a text.
<td><img border="0" src="images/img1.jpg"
alt="iphone 5s" width="304" height="228"></td>
你可以只使用 css
而不是使用 javascript: codepen: 当我悬停时显示文本 - css
.HTML
<div class="thumb-item">
<img class="thumb-item__image" src="http://clapat.ro/themes/legrand-wordpress/wp-content/uploads/2015/11/2.jpg" alt="">
<div class="thumb-item__info">
<h5>Title here</h5>
<p>This Is a caption Line</p>
</div>
</div>
.CSS
.thumb-item {
width: 400px;
position: relative;
overflow: hidden;
}
.thumb-item__image {
width: 100%;
}
.thumb-item__info {
position: absolute;
bottom: -30px;
left: 0;
width: 100%;
opacity: 0;
box-sizing: border-box;
color: #fff;
padding: 10px 20px;
}
.thumb-item__info h5 {
font-size: 16px;
margin: 0;
padding: 0;
font-family: tahoma;
font-weight: bold;
}
.thumb-item__info p {
font-size: 12px;
margin: 4px 0 14px;
}
.thumb-item:hover .thumb-item__info {
opacity: 1;
bottom: -5px;
}
您想用图像替换文本还是将文本放在其他地方?据我了解,请检查以下代码:
<script>
function showText(text){
document.getElementById("text").innerHTML=text;
}
function hide(){
document.getElementById("text").innerHTML="";
}
</script>
<img src="image.jpg" id="image" onMouseOver="showText('Some Text')" onMouseOut="hide();">
<div id="text"></div>
要在图像上写入任何文本,甚至添加图标或类似的东西,您必须在图像标签之前创建一个div,通常称为布局div。
<td><div class="layout"></div><img id="testImg" src="Your img path"></td>
注意:我们在放置图像标签之前关闭了div。您的 CSS 将是 :
background:rgba(7,7,7,0.2);
position:absolute;
top:0;
bottom:0;
right:0;
left:0;
display:none
根据需要更改背景颜色和不透明度..j查询代码 :
$("#testImg").click(function () {
$(".layout").css("display", "block");
$(".layout").text('Test Paragraph');
});
你已经相当接近了。我认为您的代码现在的问题是您的具有"onmouseover"和"onmouseout"的div非常小,因为它没有任何内容。 此外,某些浏览器不会像您那样对内联创建的此事件做出反应。此外,块样式在某些浏览器中可能存在一些样式问题。 您可能不希望这样做,而只是想删除显示的"无"。 如果可以的话,我也会指出你 JQuery,但由于你没有提到 JQuery,下面就可以了。 在这里,它也在JSFiddle中供您查看:
https://jsfiddle.net/nkxjphjm/1/
<script>
function show() {
var welcome = document.getElementById('sub1');
welcome.style.display = '';
}
function hide() {
var welcome = document.getElementById('sub1');
welcome.style.display = 'none';
}
function init() {
var surround = document.getElementById('surroundDiv');
surround.onmouseover = show;
surround.onmouseout=hide;
}
window.onload = init;
</script>
<div id="surroundDiv">
<img border="0" src="images/img1.jpg"
alt="iphone 5s" width="304" height="228">
</div>
<div id="sub1" style="display:none">Text to display</div>
现在,当他们将鼠标悬停在图像上时,他们也将越过div。 这将触发显示包含文本的内部div "sub1" 的操作。您还可以修改里面的文本等。 选择是无穷无尽的。 我希望这能让你开始。 如果是这样,请接受答案。 如果您需要更多帮助,请发表评论,我可以更新我的答案。 谢谢!
相关文章:
- 将HTML表格导出到带有文本和图像的excel中
- 可单击滚动图像以显示文本框
- 正确输入的文本会更改图像
- 无法将包含文本和图像的SVG保存到画布
- 如何知道锚在文本或图像上
- 自动缩放图像以匹配文本高度
- 一个javascript实现base64图像编码并将结果写入文本文件
- 有人知道有没有jquery插件可以在图像上写文本,并让用户将其放置在图像内的任何位置
- jQuery对请求图像的外部文本进行查询
- 如何检测移动浏览器(PHP/Javascript),然后进行图像/文本更改
- ASP.NET网页-使用脚本显示由预定义日期和时间指定的图像/文本
- 合并 2 个 javascript/jquery 随机横幅图像/文本函数彼此对应
- 使用javascript在html中制作一个随机的图像/文本/url元素
- 将彩色带置于图像/标签+图像/文本对齐方式之上->JavaScript Titanium移动应用程序
- 将2D画布(图像+文本)应用于3D表面,并进行360度旋转/动画-可以使用HTML5/CSS3/Javascript或F
- 覆盖HTML 5输入类型文件,相机与图像/文本从HTML网页
- 脚本标记中的Paperclip图像集显示的是图像文本url,而不是图像-expandablebanners.js
- 图像文本上的代码后面
- 电子邮件中包含图像/文本/插图的HTML滑块
- 如何从不同的媒体子集创建视频,如图像/文本等