英文:
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 -->
$('.linked').scroll(function() {
$('.linked').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 -->
<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 -->
Here is a fiddle reproducing my problem.
Any help appreciated.
答案1
得分: 1
由于您希望高亮部分跟随滚动文本区域的内容,您只需相应地移动.hwt-highlights
。这可以通过简单地为其添加transform: translateY(<number>px)
来实现,其中<number>
是文本区域当前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(<number>px)
, where <number>
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 -->
$('.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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论