同步滚动文本输入框和高亮容器

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

sync scrolling of textarea input and highlighting container

问题

我正在尝试模仿Highlight Within Textarea plugin的一些行为,但实际上不使用它。

我已经基本弄清楚了高亮部分,但是当输入文本超过文本区域的可见区域时,高亮显示没有正确显示。

问题在于文本区域可以向下滚动,而高亮容器似乎无法滚动。我尝试了链接文本区域和高亮容器的滚动条,遵循这个方法,但是不知何故它不起作用。

$('.linked').scroll(function() {
  $('.linked').scrollTop($(this).scrollTop());
});
.hwt-container {
  /* ... */
}

.hwt-backdrop {
  /* ... */
}

.hwt-highlights {
  /* ... */
}

.hwt-input {
  /* ... */
}

.hwt-content {
  /* ... */
}

.linked {
  overflow: auto !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Highlight Within Textarea test</h1>
<div class="hwt-container">
  <div class="hwt-backdrop" style="margin: 1px;">
    <div class="hwt-highlights hwt-content linked" style="padding: 0px; border-width: 0px;">This is <mark>test</mark> string which is not completely displayed in the visible area of <mark>textarea</mark> input.
    </div>
  </div>
  <textarea spellcheck="false" class="hwt-input hwt-content linked">This is test string which is not completely displayed in the visible area of textarea input.</textarea>
</div>

这里是一个fiddle,可以重现我的问题。

任何帮助都会感激。

英文:

I am trying to mimic some of the behavior of the Highlight Within Textarea plugin without actually using it.

I have pretty much figured out the highlighting part, however, when the input text exceeds the visible area of the textarea input, the highlighting is not displayed properly.

The problem is that the textarea can be scrolled down, while the highlighting container does not seem to be scrollable. I tried to link the scrollbars of both the textarea and the highlighting container following this approach, but somehow it is not working.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;.linked&#39;).scroll(function() {
  $(&#39;.linked&#39;).scrollTop($(this).scrollTop());
});

<!-- language: lang-css -->

.hwt-container {
  display: inline-block;
  position: relative;
  overflow: hidden;
  -webkit-text-size-adjust: none;
}

.hwt-backdrop {
  position: absolute;
  top: 0;
  right: -99px;
  bottom: 0;
  left: 0;
  padding-right: 99px;
  overflow-x: hidden;
  overflow-y: auto;
}

.hwt-highlights {
  width: auto;
  height: auto;
  border-color: transparent;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: transparent;
  overflow: hidden;
}

.hwt-input {
  display: block;
  position: relative;
  margin: 0;
  padding: 0;
  border-radius: 0;
  font: inherit;
  overflow-x: hidden;
  overflow-y: auto;
}

.hwt-content {
  border: 1px solid;
  background: none transparent;
}

.hwt-content mark {
  padding: 0;
  color: inherit;
}

