Witam
Mam taki problem.
Otóż dodałem skrypt do pliku html w celu zrobienia animacji napisu nagłówka w języku javascript i problem w tym że chciałbym aby się on wyświetlił w tej animacji tylko raz a on jest zapętlony i co zmienić w kodzie aby wykonywał się tylko raz
Proszę o pomoc
O to kod:
const resolver = {
resolve: function resolve(options, callback) {
// The string to resolve
const resolveString = options.resolveString || options.element.getAttribute('data-target-resolver');
const combinedOptions = Object.assign({}, options, {resolveString: resolveString});
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
function randomCharacter(characters) {
return characters[getRandomInteger(0, characters.length - 1)];
};
function doRandomiserEffect(options, callback) {
const characters = options.characters;
const timeout = options.timeout;
const element = options.element;
const partialString = options.partialString;
let iterations = options.iterations;
setTimeout(() => {
if (iterations >= 0) {
const nextOptions = Object.assign({}, options, {iterations: iterations - 1});
// Ensures partialString without the random character as the final state.
if (iterations === 0) {
element.textContent = partialString;
} else {
// Replaces the last character of partialString with a random character
element.textContent = partialString.substring(0, partialString.length - 1) + randomCharacter(characters);
}
doRandomiserEffect(nextOptions, callback)
} else if (typeof callback === "function") {
callback();
}
}, options.timeout);
};
function doResolverEffect(options, callback) {
const resolveString = options.resolveString;
const characters = options.characters;
const offset = options.offset;
const partialString = resolveString.substring(0, offset);
const combinedOptions = Object.assign({}, options, {partialString: partialString});
doRandomiserEffect(combinedOptions, () => {
const nextOptions = Object.assign({}, options, {offset: offset + 1});
if (offset <= resolveString.length) {
doResolverEffect(nextOptions, callback);
} else if (typeof callback === "function") {
callback();
}
});
};
doResolverEffect(combinedOptions, callback);
}
}
const strings = [
'CZEŚĆ'
];
let counter = 0;
const options = {
// Initial position
offset: 0,
// Timeout between each random character
timeout: 5,
// Number of random characters to show
iterations: 10,
// Random characters to pick from
characters: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'x', '#', '%', '&', '-', '+', '_', '?', '/', '\\', '='],
// String to resolve
resolveString: strings[counter],
// The element
element: document.querySelector('[data-target-resolver]')
}
// Callback function when resolve completes
function callback() {
setTimeout(() => {
counter ++;
if (counter >= strings.length) {
counter = 0;
}
let nextOptions = Object.assign({}, options, {resolveString: strings[counter]});
resolver.resolve(nextOptions, callback);
}, 1000);
}
resolver.resolve(options, callback);
// Wrap every letter in a span
$('.ml9 .letters').each(function(){
$(this).html($(this).text().replace(/([^\x00-\x80]|\w)/g, "<span class='letter'>$&</span>"));
});
anime.timeline({loop: true})
.add({
targets: '.ml9 .letter',
scale: [0, 1],
duration: 1500,
elasticity: 600,
delay: function(el, i) {
return 45 * (i+1)
}
}).add({
targets: '.ml9',
opacity: 0,
duration: 1000,
easing: "easeOutExpo",
delay: 1000
});