我来帮你设计一个具有打字机效果的网页,模拟大模型逐字输出的效果。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI 打字机效果</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background-color: #f7f7f8;
color: #374151;
line-height: 1.6;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 40px 20px;
}
h1 {
text-align: center;
color: #1f2937;
margin-bottom: 40px;
font-size: 2.5rem;
font-weight: 600;
}
.input-section {
background: white;
border-radius: 12px;
padding: 24px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
margin-bottom: 24px;
}
textarea {
width: 100%;
min-height: 120px;
padding: 12px 16px;
border: 1px solid #e5e7eb;
border-radius: 8px;
font-size: 16px;
resize: vertical;
font-family: inherit;
transition: border-color 0.2s;
}
textarea:focus {
outline: none;
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}
.button-group {
display: flex;
gap: 12px;
margin-top: 16px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
}
.primary-btn {
background-color: #3b82f6;
color: white;
}
.primary-btn:hover {
background-color: #2563eb;
}
.primary-btn:disabled {
background-color: #9ca3af;
cursor: not-allowed;
}
.secondary-btn {
background-color: #f3f4f6;
color: #374151;
}
.secondary-btn:hover {
background-color: #e5e7eb;
}
.output-section {
background: white;
border-radius: 12px;
padding: 24px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
min-height: 200px;
}
.output-content {
font-size: 16px;
line-height: 1.8;
color: #1f2937;
white-space: pre-wrap;
word-wrap: break-word;
}
.cursor {
display: inline-block;
width: 2px;
height: 1.2em;
background-color: #3b82f6;
animation: blink 1s infinite;
vertical-align: text-bottom;
margin-left: 2px;
}
@keyframes blink {
0%, 50% { opacity: 1; }
51%, 100% { opacity: 0; }
}
.speed-control {
margin-top: 16px;
display: flex;
align-items: center;
gap: 12px;
}
.speed-control label {
font-size: 14px;
color: #6b7280;
}
.speed-control input[type="range"] {
flex: 1;
height: 6px;
background: #e5e7eb;
border-radius: 3px;
outline: none;
-webkit-appearance: none;
}
.speed-control input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 16px;
height: 16px;
background: #3b82f6;
border-radius: 50%;
cursor: pointer;
}
.speed-control input[type="range"]::-moz-range-thumb {
width: 16px;
height: 16px;
background: #3b82f6;
border-radius: 50%;
cursor: pointer;
border: none;
}
.speed-value {
font-size: 14px;
color: #374151;
min-width: 50px;
text-align: right;
}
</style>
</head>
<body>
<div class="container">
<h1>AI 打字机效果演示</h1>
<div class="input-section">
<textarea id="inputText" placeholder="在这里输入你想要展示的文字..."></textarea>
<div class="speed-control">
<label for="speedSlider">输出速度:</label>
<input type="range" id="speedSlider" min="10" max="200" value="50">
<span class="speed-value" id="speedValue">50ms</span>
</div>
<div class="button-group">
<button class="primary-btn" id="startBtn" onclick="startTyping()">开始输出</button>
<button class="secondary-btn" id="pauseBtn" onclick="pauseTyping()" style="display: none;">暂停</button>
<button class="secondary-btn" onclick="clearOutput()">清空</button>
</div>
</div>
<div class="output-section">
<div class="output-content" id="output"></div>
</div>
</div>
<script>
let typingInterval;
let currentIndex = 0;
let isPaused = false;
let currentText = '';
// 速度滑块事件
const speedSlider = document.getElementById('speedSlider');
const speedValue = document.getElementById('speedValue');
speedSlider.addEventListener('input', function() {
speedValue.textContent = this.value + 'ms';
});
function startTyping() {
const inputText = document.getElementById('inputText').value;
if (!inputText.trim()) {
alert('请先输入一些文字!');
return;
}
currentText = inputText;
const output = document.getElementById('output');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
// 如果是继续播放
if (isPaused) {
isPaused = false;
pauseBtn.textContent = '暂停';
typeWriter();
return;
}
// 重新开始
currentIndex = 0;
output.innerHTML = '';
startBtn.style.display = 'none';
pauseBtn.style.display = 'inline-block';
typeWriter();
}
function typeWriter() {
const output = document.getElementById('output');
const speed = parseInt(document.getElementById('speedSlider').value);
if (currentIndex < currentText.length && !isPaused) {
// 移除之前的光标
const existingCursor = output.querySelector('.cursor');
if (existingCursor) {
existingCursor.remove();
}
// 添加下一个字符
const char = currentText.charAt(currentIndex);
output.innerHTML += char;
// 添加光标
output.innerHTML += '<span class="cursor"></span>';
currentIndex++;
// 继续下一个字符
typingInterval = setTimeout(typeWriter, speed);
} else if (currentIndex >= currentText.length) {
// 完成输出
const existingCursor = output.querySelector('.cursor');
if (existingCursor) {
existingCursor.remove();
}
document.getElementById('startBtn').style.display = 'inline-block';
document.getElementById('pauseBtn').style.display = 'none';
document.getElementById('startBtn').textContent = '重新开始';
}
}
function pauseTyping() {
const pauseBtn = document.getElementById('pauseBtn');
if (!isPaused) {
isPaused = true;
clearTimeout(typingInterval);
pauseBtn.textContent = '继续';
} else {
isPaused = false;
pauseBtn.textContent = '暂停';
typeWriter();
}
}
function clearOutput() {
clearTimeout(typingInterval);
document.getElementById('output').innerHTML = '';
document.getElementById('inputText').value = '';
document.getElementById('startBtn').style.display = 'inline-block';
document.getElementById('pauseBtn').style.display = 'none';
document.getElementById('startBtn').textContent = '开始输出';
currentIndex = 0;
isPaused = false;
}
// 示例文本
document.getElementById('inputText').value = '人工智能正在改变我们的世界。从自然语言处理到计算机视觉,从推荐系统到自动驾驶,AI技术正在各个领域发挥着越来越重要的作用。\n\n大语言模型的出现,让机器能够更好地理解和生成人类语言,这为人机交互开启了全新的可能性。';
</script>
</body>
</html>
这个网页实现了以下功能:
-
逐字输出效果:模拟ChatGPT的token-by-token输出方式,文字会一个字一个字地显示出来
-
闪烁光标:在输出过程中显示一个蓝色闪烁的光标,增强视觉效果
-
速度控制:通过滑块可以调整输出速度(10ms-200ms之间)
-
暂停/继续功能:可以随时暂停和继续输出
-
美观的界面设计:
- 采用现代化的卡片式设计
- 柔和的阴影和圆角
- 响应式布局
- 优雅的颜色搭配
- 平滑的过渡动画
-
交互体验:
- 输入框支持多行文本
- 按钮有悬停效果
- 焦点状态有视觉反馈
你可以直接将这个HTML文件保存并在浏览器中打开使用。如果需要调整样式或添加其他功能,可以告诉我具体需求。