代码之家  ›  专栏  ›  技术社区  ›  archvist

阻止绝对div溢出父级

  •  -2
  • archvist  · 技术社区  · 8 年前

    .hotspot-wrp . 目前,弹出窗口流到可视区域之外,而在移动设备上,内容是不可见的。

    .hotspot-wrp {
      display: block;
      position: relative;
      background: #636e72;
      width: 80vw;
      margin: 0px auto;
      height: 200px;
    }
    
    .hot-spot {
      width: 24px;
      height: 24px;
      border-radius: 50%;
      background: rgba(227, 193, 122, 0.8);
      position: absolute;
      cursor: pointer;
      transition: all ease 350ms;
      margin-left: -12px;
      margin-top: -12px;
    }
    
    .tooltip {
      position: absolute;
      background: #fff;
      border: 1px solid #E8C378;
      width: 100%;
      padding: 15px;
      display: none;
      margin-top: 30px;
      -webkit-transform: translateX(0%);
      transform: translateX(0%);
      z-index: 9999;
    }
    
    .hot-spot:hover .tooltip,
    .hot-spot:active .tooltip {
      display: block;
    }
    <div class="hotspot-wrp">
      <div class="hot-spot" style="top:43.8776%; left:4.4476%;">
        <div class="tooltip">
          <h3>Test</h3>
          <p>Some demo text will be in here.</p>
        </div>
      </div>
      <div class="hot-spot" style="top:4.0816%; left:63.4146%;">
        <div class="tooltip">
          <h3>Test</h3>
          <p>Some demo text will be in here.</p>
        </div>
      </div>
    </div>

    http://jsfiddle.net/L7ksfdmc/

    2 回复  |  直到 8 年前
        1
  •  0
  •   Emil S. Jørgensen    8 年前

    添加 overflow: hidden; 到父容器。

    .hotspot-wrp {
      display: block;
      position: relative;
      background: #636e72;
      width: 80vw;
      margin: 0px auto;
      height: 200px;
      overflow: hidden;
    }
    
    .hot-spot {
      width: 24px;
      height: 24px;
      border-radius: 50%;
      background: rgba(227, 193, 122, 0.8);
      position: absolute;
      cursor: pointer;
      transition: all ease 350ms;
      margin-left: -12px;
      margin-top: -12px;
    }
    
    .tooltip {
      position: absolute;
      background: #fff;
      border: 1px solid #E8C378;
      width: 100%;
      padding: 15px;
      display: none;
      margin-top: 30px;
      -webkit-transform: translateX(0%);
      transform: translateX(0%);
      z-index: 9999;
    }
    
    .hot-spot:hover .tooltip,
    .hot-spot:active .tooltip {
      display: block;
    }
    <div class="hotspot-wrp">
      <div class="hot-spot" style="top:43.8776%; left:4.4476%;">
        <div class="tooltip">
          <h3>Test</h3>
          <p>Some demo text will be in here.</p>
        </div>
      </div>
      <div class="hot-spot" style="top:4.0816%; left:63.4146%;">
        <div class="tooltip">
          <h3>Test</h3>
          <p>Some demo text will be in here.</p>
        </div>
      </div>
    </div>
        2
  •  -1
  •   pankaj pant    8 年前

    就像埃米尔说的那样 overflow:hidden 要使工具提示为全宽,请选择所需的长度(以像素为单位)以增加宽度。如果你使用 width:100% 在.tootlipcss中,它将只获取其父div的宽度。所以根据需要使用绝对长度或工具提示中的文本长度。