【发布时间】:2017-11-09 08:12:19
【问题描述】:
我是编码的初学者,我已经在这里搜索过,但我真的找不到解决问题的合适方法。
当我将鼠标悬停在图像上时,我试图让文本出现在图像的位置。 不幸的是,必须使用 jQuery,我知道它可以通过使用 CSS 来完成。
到目前为止,我在这里找到了以下内容:
在头脑中:
<script>
$(function(){
$('.parent').mouseenter(function(){
$(this).children('.child').fadeIn();
}).mouseleave(function(){
$(this).children('.child').fadeOut();
});
});
</script>
在体内:
<div class='parent'>
<img src='https://ichef.bbci.co.uk/news/660/cpsprodpb/37B5/production/_89716241_thinkstockphotos-523060154.jpg' alt='image'/>
<div class='child'>
<p>Random text.</p>
</div>
</div>
CSS:
.parent
{
position:relative;
}
.child
{
position: absolute;
left: 0;
bottom: 0;
background-color: black;
opacity: 0.5;
padding:10px;
display:none;
}
感谢您提供关于我做错了什么以及如何解决该问题的简单提示或解释。
编辑:
这是我的 PHP 文件中的完整代码:
echo "
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>Test Blog</title>
<script>
$(document).ready(function() {
$('.gallery-item').hover(function() {
$(this).find('.img-title').fadeIn(300);
}, function() {
$(this).find('.img-title').fadeOut(100);
});
});
</script>
</head>
<body>
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>
<div class=\"wrapper clearfix\">
<figure class=\"gallery-item\">
<img src='https://ichef.bbci.co.uk/news/660/cpsprodpb/37B5/production/_89716241_thinkstockphotos-523060154.jpg' alt='image'>
<figcaption class=\"img-title\">
<h5>Random text.</h5>
</figcaption>
</figure>
</div>
然后它继续通过下拉菜单路由到其他页面。 CSS 代码在我上面链接到的 CSS 文件中(链接是正确的,因为所有其他 CSS 代码都在工作)。
【问题讨论】:
标签: javascript jquery html css hover