lukePeavey / SplitType

Javascript utility that splits text into lines, words, characters for animation
https://lukepeavey.github.io/SplitType/
558 stars 40 forks source link

feature: preserve html inside target elements #28

Closed lukePeavey closed 2 years ago

lukePeavey commented 2 years ago

Summary

previously, all HTML inside the target element was stripped away when the text was split.

With this feature, HTML elements inside the target element(s) will be preserved when splitting text. This will make it possible to:

Example

<div id="target">Some <a href="#">active</a> text</div>
SplitType.create('#target', { types: 'lines, words, chars' })

Result

<div id="target">
  <div class="word" style="display: inline-block; position: relative">
    <div class="char" style="display: inline-block">S</div>
    <div class="char" style="display: inline-block">o</div>
    <div class="char" style="display: inline-block">m</div>
    <div class="char" style="display: inline-block">e</div>
  </div>
  <a href="#" style="display: inline-block">
    <div class="word" style="display: inline-block; position: relative">
      <div class="char" style="display: inline-block">a</div>
      <div class="char" style="display: inline-block">c</div>
      <div class="char" style="display: inline-block">t</div>
      <div class="char" style="display: inline-block">i</div>
      <div class="char" style="display: inline-block">v</div>
      <div class="char" style="display: inline-block">e</div>
    </div>
  </a>
  <div class="word" style="display: inline-block; position: relative">
    <div class="char" style="display: inline-block">t</div>
    <div class="char" style="display: inline-block">e</div>
    <div class="char" style="display: inline-block">x</div>
    <div class="char" style="display: inline-block">t</div>
  </div>
</div>

Limitations

1. Not fully compatible with splitting text into lines

If the content of a nested element spans multiple lines, it's not possible wrap each line and preserve the nested element. GSAP's plugin has the same limitation. There might be a way to solve this in the future. But for now, this feature will have the caveat that splitting lines is not fully supported.

However, nested elements can be used when splitting lines, as long as there are no line breaks in the nested elements.

TODO

For #3