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)

huangapple go评论99阅读模式
英文:

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>元素中输入文本,而且当我滚动到表格的底部时,它会跳动,就像这样:

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)

它应该跟随光标。我创建了这个演示: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 &lt;input&gt; element and also makes jumps when I scroll far down the table, like this:

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)

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:

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;h4 id=&quot;log&quot;&gt;pageX: -, pageY: -&lt;/h4&gt;
&lt;div class=&quot;tooltip&quot;&gt;I&#39;m a tooltip!&lt;/div&gt;

&lt;input type=&quot;text&quot; placeholder=&quot;Enter something&quot;&gt;
&lt;table class=&#39;items&#39;&gt;
            &lt;thead&gt;
            &lt;tr class=&quot;table-header&quot;&gt;
                &lt;th&gt;Id&lt;/th&gt;
                &lt;th&gt;Icon&lt;/th&gt;
                &lt;th&gt;Name&lt;/th&gt;
            &lt;/tr&gt;
            &lt;/thead&gt;
            
            &lt;tr&gt;
              &lt;td&gt;1&lt;/td&gt;
              &lt;td&gt;1&lt;/td&gt;
              &lt;td&gt;1&lt;/td&gt;
            &lt;/tr&gt;
            
            &lt;tr&gt;
              &lt;td&gt;12&lt;/td&gt;
              &lt;td&gt;12&lt;/td&gt;
              &lt;td&gt;12&lt;/td&gt;
            &lt;/tr&gt;
            ...

js:

$( document ).on( &quot;mousemove&quot;, function( event ) {
  $( &quot;#log&quot; ).text( &quot;pageX: &quot; + event.pageX + &quot;, pageY: &quot; + event.pageY );
  $( &quot;.tooltip&quot; ).css({
    &quot;left&quot; : event.pageX,
    &quot;top&quot; : 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

XY位置分别增加1,以使您可以选择其后面的内容,或者将margin-top:1px添加到您的**.tooltip**也可以。

解决滚动问题的简单方法是使用clientXclientY而不是。

这是修改后的脚本:

$(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( &quot;mousemove&quot;, function( event ) {
  $( &quot;#log&quot; ).text( &quot;clientX: &quot; + event.clientX + &quot;, clientY: &quot; + event.clientY );
  $( &quot;.tooltip&quot; ).css({
    &quot;left&quot; : event.clientX + 1,
    &quot;top&quot; : 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.

huangapple
  • 本文由 发表于 2020年1月6日 02:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定