【问题标题】:Have parent and child div. Want my tooltip to drop from child div. It drops when I hover parent div有父子div。希望我的工具提示从子 div 中删除。当我悬停父 div 时它会下降
【发布时间】:2019-05-18 15:38:53
【问题描述】:

图像是父 div,子 div 位于一个小的 图像的一部分(父)。孩子是我要悬停的div 并让工具提示下拉,但是当我悬停时工具提示会下拉 图像(父)。我不会真正使用 HTML 和 css,所以请原谅 任何新手的错误。我找到了工具提示的大部分代码 各种网站。我实在是太过分了!!!

我从网上下载了 sn-ps。试图把 在一起,只是经验不够。

/* I do not understand the css, but after several
       weeks of research this is what I have. The Image
       seems to be placed correctly and is the right size.
    */

body {
  text-align: center;
}

.tooltip {
  position: relative;
  display: inline-block;
  border-bottom: none;
}


/* The tooltip is in fact hidden.*/

.tooltip .tooltiptext {
  visibility: hidden;
  width: 290px;
  background-color: #ffcc99;
  color: #000;
  text-align: left;
  border-radius: 6px;
  padding: 5px 0;
  margin-left: 10px;
  /* Position the tooltip */
  position: absolute;
  z-index: 1;
  top: 0px;
  left: 110%;
}


/* Tooltip becomes visible when hover
         over the big Image. */

.tooltip:hover .tooltiptext {
  visibility: visible;
}


/* I added this to the sample I downloaded. It
         sizes the Image correctly, and locates it
         where I want it on the Webpage.
    */

#container {
  position: absolute;
  width: 330px;
  height: 240px;
  border: 5px, #000;
  border-style: solid;
  margin: auto;
  margin-top: 10px;
  left: 40px;
  top: 20px;
}

#container img {
  width: 100%;
  height: 100%;
}


/* This creates the child div. I have located it
         over the area on the Image where I want the
         tooltip to become visible. I put a border 
         around the child so I can see where it is.
    */

.dropdown {
  position: absolute;
  width: 226px;
  height: 23px;
  border: 1px, #000000;
  border-style: solid;
  background: transparent;
  left: 45px;
  top: 15px;
}
/* The below code is where I am really not sure how to use. The tooltip drops down when I hover the Image. I cannot make it so that it only drops when I hover the child. I will send a screenshot if I can figure out how to do so. */
<h2>The Best Tooltip</h2>
<p>Move the mouse over the Image below:</p>
<div class="tooltip"> <img src="MMDVM_Pic.jpg" alt="#">
  <div class="dropdown">
    <span class="tooltiptext"><b><center>More Information About Date and 
        Time</center></b>
      <p style="margin-left:10px; margin-right:10px">This shows the <font 
        color=”#050778A”;><b>Date and Time </b></font>as reported by <font 
        color=”#ff0000”;><b>pistar.</b></font> There are several <font 
        color=”#E0E614”;>formats</font> supported.</p>
    </div>
    </div>

【问题讨论】:

  • 您显示的 HTML 存在有效性问题(未关闭 span,错误关闭 div 元素)。
  • 您正在对父级应用 :hover 效果,而不是对子级
  • Alon,请注意,当您悬停子项(黑色矩形)时,工具提示变为可见。当您将鼠标悬停在父级(小图标)时,它也会变得可见。阻止它从父悬停下拉是问题所在。谢谢。
  • @MitchSavage 然后将&lt;div&gt; 下拉菜单作为悬停的父级,而不是将其应用于父级并期望子级获得事件,除非您希望在 JavaScript 中得到答案

标签: html css


【解决方案1】:

首先,最好删除内联样式并将其添加到您的 CSS 中。然后我会建议使用相对单位而不是像素单位(20% vs 100px),以便您的网站能够响应。

要回答你的问题,这是罪魁祸首:

.tooltip:hover .tooltiptext {
  visibility: visible;
}

现在,当您将鼠标悬停在 .tooltip(即父级)上时,它就会激活。您希望在将鼠标悬停在 .tooltiptext 上时激活它。这样代码就变成了:

.tooltip .tooltiptext:hover {
  visibility: visible;
}

【讨论】:

  • 乍得,当我更改为 .tooltip .tooltiptext:hover 时,它不会从父级或子级下拉。当我改回 .tooltipHhover .tooltiptext 时,它当然会悬停在父级上。这是否表明 .tooltiptxt 可能有问题?
  • 您知道吗,您可能无法将鼠标悬停在孩子身上,因为它是隐藏的。我会将visibility:hidden 替换为opacity: 0,将visibility: visible 替换为opacity: 1。 dom 中仍然会列出透明元素,但不会列出隐藏元素。
  • 乍得,当我更改为不透明度时,没有悬停在任何东西上。,返回它仍然悬停在父级上。
  • 你能制作并发布一个包含所有代码的 codepen 或 jsfiddle 以便我们验证吗?
  • 乍得,以前从未使用过 stackoverflow、codepen 或 jsfiddle。我现在就开始,看看我能否让其中一项为我们工作。谢谢,米奇
【解决方案2】:

您应该考虑开始养成使用正确标识组织代码的习惯,这样您就不会迷失在自己的代码中。

看看是不是你想要的:

document.getElementById("sibling").addEventListener("mouseover", function(){
  document.getElementById("child").style.visibility = "visible";
});
document.getElementById("sibling").addEventListener("mouseout", function(){
  document.getElementById("child").style.visibility = "hidden";
});
* {
  margin: 0;
  padding: 0;
  text-align: center;
}

.parent {
  position: relative;
  display: inline-block;
  width: 400px;
  height: 250px;
  padding-top: 30px;
  background-color: silver;
}

.sibling {
  background-color: grey;
  width: 50%;
  height: 100px;
  margin: 0 auto;
  text-align: center;
  vertical-align: middle;
  line-height: 100px;
}

.child {
  visibility: hidden;
  width: 50%;
  height: 100px;
  background-color: yellow;
  margin: 0 auto;
  text-align: center;
  vertical-align: middle;
  line-height: 100px;
}
<div class="parent">
  I'm the parent DIV !
  <div class="sibling" id="sibling">I'm the sibling DIV, hover me !</div>
  <div class="child" id="child">Here it goes an example !</div>
</div>

【讨论】:

  • Matheus,用我的图片试试你的例子。仍然落在父母身上。子 div 消失了。我们必须使用类下拉菜单来创建孩子。
  • 那么你必须在 JavaScript 上调整它来处理从父类到子类的转换
  • Matheus,您发布了一个很好的示例,但它显示了关于 parent 和 child 的工具提示...谢谢...Mitch
  • 所以更清楚一点,您希望在将鼠标悬停在父级上并丢失父级 div 的工具提示并将其应用于子级 div 之后创建子级,对吗?
  • Matheus,我不想在父母身上出现工具提示,只在孩子身上。我正在尝试学习如何只用一个孩子来做到这一点。最终,我将在图像上有 17 个子
    ,每个都有不同的工具提示。我现在用 来做这件事。现在无法设置我的工具提示的样式。看看jsfiddle.net/jLxpk5ay 看看我在做什么。左侧带有黑色边框的 div 应该是父级(图像)的子级。稍后我会将孩子移回图像上。问题是当任何一个悬停时,下拉菜单都会出现在父母和孩子身上。只有孩子的下拉菜单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
  • 2020-09-06
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多