Let's say I have a <div>
element, which contains a very long text (real use case would be much longer):
aaaaa bbbbb ccccc ddddd eeeee fffff ggggg hhhhh iiiii jjjjj kkkkk lllll mmmmm nnnnn ooooo ppppp qqqqq rrrrr sssss ttttt uuuuu vvvvv wwwww xxxxx yyyyy zzzzz
The container <div>
has rendered a vertical scroll bar. It rendered like this:
+---------------------+
| ddddd eeeee fffff |^|
| ggggg hhhhh iiiii | |
| jjjjj kkkkk lllll |||
| mmmmm nnnnn ooooo |||
| ppppp qqqqq rrrrr | |
| sssss ttttt uuuuu |v|
+---------------------+
I want a function which tells me where the <div>
is scrolled. In this example, top most text is ddddd
which has index 18 ('aaaaa bbbbb ccccc ddddd ...'.indexOf('ddddd') === 18
). So the function should return 18.
HTMLElement
only tells me the scrollTop
in px
, but not by how many characters. What should I do?
Notes:
- If the top most texts are partial shown, they may be counted or may not. Any behavior is acceptable.
- You may assume the
<div>
contains only plain texts. And you may assume no CSS rules like::first-letter
applied. - You may not assume mono-spaced fonts used as the ascii art shown above.