Hover issue on div, Horizontal Scroll is coming on last div

Viewed 53

I have multiple blocks in a row, each block have custom popover div. If the user hovers on last div or second last div in a row then popover is going beyond the window width. I want, if there is no space on the right side for popover, then tile-box class change to tile-box1. Thanks in advance! Below is the code pen URL for the reference and i am also attaching the image.

.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 Url: (https://codepen.io/uibeast/pen/yLJgJra)

reference image how it is behaving nowenter image description here

1 Answers

Here is a bit of Javascript that should help detect when the tile is out of bounds. For now I only included a dummy console log, you should be able to switch class names or something instead of the console log.

Basically, what the code does is get the x-coord of the right-most part of the tile, and comparing it with window.innerHeight. If the former is greater, then the tile is going beyond the right of the window.

$(".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
  }
})

Demo:

$(".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>

Related