在开始 typed.js 打字动画之前使用什么回调?

问题描述 投票:0回答:0

我正在创建一个脚本,在其中我在父 div 中的第一个和第二个跨度上添加一个类。但我似乎无法使 typed.js 中的 onBegin 回调正常工作。我错过了什么吗?

function addClassSpan() {
    // Get all the spans in the typed element
    const spans = document.querySelectorAll('.typed-words span');

    // Add the class to the first span
    spans[0].classList.add('gradient-clip');

    // Add the class to the last span
    spans[spans.length - 1].classList.add('gradient-clip');
}

function typeText() {
    var typed = new Typed('.typed-words', {
        strings: [
            '<span>Moving money between banks your way.</span> \n<span>How&apos;s Kash different?</span> \n<span>We know the hard work behind each dollar you send back home. That&apos;s why we work harder to avoid unfair fees and even better...</span>'
        ],
        stringsElement: null,
        typeSpeed: 10,
        backSpeed: 100,
        backDelay: 500,
        startDelay: 1000,
        fadeOut: true,
        fadeOutDelay: 500,
        loop: false,
        attr: null,
        showCursor: false,
        onBegin: (self) => {
            addClassSpan();
        }
    });
}
typeText();

我使用 onComplete 回调函数进行了测试,它可以正常工作,但我需要在开始输入之前添加跨度。

javascript callback typed.js
© www.soinside.com 2019 - 2024. All rights reserved.