2014-11-08 22 views
20

Sto provando a creare una scheda hover con css. Ho una domanda sulla posizione della pagina in basso.Posizionamento scheda hover CSS

Ho creato questa pagina DEMO da codepen.io. Quindi, se sei nella parte inferiore della pagina demo, vedi la bolla div.

Cosa devo fare per mostrare il .bubble nella parte inferiore del triangolo in basso nella pagina? enter 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

potrei sbagliarmi, ma ho pensa che hai bisogno di javascript per controllare i limiti – Timmerz

+1

@Timmerz hai ragione su questo. Ma non sono riuscito a trovare un'applicazione di esempio. – innovation

+0

Che browser stai usando? Perché nell'ultima versione di FF l'IT funziona bene. hai provato la tua DEMO in piena pagina qui: http://codepen.io/shadowman86/full/mCIkt – dippas

risposta

10

EDIT

ho fatto un plugin jQuery che affronta questi problemi, riposiziona il tooltip di stare dentro la finestra, semplice & reattivo . Potete vederlo in azione qui tipso

I biforcato tua codepen e rielaborato si on codepen

credo che questo è quello che stai cercando :)

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

è possibile aggiungere "tempo" per il passaggio del mouse. Come quando si passa su un div e poi '.bubble' si apre dopo qualche secondo? – innovation

+1

Qui siete http://codepen.io/anon/pen/emOoEy Basta impostare la variabile temporale :) –

4

ho trovato una soluzione nel modo seguente.

Lavoro DEMO ma non sono riuscito a farlo con window. Se chiunque può farlo con finestra favore, rispondimi ...

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

Se stai già usando jQuery - perché non prendere una soluzione pronta? Ci sono un sacco di plugin jQuery. Per esempio. http://www.paulund.co.uk/using-social-media-hovercard-jquery-plugin – Kiril