I have a CSS only tooltip which loads a span
as a tooltip when you hover
the link. However this is positioned with CSS but if the link is near to the top of a page or side then the tooltip goes off the side/top of the page.
Is there a way with css to make this change or will I have to rely on JS? I have started to try to put something together with jQuery but would rather use CSS if possible.
JS fiddle at https://jsfiddle.net/gtoprh21/12/
Snippet:
$( ".ktooltip" )
.mouseover(function(e) {
var mousex = e.pageX + 20; //Get X coordinates
var mousey = e.pageY + 10; //Get Y coordinates
if((mousey+100)>$(window).height())
{
$('.tooltip')
.css({ top: mousey-100 ,left: mousex })
}
else if((mousex+200)>$(window).width())
{
$('.tooltip')
.css({ top: mousey ,left: mousex-200})
}
else
{
$('.tooltip')
.css({ top: mousey, left: mousex })
}
})
.ref, .refs {
position:relative;
}
/*added a text indent to overide indent styles further down*/
.ktooltip {
display: inline-block;
text-indent:0em;
}
.ref .ktooltiptext, .refs .ktooltiptext {
visibility:hidden;
width: 200px;
background: #fff;
border-radius: 6px;
padding: 5px 5px;
top: -40px;
left: 10px;
border:2px solid grey;
line-height: normal;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.ref:hover .ktooltiptext, .refs:hover .ktooltiptext {
visibility: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>
<span id="edtxt.trans1" class="tei l">My hope is in a bishop,
<!--link to a reference -->
<sup class="ref expl">
<a href="#edtxt-explnote1" id="reference-to-edtxt-explnote1" class="ktooltip">1</a>
<!-- lhe reference in a tooltip -->
<span class="ktooltiptext">According to tradition <span style="name">Nicholas</span> was bishop of Myra in Lycia (south-west Turkey today).</span>
</sup>
</span><br>
<span id="trans2" class="tei l">and in almighty God and his never-ending miracles.</span><br>
<span id="trans3" class="tei l">Generous Saint Nicholas holds an office,</span><br>
<span id="trans4" class="tei l">there is a gold symbol in his sign.
<!-- likn to ref -->
<sup class="ref expl">
<a href="#edtxt-explnote2" id="reference-to-edtxt-explnote2" class="ktooltip">2</a>
<!-- the tooltip -->
<span class="ktooltiptext"> One of <span style="">Nicholas’s</span> symbols was three <sup>bags</sup> of gold <span style="font-variant: small-caps;">which</span> were the <span style="color: red;">dowry</span> he provided <span style="color: blue;">for</span> three <span style="color: green;">girls</span>
</span>
</sup>
</span><br>
</p>
although the whole thing needs to recode but you can achieve that with something like this :
Option 1) Using
title
global attributeOption 2) If the text and the tooltip text are fixed sized: 4 css classes can be used to place the tooltip referencing the trigger element. Something like:
You can try this with only CSS and no JS at all. Not the most elegant type of tooltip but it will never fail you and it will never give you up :)
https://jsfiddle.net/gtoprh21/72/
just copy this css nd replace with your class
.ref .ktooltiptext, .refs .ktooltiptext
Unfortunately, if you want to keep your tooltip good positioning, it isn't possible using only CSS.
Script solutions
But, as you're already using some script, I suggest you this solution:
position: absolute
to position the.ktooltiptext
accordingly to.ref
,.getBoundingClientRect()
method to easily get the position and size of the tooltip,window
,Snippet with only native JavaScript (avoiding jQuery, document will be lighter.)
Snippet with jQuery
With any of the above snippets, you can play with your window to see that the pop-up won't go outside on the right! It shouldn't go out on the top as well.
⋅ ⋅ ⋅
A CSS only suggestion
Now, if you don't care that much about the positioning of your tooltip, you can use this solution where I didn't change any of your HTML:
position: relative;
on thespan
elements to use it as a reference,position: absolute
on the.ktooltiptext
,top
andleft
to position the.ktooltiptext
as you wish.Using that solution, the tooltip will keep its style, but would be aligned on the left, under the
span
element, so the tooltip should never go out on the right or on the top.