英文:
A popup cursor-following tooltip with a fixed position doesn't let me to fill in an input element and jumps when far down a table (JSFIDDLE)
问题
我正在尝试使用这个答案中的工具提示代码,但我遇到了一些问题。工具提示不允许我在<input>
元素中输入文本,而且当我滚动到表格的底部时,它会跳动,就像这样:
它应该跟随光标。我创建了这个演示:https://jsfiddle.net/nuvgef12/,以下是代码:
CSS:
.tooltip {
display: inline-block;
position: fixed;
padding: .5em 1em;
background-color: red;
}
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h4 id="log">pageX: -, pageY: -</h4>
<div class="tooltip">I'm a tooltip!</div>
<input type="text" placeholder="Enter something">
<table class='items'>
<thead>
<tr class="table-header">
<th>Id</th>
<th>Icon</th>
<th>Name</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>12</td>
<td>12</td>
<td>12</td>
</tr>
...
JavaScript:
$( document ).on( "mousemove", function( event ) {
$( "#log" ).text( "pageX: " + event.pageX + ", pageY: " + event.pageY );
$( ".tooltip" ).css({
"left" : event.pageX,
"top" : event.pageY
});
});
编辑:
提供的解决方案有助于在输入框中放置文本,但仍然存在跳跃问题。在滚动表格后会出现这个问题 - 工具提示会短暂消失,然后重新出现在下方和侧面。
英文:
I'm trying to make use of this answer's tooltip code, but I've encountered a few problems. The tooltip doesn't let me type text in a <input>
element and also makes jumps when I scroll far down the table, like this:
while it's supposed to follow the cursor. I made this fiddle: https://jsfiddle.net/nuvgef12/ and here's the code:
css:
.tooltip {
display: inline-block;
position: fixed;
padding: .5em 1em;
background-color: red;
}
html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h4 id="log">pageX: -, pageY: -</h4>
<div class="tooltip">I'm a tooltip!</div>
<input type="text" placeholder="Enter something">
<table class='items'>
<thead>
<tr class="table-header">
<th>Id</th>
<th>Icon</th>
<th>Name</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>12</td>
<td>12</td>
<td>12</td>
</tr>
...
js:
$( document ).on( "mousemove", function( event ) {
$( "#log" ).text( "pageX: " + event.pageX + ", pageY: " + event.pageY );
$( ".tooltip" ).css({
"left" : event.pageX,
"top" : event.pageY
});
});
EDIT:
The given solutions help with letting putting the text inside input, but there still exists the problem with jumping. It happens after a scroll down the table - the tooltip briefly disappears and then reappears much off to down and to the side.
答案1
得分: 1
如果您将工具提示添加一些边距,它将允许指针点击输入而不是工具提示。
.tooltip {
display: inline-block;
position: fixed;
margin: 1px;
background-color: red;
}
英文:
If you add a little margin to your tooltip it will let your pointer click on the input instead of the tooltip
.tooltip {
display: inline-block;
position: fixed;
margin: 1px;
background-color: red;
}
答案2
得分: 1
将X和Y的位置分别增加1,以使您可以选择其后面的内容,或者将margin-top:1px
添加到您的**.tooltip**也可以。
解决滚动问题的简单方法是使用clientX
和clientY
而不是。
这是修改后的脚本:
$(window).on("mousemove", function( event ) {
$("#log").text("clientX: " + event.clientX + ", clientY: " + event.clientY );
$(".tooltip").css({
"left" : event.clientX + 1,
"top" : event.clientY + 1
});
});
您可以在**这里了解更多关于它们之间的区别,但基本上pageX相对于文档在视口内的位置,当滚动时会发生变化,而clientX**相对于视口本身,不会以这种方式发生变化。
英文:
Add 1 to the X and Y position to allow you to select behind it, or add a margin-top:1px
to your .tooltip will also work.
An easy fix to the scroll issue is to use clientX
and clientY
instead.
Here is the modified script:
$(window).on( "mousemove", function( event ) {
$( "#log" ).text( "clientX: " + event.clientX + ", clientY: " + event.clientY );
$( ".tooltip" ).css({
"left" : event.clientX + 1,
"top" : event.clientY + 1
});
});
<a href="https://jsfiddle.net/52bv4tgp/">Here is the working code.</a>
You can read more <a href="https://stackoverflow.com/questions/6073505/what-is-the-difference-between-screenx-y-clientx-y-and-pagex-y">here</a> on the difference between them, but basically pageX is relative to the document's position within the viewport, which changes when scrolled, and clientX is relative to the viewport itself, which does not change in this way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论