如何使工具提示溢出并在溢出:隐藏的父容器外部正确显示?

问题描述 投票:0回答:1

我有一个 div 元素的嵌套结构,其中最里面的子元素包含一个工具提示。父容器(父类)具有 CSS 属性

overflow: hidden
。但是,当将鼠标悬停在工具提示上时,它会被剪切并且不会溢出到父级的边界之外。这是结构的简化版本:

<div class="parent_class">
  <div class="child_level_one">
    <div class="child_level_two">
      <div class="child_level_three">
        <div class="child_level_four">
          <div class="tooltip">
            <p>tooltip text</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

document.querySelectorAll(".tooltip_container").forEach((tooltip) => {
  const tooltipText = tooltip.getAttribute("data-tooltip");
  const tooltipContent = document.createElement("div");
  tooltipContent.className = "tooltip_content";
  tooltipContent.innerText = tooltipText;

  tooltip.appendChild(tooltipContent);

});
.parent_class {
  position: relative;
  width: 300px;
  height: 100px;
  margin: 50px;
  overflow: auto;
  border: 2px solid #ccc;
  padding: 20px;
  background-color: #f9f9f9;
}

.child_level_four {
  position: relative;
  margin-top: 0px;
}


.tooltip_container {
  position: relative;
  display: inline-block;
}

.tooltip_content {
  position: absolute;
  bottom: 100%;
  left: -50%;
  transform: translateX(-50%);
  background-color: #333;
  color: #fff;
  padding: 8px;
  border-radius: 4px;
  opacity: 0;
  visibility: hidden;
  white-space: nowrap;
  transition: all 0.3s ease-in-out;
  z-index: 1000;
}

.tooltip_content::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border-width: 5px;
  border-style: solid;
  border-color: #333 transparent transparent transparent;
}

.tooltip_container:hover .tooltip_content {
  opacity: 1;
  visibility: visible;
  transform: translateY(-18%);
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Pure JS Tooltip</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="parent_class">
    <div class="child_level_one">
      <div class="child_level_two">
        <div class="child_level_three">
          <div class="child_level_four">
            <div class="tooltip_container" data-tooltip="Overflow Tooltip Text">
              <button>Hover Me</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="script.js"></script>
</body>
</html>

html css tooltip overflow
1个回答
0
投票

删除/更改溢出自动

.parent_class {
    position: relative;
    width: 300px;
    height: 100px;
    margin: 50px;
    /* overflow: auto; */
    border: 2px solid #ccc;
    padding: 20px;
    background-color: #f9f9f9;
© www.soinside.com 2019 - 2024. All rights reserved.