我正在使用这种未来派风格的文字效果,并希望创建如下效果:
首先应设置动画短语:
测试短语
之后,在此词组之后添加一个单词,例如:
狗 猫
所以结尾短语是:
测试词组狗
或
测试词组cat
第一个词只可以动画一次,第二个词可以互换,先显示狗,然后显示猫,然后重复。
我目前发现了一种解决方法,可以使用CSS使第二个短语在一段时间后出现,但这不是理想的情况。可以在JavaScript中进行设置吗?
另外,由于某种原因,第二个短语在第一次出现时没有空格,使它像这样:
测试词组猫
这可能是什么原因?
// Futuristic Resolving/Typing Text Effect
// Copyright (c) by Kevin (https://codepen.io/qkevinto/pen/WQVNWO)
// License (MIT): https://choosealicense.com/licenses/mit/
var resolver = {
resolve: function resolve(options, callback) {
// The string to resolve
var resolveString = options.resolveString || options.element.getAttribute('data-target-resolver');
var 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) {
var characters = options.characters;
var timeout = options.timeout;
var element = options.element;
var partialString = options.partialString;
var iterations = options.iterations;
setTimeout(function () {
if (iterations >= 0) {
var 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) {
var resolveString = options.resolveString;
var characters = options.characters;
var offset = options.offset;
var partialString = resolveString.substring(0, offset);
var combinedOptions = Object.assign({}, options, { partialString: partialString });
doRandomiserEffect(combinedOptions, function () {
var nextOptions = Object.assign({}, options, { offset: offset + 1 });
if (offset <= resolveString.length) {
doResolverEffect(nextOptions, callback);
} else if (typeof callback === "function") {
callback();
}
});
};
doResolverEffect(combinedOptions, callback);
}
/* Some GLaDOS quotes from Portal 2 chapter 9: The Part Where He Kills You
* Source: http://theportalwiki.com/wiki/GLaDOS_voice_lines#Chapter_9:_The_Part_Where_He_Kills_You
*/ };
var strings = [
'Test phrase '];
var counter = 0;
var 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(function () {
counter++;
if (counter >= strings.length) {
counter = 0;
}
var nextOptions = Object.assign({}, options, { resolveString: strings[counter] });
resolver.resolve(nextOptions, callback);
}, 100000000);
}
resolver.resolve(options, callback);
// Futuristic Resolving/Typing Text Effect
// Copyright (c) by Kevin (https://codepen.io/qkevinto/pen/WQVNWO)
// License: (MIT) https://choosealicense.com/licenses/mit/
var resolver = {
resolve: function resolve(options, callback) {
// The string to resolve
var resolveString = options.resolveString || options.element.getAttribute('data-target-resolver2');
var 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) {
var characters = options.characters;
var timeout = options.timeout;
var element = options.element;
var partialString = options.partialString;
var iterations = options.iterations;
setTimeout(function () {
if (iterations >= 0) {
var 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) {
var resolveString = options.resolveString;
var characters = options.characters;
var offset = options.offset;
var partialString = resolveString.substring(0, offset);
var combinedOptions = Object.assign({}, options, { partialString: partialString });
doRandomiserEffect(combinedOptions, function () {
var nextOptions = Object.assign({}, options, { offset: offset + 1 });
if (offset <= resolveString.length) {
doResolverEffect(nextOptions, callback);
} else if (typeof callback === "function") {
callback();
}
});
};
doResolverEffect(combinedOptions, callback);
}
/* Some GLaDOS quotes from Portal 2 chapter 9: The Part Where He Kills You
* Source: http://theportalwiki.com/wiki/GLaDOS_voice_lines#Chapter_9:_The_Part_Where_He_Kills_You
*/ };
var strings = [
'cat',
'dog'];
var counter = 0;
var 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-resolver2]')
// Callback function when resolve completes
};function callback() {
setTimeout(function () {
counter++;
if (counter >= strings.length) {
counter = 0;
}
var nextOptions = Object.assign({}, options, { resolveString: strings[counter] });
resolver.resolve(nextOptions, callback);
}, 3000);
// }, 3000);
}
// setTimeout(resolver.resolve(options, callback), 5000);
resolver.resolve(options, callback);
.second-line {
-webkit-animation: appear 2.5s ease-out;
animation: appear 2.5s ease-out; }
@keyframes appear {
0% {
opacity: 0;
display: none;
position: absolute; }
80% {
opacity: 0;
display: none;
position: absolute; }
100% {
opacity: 1; } }
<h1><span data-target-resolver></span><span data-target-resolver2 c class="second-line"></span></h1>
答案 0 :(得分:0)
对于空间问题,您可以在
之间添加不间断空格(span
),并删除/忽略脚本中的空间。
对于函数调用链(1 =解析“测试短语”,2 =解析“ Cat”,等等),您可以将resolve
调用用于解析2到回调函数1中({{1 }})。在第一句完成动画处理后,将调用此方法。 (并完全删除CSS动画。)