.linked {
  overflow: auto !important;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;h1&gt;Highlight Within Textarea test&lt;/h1&gt;
&lt;div class=&quot;hwt-container&quot;&gt;
  &lt;div class=&quot;hwt-backdrop&quot; style=&quot;margin: 1px;&quot;&gt;
    &lt;div class=&quot;hwt-highlights hwt-content linked&quot; style=&quot;padding: 0px; border-width: 0px;&quot;&gt;This is &lt;mark&gt;test&lt;/mark&gt; string which is not completely displayed in the visible area of &lt;mark&gt;textarea&lt;/mark&gt; input.
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;textarea spellcheck=&quot;false&quot; class=&quot;hwt-input hwt-content linked&quot;&gt;This is test string which is not completely displayed in the visible area of textarea input.&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

Here is a fiddle reproducing my problem.

Any help appreciated.

答案1

得分: 1

由于您希望高亮部分跟随滚动文本区域的内容,您只需相应地移动.hwt-highlights。这可以通过简单地为其添加transform: translateY(&lt;number&gt;px)来实现,其中&lt;number&gt;是文本区域当前scrollTop值的负值:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
$('.linked').scroll(function() {
  $('.hwt-highlights').css('transform', `translateY(${-this.scrollTop}px)`);
});

<!-- language: lang-css -->
.hwt-container {
  display: inline-block;
  position: relative;
  overflow: hidden;
  -webkit-text-size-adjust: none;
}

.hwt-backdrop {
  position: absolute;
  top: 0;
  right: -99px;
  bottom: 0;
  left: 0;
  padding-right: 99px;
  overflow-x: hidden;
  overflow-y: auto;
}

.hwt-highlights {
  width: auto;
  height: auto;
  border-color: transparent;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: transparent;
  overflow: hidden;
}

.hwt-input {
  display: block;
  position: relative;
  margin: 0;
  padding: 0;
  border-radius: 0;
  font: inherit;
  overflow-x: hidden;
  overflow-y: auto;
}

.hwt-content {
  border: 1px solid;
  background: none transparent;
}

.hwt-content mark {
  padding: 0;
  color: inherit;
}

.linked {
  overflow: auto !important;
}

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Highlight Within Textarea test</h1>
<div class="hwt-container">
  <div class="hwt-backdrop" style="margin: 1px;">
    <div class="hwt-highlights hwt-content linked" style="padding: 0px; border-width: 0px;">This is <mark>test</mark> string which is not completely displayed in the visible area of <mark>textarea</mark> input.
    </div>
  </div>
  <textarea spellcheck="false" class="hwt-input hwt-content linked">This is test string which is not completely displayed in the visible area of textarea input.</textarea>
</div>

<!-- end snippet -->

请注意,我已经保留了您提供的HTML和CSS代码的原始结构,只对JavaScript部分进行了翻译。

英文:

Since you want the highlights to follow the content of the scrolled textarea, all you need is to move .hwt-highlights accordingly. This can be done by simply giving it a transform: translateY(&lt;number&gt;px), where &lt;number&gt; is simply the negative value of the current scrollTop value of the textarea:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;.linked&#39;).scroll(function() {
  $(&#39;.hwt-highlights&#39;).css(&#39;transform&#39;, `translateY(${-this.scrollTop}px)`);
});

<!-- language: lang-css -->

.hwt-container {
  display: inline-block;
  position: relative;
  overflow: hidden;
  -webkit-text-size-adjust: none;
}

.hwt-backdrop {
  position: absolute;
  top: 0;
  right: -99px;
  bottom: 0;
  left: 0;
  padding-right: 99px;
  overflow-x: hidden;
  overflow-y: auto;
}

.hwt-highlights {
  width: auto;
  height: auto;
  border-color: transparent;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: transparent;
  overflow: hidden;
}

.hwt-input {
  display: block;
  position: relative;
  margin: 0;
  padding: 0;
  border-radius: 0;
  font: inherit;
  overflow-x: hidden;
  overflow-y: auto;
}

.hwt-content {
  border: 1px solid;
  background: none transparent;
}

.hwt-content mark {
  padding: 0;
  color: inherit;
}

.linked {
  overflow: auto !important;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;h1&gt;Highlight Within Textarea test&lt;/h1&gt;
&lt;div class=&quot;hwt-container&quot;&gt;
  &lt;div class=&quot;hwt-backdrop&quot; style=&quot;margin: 1px;&quot;&gt;
    &lt;div class=&quot;hwt-highlights hwt-content linked&quot; style=&quot;padding: 0px; border-width: 0px;&quot;&gt;This is &lt;mark&gt;test&lt;/mark&gt; string which is not completely displayed in the visible area of &lt;mark&gt;textarea&lt;/mark&gt; input.
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;textarea spellcheck=&quot;false&quot; class=&quot;hwt-input hwt-content linked&quot;&gt;This is test string which is not completely displayed in the visible area of textarea input.&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 20:00:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578271.html
匿名

发表评论

匿名网友

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

确定