最近在制作文字游戏时需要打字机效果,研究了一下实现了一个支持选项显示和鼠标点击跳过的打字机效果宏,分享给大家。
将以下代码添加到你的 JavaScript 部分:
/* ---------分割线-----------文字打印js----------------- */
setup.typewriter = (() => {
let currentTyping = null;
function typewriter(element, text, speed = 50, choiceElement = null) {
element.textContent = '';
element.classList.add('typing');
const cursor = document.createElement('span');
cursor.className = 'cursor';
element.appendChild(cursor);
document.addEventListener('mousedown', handleSkip);
return new Promise(resolve => {
currentTyping = { abort: false };
document.removeEventListener('mousedown', handleSkip);
element.innerHTML = text;
const char = text.charAt(i);
element.insertBefore(document.createElement('br'), cursor);
element.insertBefore(document.createTextNode(char), cursor);
document.removeEventListener('mousedown', handleSkip);
choiceElement.style.display = 'block';
choiceElement.style.opacity = '1';
choiceElement.style.transform = 'translateY(0)';
Macro.add('typewriter', {
return this.error('No typing speed specified');
const speed = this.args[0];
const $wrapper = $('<div/>').addClass('typewriter');
$choices = $('<div/>').addClass('choice-container').css({
'transform': 'translateY(20px)',
'transition': 'all 0.5s ease'
$choices.wiki(this.payload[1].contents);
this.output.appendChild($wrapper[0]);
this.output.appendChild($choices[0]);
setup.typewriter($wrapper[0], this.payload[0].contents, speed, $choices ? $choices[0] : null);
/* ---------分割线-----------文字打印js----------------- */
transform: translateY(20px);
transition: all 0.5s ease;
background: linear-gradient(45deg, #2a2a2a, #333);
transition: all 0.2s ease;
.choice-container a:hover {
background: linear-gradient(45deg, #333, #3a3a3a);
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
animation: blink 1s step-end infinite;
- typewriter 后面的数字(50)是打字速度,数字越小越快
1. 确保代码放在正确的位置(JavaScript放在StoryScript,CSS放在StyleSheet)