CSS hover card positioning

后端 未结 2 761
礼貌的吻别
礼貌的吻别 2021-02-04 02:13

I am trying to make a hover card with css. I have one question about position of the page down.

I have created this DEMO page from codepen.

相关标签:
2条回答
  • 2021-02-04 02:31

    EDIT

    I have made a jQuery plugin that addresses this issues, repositions the tooltip to stay inside window, simple & responsive. You can see it in action here tipso

    I forked your codepen and reworked it on codepen

    I guess this is what you are looking for :)

    $('.hub').on({
      mouseenter: function() {
        $(this).addClass('zIndex');
    
        var top, left,
          toolTipWidth = 250,
          toolTipHeight = 120,
          arrowHeight = 15,
          elementHeight = $(this).height(),
          elementWidth = $(this).width(),
          documentHeight = $(window).height(),
          bounding = $(this)[0].getBoundingClientRect(),
          topHub = bounding.top;
    
    
        if (topHub < topHub + toolTipHeight && topHub + toolTipHeight + arrowHeight + elementHeight <= documentHeight) {
    
          $('.bubble').addClass('top');
          top = elementHeight + arrowHeight;
          left = -(elementWidth / 2);
    
        }
    
        if (topHub + toolTipHeight + arrowHeight + elementHeight >= documentHeight) {
          $('.bubble').addClass('bottom');
          top = -toolTipHeight - arrowHeight;
          left = -(elementWidth / 2);
        }
    
    
        $('.bubble').css({
          'top': top,
          'left': left
        });
      },
      mouseleave: function() {
        $('.bubble').removeClass('top bottom');
        $(this).removeClass('zIndex');
      }
    });
    
    0 讨论(0)
  • 2021-02-04 02:42

    I found a solution in the following way.

    Working DEMO but i couldn't make it with window. If anyone can do it with window please answer me...

        $(document).ready(function () {
            $('.hub').mouseover(function () {
                var elementHeight = $(this).height();
                var offsetWidth = -250;
                var offsetHeight = 25;
                var toolTipWidth = $(".bubble").width();
                var toolTipHeight = $(".bubble").height();
                var documentWidth = $(document).width();
                var documentHeight = $(document).height();
                var top = $(this).offset().top;
    
                if (top + toolTipHeight > documentHeight) {                   
                    top = documentHeight - toolTipHeight - offsetHeight - (2 * elementHeight);
                }
                var left = $(this).offset().left + offsetWidth;
                if (left + toolTipWidth > documentWidth) {                      
                    left = documentWidth - toolTipWidth - (2 * offsetWidth);
                }                    
                $('.bubble').css({ 'top': top, 'left': left });
            });
        });
    
    0 讨论(0)
提交回复
热议问题