首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在将鼠标悬停在图像上时显示div

尝试在将鼠标悬停在图像上时显示div
EN

Stack Overflow用户
提问于 2019-10-02 03:00:24
回答 3查看 187关注 0票数 0

当我将鼠标悬停在图像上时,我试图使一个包含图像相关信息的div出现,我的代码似乎无法工作。

HTML:

代码语言:javascript
复制
< div class="batmobile-info">This is Batman's batmobile, he uses it to chase his enemies or to flee from the police.< /div>
 < img class="batmobile" src="images/batmobile.png" alt="batmobile" >

CSS:

代码语言:javascript
复制
.batmobile{
position:relative;
width:500px;
height:200px;
top:350px;
left:200px;
}

.batmobile-info{
visibility:hidden;
}

.batmobile:hover .batmobile-info{
background-color:white;
text-align:center;
width:290px;
height:40px;
position:absolute;
top:500px;
left:700px;
visibility: visible;
}
EN

回答 3

Stack Overflow用户

发布于 2019-10-02 03:26:10

如果您颠倒了元素的顺序,那么img将出现在div之前,从而为您提供:

代码语言:javascript
复制
<img class="batmobile" src="images/batmobile.png" alt="batmobile">
<div class="batmobile-info">This is Batman's batmobile, he uses it to chase his enemies or to flee from the police.</div>

然后,您可以使用以下CSS来显示悬停时的div

代码语言:javascript
复制
.batmobile:hover + .batmobile-info {
  visibility: visible;
}

+是一个adjacent sibling selector --一个非常有用的选择器!

票数 0
EN

Stack Overflow用户

发布于 2019-10-02 03:34:07

要做到这一点,一种方法是使用容器,您将在其中放置带有信息的imgdiv

我使用opacity而不是visibility,因为opactity是可以在转换中使用的属性。

代码语言:javascript
复制
* { 
  box-sizing: border-box;
}
html, body {
  margin: 0;
}
/* contiainer to center my content, not relevant to question*/
.main-container {
  margin-top: 2rem;
  height: calc(100vh - 2rem);
  display: flex;
  align-items: center;
  justify-content: space-around;
}
/* image and info container */
.img-container {
  display: inline-block;
  position: relative;
  border: 2px solid #333;
}
/* style for div with information */
.img-info {
  position: absolute;
  background-color: rgba(0, 0, 0, 0.8);
  color: #eee;
  transition: opacity .3s ease-in-out;
  opacity: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: .5rem;
}
/* information positioning styles */
.img-info-overlay {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.img-info-above {
  left: -2px;
  top: -2rem;
  height: 2rem;
  width: calc(100% + 4px);
}
/* this part show information on hover */
.img-container:hover .img-info {
  opacity: 1;
}
代码语言:javascript
复制
<div class="main-container">
  <div class="img-container">
    <img src="https://dummyimage.com/250x100/fff/aaa" alt="Filler image">
    <div class="img-info img-info-overlay">Some info about this image</div>
  </div>
  <div class="img-container">
    <img src="https://dummyimage.com/250x100/fff/aaa" alt="Filler image">
    <div class="img-info img-info-above">Some info about this image</div>
  </div>
</div>

票数 0
EN

Stack Overflow用户

发布于 2019-10-02 03:41:52

问题出在HTML和CSS中,您需要将图像和文本放在一个父块中,并从该块开始设置样式。我会避免绝对定位,也会尝试使用opacity CSS property,以防您需要为文本块.batmobile:hover状态添加一些额外的过渡效果。检出代码并运行代码片段:

代码语言:javascript
复制
body {background:#ccc}

.batmobile {
  position: relative;
  width: 500px;
  height: 200px
}

.batmobile div p {
  visibility: hidden;
  background-color: white;
  width: 300px;
  height: 40px;
  margin: 0px auto; /* Think you wanted to center the block as width is less than parent */
  text-align: center /*In case you need to center text only */
}

.batmobile:hover div p {
  visibility: visible;
}

.batmobile img {width: 100%}
代码语言:javascript
复制
<div class="batmobile">
 
  <div>
  <p>An excelent drone - Ryze Tello with price under $100</p>
  </div>

  <img src="https://bestdroneforkids.com/wp-content/uploads/2019/09/ryze-tello-drone-forkids.jpg" alt="Ryze tello drone in the air">

</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58190775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档