javascript - 文本区域输入和突出显示容器的同步滚动

标签 javascript jquery

我正在尝试模仿 Highlight Within Textarea plugin 的一些行为没有实际使用它。

我已经很清楚高亮部分了,但是,当输入文本超出文本区域输入的可见区域时,高亮显示不正确。

问题是文本区域可以向下滚动,而高亮容器似乎不能滚动。我试图在 this approach 之后链接文本区域和突出显示容器的滚动条。 , 但不知何故它不起作用。

$('.linked').scroll(function() {
  $('.linked').scrollTop($(this).scrollTop());
});
.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;
}
<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重现我的问题。

感谢任何帮助。

最佳答案

既然你想让高亮跟随滚动文本区域的内容,你只需要移动.hwt-highlights因此。这可以通过简单地给它一个 transform: translateY(<number>px) 来完成。 , 其中<number>只是当前 scrollTop 的负值文本区域的值:

$('.linked').scroll(function() {
  $('.hwt-highlights').css('transform', `translateY(${-this.scrollTop}px)`);
});
.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;
}
<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>

关于javascript - 文本区域输入和突出显示容器的同步滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59578271/

相关文章:

javascript - agGrid - 在列标题名称中显示过滤短语

javascript - 如何在 HTML Canvas 上绘制可变粗细的贝塞尔曲线?

javascript - 如何将 `appendPre` 放入表中?

Jquery 倒计时平滑动画

javascript - 无法循环 JQuery .hover()

javascript - 在 Jquery 中将 CSS 属性应用于 html

javascript - 使用 DOMparser 进行 Web 抓取时的数组输出问题

javascript - 页面上jquery元素加载效果

datepicker() 和 mask() 之间的 jQuery 冲突

jquery - JEdi​​table - 重置值