admin管理员组

文章数量:1323317

I am learning how to create tooltips based on a w3schools tutorial.

One problem I am faced with is the visibility of the tooltips when the hovered-upon text is close to a browser edge and the tooltip is displayed outside of the visible area.

Is there a standard way to prevent this (any approch (CSS, JS, ...) would be wele)?
The idea I have right now is to calculate the coordinates of the tooltip depending on the size of the screen and the position of the tooltip anchor - but I would like to avoid reinventing the wheel.

A tooltip which expands to the top or to the bottom would be enough - maybe there is a "is pletely visible" check which can be done to make a decision on the direction? This would be similar to what is in place in tooltips in SO:

I am learning how to create tooltips based on a w3schools tutorial.

One problem I am faced with is the visibility of the tooltips when the hovered-upon text is close to a browser edge and the tooltip is displayed outside of the visible area.

Is there a standard way to prevent this (any approch (CSS, JS, ...) would be wele)?
The idea I have right now is to calculate the coordinates of the tooltip depending on the size of the screen and the position of the tooltip anchor - but I would like to avoid reinventing the wheel.

A tooltip which expands to the top or to the bottom would be enough - maybe there is a "is pletely visible" check which can be done to make a decision on the direction? This would be similar to what is in place in tooltips in SO:

Share edited Dec 1, 2016 at 8:05 WoJ asked Dec 1, 2016 at 8:00 WoJWoJ 30.1k58 gold badges213 silver badges403 bronze badges 7
  • 1 WoJ, guys here mostly hate W3Schools that maybe why you are downvoted ! – asdf_enel_hak Commented Dec 1, 2016 at 8:03
  • 1 @asdf_enel_hak Maybe, but they shouldn't downvote because of that... People should tell WoJ why they downvoted. – Sj03rs Commented Dec 1, 2016 at 8:14
  • I think you are jumping to conclusions about the downvote being due to the W3Schools mention, it's more likely that it is because the question is a bit broad for SO. @WoJ, I suggest you add code showing what you have done it'll probably get a warmer reaction. That said, I can't think of a CSS only way of achieving this, your ment about calculating the position and size of the tooltip with JavaScript is how I have tackled this in the past. – Hidden Hobbes Commented Dec 1, 2016 at 8:27
  • @HiddenHobbes: As opposed to the vast majority of my questions where I provide code which can be hacked (= I am trying to give a plete, working example), here I wanted to get an early advice before jumping into convoluted code and realizing after the fact that there is a simple flag / approach for the problem I am having. – WoJ Commented Dec 1, 2016 at 8:31
  • @HiddenHobbes How about no mercy in answers if w3schools is referenced instead of for example developer.mozilla – asdf_enel_hak Commented Dec 1, 2016 at 8:37
 |  Show 2 more ments

1 Answer 1

Reset to default 4

You need to calculate if the position of the tooltip will be outside the window. In the example below I just add class .bottom to the .tooltiptext in this case.

The calculation is:

$('.tooltip').offset().top - $('.tooltiptext').height() < 0

In this case we should add that class.

The full example:

$('.tooltip').on('mouseenter', function() {
  var $this = $(this);
  var tooltip = $(this).find('.tooltiptext');
  var offset = $this.offset();

  tooltip.toggleClass('bottom', offset.top - tooltip.height() < 0);
  // - just for case you want to change the default behavior - tooltip.toggleClass('top', $(window).height() + tooltip.height() < 0);
});
.footer {
  position: absolute;
  bottom: 0;
  text-align: center;
  padding-bottom: 10px;
  width: 100%;
}
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery./jquery-2.1.4.js"></script>

    <style>
      .tooltip {
        position: relative;
        display: inline-block;
        border-bottom: 1px dotted black;
      }

      .tooltip .tooltiptext {
        visibility: hidden;
        width: 120px;
        background-color: #555;
        color: #fff;
        text-align: center;
        border-radius: 6px;
        padding: 5px 0;
        position: absolute;
        z-index: 1;
        left: 50%;
        margin-left: -60px;
        opacity: 0;
        transition: opacity 1s;
        bottom: 125%;
      }

      .tooltip .tooltiptext.bottom {
        bottom: auto;
        top: 125%;
      }

      .tooltip .tooltiptext::after {
        content: "";
        position: absolute;
        top: 100%;
        left: 50%;
        margin-left: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: #555 transparent transparent transparent;
      }

      .tooltip:hover .tooltiptext {
        visibility: visible;
        opacity: 1;
      }
    </style>
  </head>
  <body style="text-align:center;">
    <div class="tooltip">Hover over me
      <span class="tooltiptext">Tooltip text</span>
    </div>

    <h2>Tooltip</h2>
    <p>Move the mouse over the text below:</p>

    <div class="footer">
      <div class="tooltip">Hover over me
        <span class="tooltiptext">Tooltip text</span>
      </div>
    </div>
  </body>
</html>

http://output.jsbin./nemebob

I let you to change the arrow direction to top.

本文标签: javascriptHow to ensure a tooltip stays within the browser visible areaStack Overflow