定义了行是否以及如何被分割。
分割后的行元素可通过 TextSplit 实例的 lines 属性返回的数组访问。

const { lines } = text.split(target, { lines: true });

默认分割包装器

默认情况下,每行都包裹在一个带有以下样式和数据属性的 span 元素中

<span style="display: block;" data-line="0">This is the first line</span>
<span style="display: block;" data-line="1">This is the second line</span>
<span style="display: block;" data-line="2">This is the third line</span>

自定义分割包装器

分割包装器可以通过传递一个分割参数Object对象或传递一个自定义的HTML 模板String来配置。

分割嵌套元素

必要时,嵌套元素会在行之间重复。例如,以下 HTML

<p>
  This is a text <a href="#link">with a link 
  that contains a nested <em>em 
  element</em></a>
</p>

分割后产生以下结构(为清晰起见,省略了 CSS 样式)

<p>
  <span>This is a text <a href="#link">with a link</a></span>
  <span><a href="#link">that contains a nested <em>em</em></a></span>
  <span><a href="#link"><em>element</em></a></span>
</p>

字体加载和行大小调整处理

为防止行计算不准确,所有字体加载和布局操作完成后才进行行分割,这通过等待document.fonts.ready.then promise 完成来实现。
然后,如果目标元素调整大小,行会自动重新分割,在此过程中覆盖现有的行、单词和字符元素。

按行分割时对行、单词和字符进行动画处理

每次行分割都会覆盖现有的分割元素,这会导致正在运行的分割元素动画在字体加载完成或每次文本元素调整大小时停止。
split.addEffect()方法中声明动画可确保在调整大小之间连续播放,并在使用split.revert()时自动恢复。

const split = text.split(target, params);

split.addEffect(({ lines, words, chars }) => animate([lines, words, chars], {
  opacity: { from: 0 },
}));

split.revert(); // This also reverts the animation declared with addEffect

接受

默认值

false

行代码示例

import { animate, text, stagger } from 'animejs';

text.split('p', {
  lines: { wrap: 'clip' },
})
.addEffect(({ lines }) => animate(lines, {
  y: [
    { to: ['100%', '0%'] },
    { to: '-100%', delay: 750, ease: 'in(3)' }
  ],
  duration: 750,
  ease: 'out(3)',
  delay: stagger(200),
  loop: true,
  loopDelay: 500,
}));
<div class="large centered row">
  <p class="text-xl">Split text by lines.<br>線で分割します。</p>
</div>
<div class="small row"></div>