我有一个仅CSS的工具提示,当您span
链接时会加载hover
作为工具提示。但是,这是用CSS定位的,但是如果链接位于页面顶部或侧面附近,则工具提示会偏离页面的侧面/顶部。
css是否可以进行此更改,还是必须依靠JS? 我已经开始尝试将某些东西与jQuery放在一起,但在可能的情况下宁愿使用CSS。
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>
答案 0 :(得分:18)
不幸的是,如果您想保持工具提示的良好定位,就不可能仅使用CSS。
脚本解决方案
但是,由于您已经在使用某些脚本,因此建议您使用以下解决方案:
position: absolute
将.ktooltiptext
定位到.ref
,.getBoundingClientRect()
方法轻松获得工具提示的位置和大小,window
,请应用一些更正,仅包含本机JavaScript的代码段 (避免使用jQuery,文档会更浅。)
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的代码段
$(".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>
使用上面的任何摘录,您都可以在窗口中玩,以查看弹出窗口不会在右侧出现!它也不应该排在首位。
⋅ ⋅ ⋅
仅CSS建议
现在,如果您对工具提示的位置不太在意,可以在不更改任何HTML的情况下使用此解决方案:
position: relative;
元素上使用span
用作参考,position: absolute
上使用.ktooltiptext
,top
和left
来放置.ktooltiptext
。使用该解决方案时,工具提示将保持其样式,但会在左侧的span
元素下对齐,因此工具提示永远不会在右侧或顶部出现。
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>
答案 1 :(得分:3)
您可以仅使用CSS而不使用JS来尝试此操作。不是最优雅的工具提示类型,但它永远不会让您失望,也永远不会让您放弃:)
.ktooltip {
display: inline-block;
text-indent:0em;
}
.ktooltiptext, .ktooltiptext {
display: none;
width: calc(100vw - 35px);
background: #fff;
border-radius: 6px;
padding: 5px 5px;
left: 10px;
border: 2px solid grey;
line-height: normal;
text-decoration: none;
position: absolute;
z-index: 1;
}
p {display:inline-block}
.ktooltip:hover + span {
display: block;
}
<p>
<span id="edtxt.trans1" class="tei l">My hope is in a bishop,
<!--link to a reference -->
<div style="display:inline-block">
<a href="#edtxt-explnote1" id="reference-to-edtxt-explnote1" class="ktooltip">1</a>
<span class="ktooltiptext">According to tradition <span style="name">Nicholas</span> was bishop of Myra in Lycia (south-west Turkey today).</span>
</div>
</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 -->
<a href="#edtxt-explnote2" id="reference-to-edtxt-explnote2" class="ktooltip">2</a>
<span class="ktooltiptext" onclick="return false;"> 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>
</span><br>
</p>
答案 2 :(得分:1)
只需将此css nd替换为您的班级
.ref .ktooltiptext,.refs .ktooltiptext
.ref .ktooltiptext, .refs .ktooltiptext {
visibility:hidden;
width: 200px;
background: #fff;
border-radius: 6px;
padding: 5px 5px;
top: -15px;
left: 10px;
border:2px solid grey;
line-height: normal;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
答案 3 :(得分:1)
尽管整个过程都需要重新编码,但是您可以通过以下方式实现:
$(".ktooltip").on('mouseover', function(e) {
var tooltip = $('.ktooltiptext'),
wt = $(window).scrollTop(), //window top pos
ww = $(window).outerWidth(), //window width
tt = tooltip.offset().top, //Tooltip top pos
tl = tooltip.offset().left, //Tooltip left pos
tw = tooltip.outerWidth(); //Tooltip Width
tooltip.css({ 'left': '10px', 'right': 'auto', 'top': '-40px' });
if(tt < wt) tooltip.css('top', 0);
if((tl + tw) > ww) tooltip.css({ 'left': 'auto', 'right': 0 });
})
答案 4 :(得分:0)
选项1)使用title
全局属性
title-指定有关元素的额外信息(显示为 工具提示)
文档:The title global attribute 使用title属性存在以下问题:
- 使用仅触摸设备的人们
- 人们使用键盘导航
- 人们借助屏幕阅读器或放大镜等辅助技术进行导航
- 有精细运动控制障碍的人
- 有认知问题的人。这主要是由于浏览器支持不一致,另外 辅助技术的解析带来的并发症 浏览器呈现的页面。
span {border-bottom: 1px dashed pink}
<span title="According to tradition Nicholas was bishop of Myra in Lycia (south-west Turkey today).">
Mouse over this paragraph, to display the title attribute as a tooltip.
</span>
选项2)如果文本和工具提示文本的大小是固定的:可以使用4个css类放置引用触发元素的工具提示。 像这样:
.tooltip-top {top: -3em}
.tooltip-bottom {top: 0}
.tooltip-left {left: -3em}
.tooltip-right {right: 0}