<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>程序员编程写代码CSS3动画特效</title>
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: #1e1e1e;
overflow: hidden;
font-family: 'Courier New', monospace;
}
.desk {
position: relative;
width: 800px;
height: 500px;
background: #2d2d2d;
border-radius: 10px;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
overflow: hidden;
}
.monitor {
position: absolute;
width: 700px;
height: 400px;
background: #121212;
border: 15px solid #333;
border-radius: 5px;
top: 30px;
left: 50px;
overflow: hidden;
}
.screen {
position: absolute;
width: 100%;
height: 100%;
background: #1e1e1e;
padding: 20px;
box-sizing: border-box;
color: #d4d4d4;
font-size: 16px;
line-height: 1.5;
overflow: hidden;
}
.code-line {
margin: 5px 0;
white-space: nowrap;
opacity: 0;
animation: type 0.3s forwards;
}
.keyword {
color: #569cd6;
}
.function {
color: #dcdcaa;
}
.string {
color: #ce9178;
}
.comment {
color: #6a9955;
}
.number {
color: #b5cea8;
}
.cursor {
display: inline-block;
width: 10px;
height: 20px;
background: #d4d4d4;
animation: blink 1s infinite;
vertical-align: middle;
}
.keyboard {
position: absolute;
width: 600px;
height: 50px;
background: #333;
border-radius: 5px;
bottom: 30px;
left: 100px;
}
.key {
position: absolute;
width: 30px;
height: 30px;
background: #444;
border-radius: 3px;
bottom: 10px;
box-shadow: 0 2px 0 #222;
animation: key-press 0.3s infinite alternate;
}
.key:nth-child(1) {
left: 50px;
animation-delay: 0.1s;
}
.key:nth-child(2) {
left: 100px;
animation-delay: 0.3s;
}
.key:nth-child(3) {
left: 150px;
animation-delay: 0.5s;
}
.key:nth-child(4) {
left: 200px;
animation-delay: 0.2s;
}
.key:nth-child(5) {
left: 250px;
animation-delay: 0.4s;
}
.coffee {
position: absolute;
width: 60px;
height: 80px;
background: #6f4e37;
border-radius: 5px 5px 20px 20px;
right: 30px;
bottom: 80px;
}
.coffee::before {
content: '';
position: absolute;
width: 70px;
height: 20px;
background: #6f4e37;
border-radius: 50%;
top: -10px;
left: -5px;
}
.steam {
position: absolute;
width: 10px;
height: 30px;
background: rgba(255, 255, 255, 0.7);
border-radius: 5px;
right: 50px;
bottom: 160px;
filter: blur(2px);
animation: steam-rise 3s infinite;
}
.steam:nth-child(1) {
right: 40px;
animation-delay: 0s;
}
.steam:nth-child(2) {
right: 60px;
animation-delay: 0.5s;
}
.title {
position: absolute;
top: 30px;
color: #569cd6;
font-size: 24px;
text-align: center;
width: 100%;
}
.link {
position: absolute;
bottom: 20px;
right: 20px;
color: #569cd6;
font-size: 14px;
text-decoration: none;
font-weight: bold;
}
@keyframes type {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}
@keyframes key-press {
0% { transform: translateY(0); }
100% { transform: translateY(5px); }
}
@keyframes steam-rise {
0% { transform: translateY(0) scale(1); opacity: 0.7; }
100% { transform: translateY(-50px) scale(0.5); opacity: 0; }
}
</style>
</head>
<body>
<h1 class="title">程序员编程动画</h1>
<div class="desk">
<div class="monitor">
<div class="screen" id="screen">
<!-- 代码将通过JS动态添加 -->
</div>
</div>
<div class="keyboard">
<div class="key"></div>
<div class="key"></div>
<div class="key"></div>
<div class="key"></div>
<div class="key"></div>
</div>
<div class="coffee"></div>
<div class="steam"></div>
<div class="steam"></div>
</div>
<script>
// 示例代码
const codeLines = [
'<span class="keyword">function</span> <span class="function">calculateSum</span>(a, b) {',
' <span class="keyword">const</span> sum = a + b; <span class="comment">// 计算两数之和</span>',
' <span class="keyword">return</span> sum;',
'}',
'',
'<span class="keyword">class</span> <span class="function">Person</span> {',
' <span class="function">constructor</span>(name, age) {',
' <span class="keyword">this</span>.name = name;',
' <span class="keyword">this</span>.age = age;',
' }',
'',
' <span class="function">greet</span>() {',
' <span class="keyword">return</span> <span class="string">`Hello, my name is ${this.name}`</span>;',
' }',
'}',
'',
'<span class="keyword">const</span> numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];',
'<span class="keyword">const</span> doubled = numbers.map(n => n * <span class="number">2</span>);',
'',
'<span class="keyword">const</span> result = <span class="function">calculateSum</span>(<span class="number">10</span>, <span class="number">20</span>);',
'console.log(<span class="string">"Result:"</span>, result);<span class="cursor"></span>'
];
const screen = document.getElementById('screen');
// 逐行显示代码
function typeCode() {
codeLines.forEach((line, index) => {
setTimeout(() => {
const div = document.createElement('div');
div.className = 'code-line';
div.innerHTML = line;
screen.appendChild(div);
// 滚动到底部
screen.scrollTop = screen.scrollHeight;
}, index * 500);
});
// 循环动画
setTimeout(() => {
screen.innerHTML = '';
typeCode();
}, codeLines.length * 500 + 2000);
}
// 开始动画
typeCode();
</script>
</body>
</html>
330

被折叠的 条评论
为什么被折叠?



