问题描述
我有一个仅CSS的工具提示,当您hover
链接时会加载span
作为工具提示.但是,这是用CSS定位的,但是如果链接位于页面顶部或侧面附近,则工具提示会偏离页面的侧面/顶部.
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.
css是否可以进行此更改,还是我必须依靠JS?我已经开始尝试将某些东西与jQuery放在一起,但如果可能的话,宁愿使用CSS.
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拨弄 https://jsfiddle.net/gtoprh21/12/
摘要:
$( ".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>
推荐答案
不幸的是,如果您想保持工具提示的良好位置,就不可能仅使用CSS.
Unfortunately, if you want to keep your tooltip good positioning, it isn't possible using only CSS.
脚本解决方案
但是,由于您已经在使用某些脚本,因此建议您使用以下解决方案:
But, as you're already using some script, I suggest you this solution:
- 使用
position: absolute
将.ktooltiptext
相应地定位到.ref
, - 使用
.getBoundingClientRect()
方法轻松获取工具提示的位置和大小, - 如果不在
window
范围内,请进行一些更正, - 还对CSS进行了一些修改.
- Use
position: absolute
to position the.ktooltiptext
accordingly to.ref
, - Use the
.getBoundingClientRect()
method to easily get the position and size of the tooltip, - Apply some correction if out of the
window
, - Also made some modifications in CSS.
仅包含本机JavaScript的代码段(避免使用jQuery,文档会更浅.)
Snippet with only native JavaScript (avoiding jQuery, document will be lighter.)
var ktooltips = document.querySelectorAll(".ktooltip");
ktooltips.forEach(function(ktooltip, index){ // For each ktooltip
ktooltip.addEventListener("mouseover", position_tooltip); // On hover, launch the function below
})
function position_tooltip(){
// Get .ktooltiptext sibling
var tooltip = this.parentNode.querySelector(".ktooltiptext");
// Get calculated ktooltip coordinates and size
var ktooltip_rect = this.getBoundingClientRect();
var tipX = ktooltip_rect.width + 5; // 5px on the right of the ktooltip
var tipY = -40; // 40px on the top of the ktooltip
// Position tooltip
tooltip.style.top = tipY + 'px';
tooltip.style.left = tipX + 'px';
// Get calculated tooltip coordinates and size
var tooltip_rect = tooltip.getBoundingClientRect();
// Corrections if out of window
if ((tooltip_rect.x + tooltip_rect.width) > window.innerWidth) // Out on the right
tipX = -tooltip_rect.width - 5; // Simulate a "right: tipX" position
if (tooltip_rect.y < 0) // Out on the top
tipY = tipY - tooltip_rect.y; // Align on the top
// Apply corrected position
tooltip.style.top = tipY + 'px';
tooltip.style.left = tipX + 'px';
}
.ref,
.refs {
position: relative;
}
.ktooltip {
display: inline-block;
text-indent: 0em;
}
.ref .ktooltiptext,
.refs .ktooltiptext {
visibility: hidden;
width: 200px;
background: #fff;
border-radius: 6px;
padding: 5px; /* TAKIT: Changed here */
top: -999px; /* TAKIT: Changed here */
left: -999px; /* TAKIT: Changed here */
border: 2px solid grey;
line-height: normal;
position: absolute; /* TAKIT: Changed here */
z-index: 1;
}
.ref:hover .ktooltiptext,
.refs:hover .ktooltiptext {
visibility: visible;
}
<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>
<!-- the 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.
<!-- link to ref -->
<sup class="ref expl">
<a href="#edtxt-explnote2" id="reference-to-edtxt-explnote2" class="ktooltip">20</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>
带有jQuery的代码段
Snippet with jQuery
$(".ktooltip").mouseover(function(e) {
var tooltip = $(this).siblings('.ktooltiptext'); // Get tooltip element (ktooltiptext)
var tipX = $(this).outerWidth() + 5; // 5px on the right of the ktooltip
var tipY = -40; // 40px on the top of the ktooltip
tooltip.css({ top: tipY, left: tipX }); // Position tooltip
// Get calculated tooltip coordinates and size
var tooltip_rect = tooltip[0].getBoundingClientRect();
// Corrections if out of window
if ((tooltip_rect.x + tooltip_rect.width) > $(window).width()) // Out on the right
tipX = -tooltip_rect.width - 5; // Simulate a "right: tipX" position
if (tooltip_rect.y < 0) // Out on the top
tipY = tipY - tooltip_rect.y; // Align on the top
// Apply corrected position
tooltip.css({ top: tipY, left: tipX });
});
.ref,
.refs {
position: relative;
}
.ktooltip {
display: inline-block;
text-indent: 0em;
}
.ref .ktooltiptext,
.refs .ktooltiptext {
visibility: hidden;
width: 200px;
background: #fff;
border-radius: 6px;
padding: 5px; /* TAKIT: Changed here */
top: -999px; /* TAKIT: Changed here */
left: -999px; /* TAKIT: Changed here */
border: 2px solid grey;
line-height: normal;
position: absolute; /* TAKIT: Changed here */
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>
<!-- the 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.
<!-- link to ref -->
<sup class="ref expl">
<a href="#edtxt-explnote2" id="reference-to-edtxt-explnote2" class="ktooltip">20</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>
使用上面的任何摘录,您都可以在窗口中玩耍,以查看弹出窗口不会在右侧移出!它也不应该排在首位.
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.
⋅⋅⋅
仅CSS建议
现在,如果您不太关心工具提示的位置,则可以在不更改任何HTML的情况下使用此解决方案:
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:
- 在
span
元素上使用position: relative;
用作参考, - 在
.ktooltiptext
上使用position: absolute
, - 根据需要使用
top
和left
定位.ktooltiptext
.
- Use
position: relative;
on thespan
elements to use it as a reference, - Use
position: absolute
on the.ktooltiptext
, - Use
top
andleft
to position the.ktooltiptext
as you wish.
使用该解决方案时,工具提示将保持其样式,但会在左侧的span
元素下方对齐,因此工具提示绝不应该在右侧或顶部出现.
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.
p span { /* TAKIT: Changed here */
position: relative;
}
.ktooltip {
display: inline-block;
text-indent: 0em;
}
.ref .ktooltiptext,
.refs .ktooltiptext {
visibility: hidden;
width: 200px;
background: #fff;
border-radius: 6px;
padding: 5px; /* TAKIT: Simplified here */
border: 2px solid grey;
line-height: normal;
position: absolute; /* TAKIT: Changed */
top: 20px; /* TAKIT: Changed */
left: 0; /* TAKIT: Added to always align tooltip on the left of the span */
z-index: 1;
}
.ref:hover .ktooltiptext,
.refs:hover .ktooltiptext {
visibility: visible;
}
<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>
<!-- the 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.
<!-- link to ref -->
<sup class="ref expl">
<a href="#edtxt-explnote2" id="reference-to-edtxt-explnote2" class="ktooltip">20</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>
这篇关于防止CSS工具提示跳出页面/窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!