【问题标题】:Hover issue on div, Horizontal Scroll is coming on last divdiv上的悬停问题,水平滚动出现在最后一个div上
【发布时间】:2021-02-06 13:58:31
【问题描述】:

我连续有多个块,每个块都有自定义弹出框 div。如果用户连续悬停在最后一个 div 或倒数第二个 div 上,则 popover 超出了窗口宽度。我想,如果右侧没有空间用于弹出框,则 tile-box 类更改为 tile-box1。提前致谢! 下面是代码笔 URL 供参考,我还附上了图片。

.content {
  display: flex;
  flex-wrap: wrap;
}

.box {
  width: 150px;
  height: 100px;
  position: relative;
  border: 1px solid #ddd;
}

.box:hover .tile-box {
  display: block;
  ;
}

.tile-box {
  display: none;
  position: absolute !important;
  z-index: 9;
  top: -6px;
  left: 169px;
  width: 220px;
  height: auto;
  /* padding: 15px; */
  margin-top: 15px;
  position: relative;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.06);
  border: solid 1px #e8e8e8;
  /* -webkit-transition: all 0.5s ease;
        -moz-transition: all 0.5s ease;
        -o-transition: all 0.5s ease;
        transition: all 0.5s ease; */
}

.tile-box {
  padding: 15px !important;
  /* display: block !important; */
}

.tile-box::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  top: 32px;
  left: 0px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-width: 8px;
  border-style: solid;
  border-color: transparent transparent white white;
  -webkit-transform-origin: 0 0;
  -webkit-transform: rotate(45deg);
  -ms-transform-origin: 0 0;
  transform-origin: 0 0;
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  -webkit-box-shadow: -3px 2px 2px rgba(0, 0, 0, 0.05);
  box-shadow: -3px 2px 2px rgba(0, 0, 0, 0.05);
}
.tile-box1{
display:block;
}
<div class="content">
  <div class="box">
    <h1>box 1</h1>

    <div class="tile-box">
      <p>lorem ipsum dolor</p>
    </div>
  </div>
  <div class="box">
    <h1>box 2</h1>
    <div class="tile-box">
      <p>lorem ipsum dolor</p>
    </div>
  </div>
  <div class="box">
    <h1>box 3</h1>
    <div class="tile-box">
      <p>lorem ipsum dolor</p>
    </div>
  </div>
  <div class="box">
    <h1>box 4</h1>
    <div class="tile-box">
      <p>lorem ipsum dolor</p>
    </div>
  </div>
</div>

Codepen 网址:(https://codepen.io/uibeast/pen/yLJgJra)

参考图片现在的表现

【问题讨论】:

  • 认为你需要 js 来计算你是否有足够的悬停空间
  • 如果有,请分享示例代码。提前致谢。

标签: javascript html css hover mousehover


【解决方案1】:

这里有一些 Javascript 可以帮助检测图块何时越界。目前我只包含了一个虚拟控制台日志,您应该能够切换类名或其他东西而不是控制台日志。

基本上,代码所做的是获取图块最右侧部分的 x 坐标,并将其与 window.innerHeight 进行比较。如果前者较大,则该图块超出了窗口的右侧。

$(".box").on("mouseenter", function() {
  var tile = $(this).find(".tile-box")[0];
  var rect = tile.getBoundingClientRect();
  
  //if right of tile is beyond viewport
  if(rect.right > window.innerWidth) {
    //dummy console log for demo
    console.log("doesn't fit!");
    //switch class of tile or something
  }
})

演示:

$(".box").on("mouseenter", function() {
  var tile = $(this).find(".tile-box")[0];
  var rect = tile.getBoundingClientRect();
  
  //if right of tile is beyond viewport
  if(rect.right > window.innerWidth) {
    //dummy console log for demo
    console.log("doesn't fit!");
    //switch class of tile or something
  }
})
.content {
  display: flex;
  flex-wrap: wrap;
}

.box {
  width: 150px;
  height: 100px;
  position: relative;
  border: 1px solid #ddd;
}

.box:hover .tile-box {
  display: block;
  ;
}

.tile-box {
  display: none;
  position: absolute !important;
  z-index: 9;
  top: -6px;
  left: 169px;
  width: 220px;
  height: auto;
  /* padding: 15px; */
  margin-top: 15px;
  position: relative;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.06);
  border: solid 1px #e8e8e8;
  /* -webkit-transition: all 0.5s ease;
        -moz-transition: all 0.5s ease;
        -o-transition: all 0.5s ease;
        transition: all 0.5s ease; */
}

.tile-box {
  padding: 15px !important;
  /* display: block !important; */
}

.tile-box::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  top: 32px;
  left: 0px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-width: 8px;
  border-style: solid;
  border-color: transparent transparent white white;
  -webkit-transform-origin: 0 0;
  -webkit-transform: rotate(45deg);
  -ms-transform-origin: 0 0;
  transform-origin: 0 0;
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  -webkit-box-shadow: -3px 2px 2px rgba(0, 0, 0, 0.05);
  box-shadow: -3px 2px 2px rgba(0, 0, 0, 0.05);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
  <div class="box">
    <h1>box 1</h1>

    <div class="tile-box">
      <p>lorem ipsum dolor</p>
    </div>
  </div>
  <div class="box">
    <h1>box 2</h1>
    <div class="tile-box">
      <p>lorem ipsum dolor</p>
    </div>
  </div>
  <div class="box">
    <h1>box 3</h1>
    <div class="tile-box">
      <p>lorem ipsum dolor</p>
    </div>
  </div>
  <div class="box">
    <h1>box 4</h1>
    <div class="tile-box">
      <p>lorem ipsum dolor</p>
    </div>
  </div>
</div>

【讨论】:

  • 我添加了一个名为 tile-box1 的 div,并编写了代码来显示 tile-box1 div 并隐藏 tile-box 来代替 console.log。在最后一个 div 上悬停后,tile-box 被隐藏并显示 tile-box1。因此,没有显示任何 div 的 tile-box。我做错什么了吗?
猜你喜欢
  • 2011-05-18
  • 2023-03-16
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 2011-11-13
相关资源
最近更新 更多