2014-11-08 72 views
20

我試圖做一個懸停卡與css。我有一個關於頁面位置的問題。CSS懸停卡定位

我已經創建了這個DEMO從codepen.io頁面。所以,如果你在演示頁面的底部,那麼你會看到泡泡div顯示出來。

我該怎麼做才能在頁面下方的三角形底部顯示.bubbleenter image description here

.container{ 
    width:400px; 
    height:400px; 
    margin:0px auto; 
    margin-top:50px; 
} 
.bubble 
{ 
position: absolute; 
width: 250px; 
height: 120px; 
padding: 0px; 
background: #000; 
-webkit-border-radius: 2px; 
-moz-border-radius: 2px; 
border-radius: 2px; 
    display:none; 
} 

.bubble:after 
{ 
content: ''; 
position: absolute; 
border-style: solid; 
border-width: 0 15px 15px; 
border-color: #000 transparent; 
display: block; 
width: 0; 
z-index: 1; 
top: -15px; 
left: 194px; 
} 
.hub:hover .bubble{ 
    display:block; 
} 
.wrp{ 
    width:300px; 
    height:68px; 
} 
+3

我可能是錯的,但我認爲你將需要JavaScript來檢查邊界 – Timmerz 2014-11-08 21:50:16

+1

@Timmerz你是對的。但我找不到示例應用程序。 – innovation 2014-11-08 21:52:32

+0

你使用什麼瀏覽器?因爲在FF最新版本中,IT運行良好。嘗試你的DEMO在整個頁面:http://codepen.io/shadowman86/full/mCIkt – dippas 2014-11-08 21:57:14

回答

10

編輯

我已經作出了jQuery插件,解決了這一問題,重新定位提示留在裏面的窗口,簡單&響應。你可以看到它在這裏的行動tipso

我叉你codepen並重新整理on codepen

我想這是你在找什麼:)

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

是否可以添加「時間」懸停。就像當你將鼠標懸停在div上一樣,然後在幾秒後打開'.bubble'? – innovation 2014-11-11 15:44:31

+1

在這裏你是http://codepen.io/anon/pen/emOoEy只需設置時間變量:) – 2014-11-11 16:03:01

4

我發現下面的方式解決。

工作DEMO但我不能使它與window。如果任何人都可以用窗口做到這一點請你回答我...

$(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 }); 
     }); 
    }); 
+2

如果您已經在使用jQuery - 爲什麼不採取現成的解決方案?有大量的jQuery插件。例如。 http://www.paulund.co.uk/using-social-media-hovercard-jquery-plugin – Kiril 2014-11-11 10:05:28