How to have transparent fonts except for the 'text-caret' in a textarea?

后端 未结 4 743
难免孤独
难免孤独 2021-02-13 19:36

I have a simple textarea and I need to make transparent letters while allowing the text-caret to be visible. When I apply the following rules then I get invisible caret:

4条回答
  •  粉色の甜心
    2021-02-13 20:01

    Here is a CSS3 solution for making the text, itself, transparent:

    Set the color attribute to be color: rgba(0,0,0,0); for the text

    The only problem is that the caret goes invisible to. I did a quick search and found out that the caret and its styling are completely at the disposal of the browser. As such, the only option that I can think of for you is to use Javascript to add a simulated caret to the end of what you are typing.

    I have an idea of how to do this, but it's messy and I wouldn't exactly call it ideal - I am, however, going to write it in case it helps further someone else's idea:

    1. add a hidden label to the page
      • make sure it's hidden and not display: none; (so that it has actual width)
      • set white-space: nowrap; to keep it all on one line)
      • make sure the text is styled exactly the same as the text in the textarea
    2. add the element | right before the textarea (I will refer to this as the caret for the rest of the spec)
      • set its position to position: relative;
      • increase its z-index to make it overlay
      • shift it right in order to set it on top of where the ACTUAL caret's initial position would be
    3. make a function to check take in the value of the textarea and check the width of the textarea against the position of the caret (lookup selectionStart if you don't know how to do this)
      • the problem here is that characters are not always the same length, nor are they always the same length as their counterparts in other fonts
      • to solve this, as text is entered into the textarea you should have it imitated in the hidden label you created in step 1
        • imitate only the text from the start of the textarea to the caret's current position
        • wrap each character (including spaces) in their own span
      • next you will have to call a function to compare the width of the label with the width of the textarea
        • if the label is less wide than the textarea, get the width of the last span in the hidden label and shift the caret to the right by that width, then move on to step 4
          • as this is function will be run as text is entered it will happen one character at a time
          • be careful here that the caret doesn't go outside the textarea when it's in its last and near last positions
        • if the label is wider than the textarea:
          • add the widths of the characters (spans) in the label one at a time until you reach the width of the textarea
          • shift the position of the caret down by the height of the font and back to the horizontal starting position (as the caret's position is relative, just change its left position back to (0 + offsetToACTUALCaretPosition)
          • use a flag (e.g. class="break") to mark the last span (character) in the previous row
          • call the width comparison function again
            • make sure that you include a condition to check for the flags that you added at the end of each "row" (if any)
    4. if you haven't already, apply any desired CSS styles to the caret span and change the color of the textarea's text to be color: rgba(0,0,0,0);

    Caveats:

    • this will have a lot of overhead for the tiny job it does
    • you will have to adjust this method to account for padding
    • you will have to adjust this method to add support for deleting characters and moving the carets to an earlier position (to the left)
    • if you leave the textarea scrollable, you will have to add support for that (also for similar settings, like static heights causing text to scroll or move off screen/out of the textarea's visible area)

    As I said before, I know that this solution is very rough, but it may help someone come up with a better one.

    Good luck!

提交回复
热议问题