50Days50Projects之day06-文字自动打印出场效果
<!-- run -->
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@500&display=swap');
:root {
--green-color: #136a10;
}
.box {
font-family: 'Inter', sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
overflow: hidden;
background-color: #000;
color: var(--green-color);
font-weight: bold;
}
.content {
font-size: 20px;
}
</style>
<div class="box">
<p> <b> I'am a coder </b> </p>
<div class="content" id="content"></div>
</div>
<script>
const content = document.getElementById('content')
const text = 'Stay hungry, stay foolish. Talk is cheap, show me the code.'
let idx = 1
let speed = 60
writeText()
function writeText() {
content.innerText = text.slice(0, idx)
idx++
if (idx > text.length) {
idx = 1
}
setTimeout(writeText, speed)
}
</script>
JS
const content = document.getElementById('content')
const speedEl = document.getElementById('speed')
const text = 'Stay hungry, stay foolish. Talk is cheap, show me the code.'
let idx = 1
let speed = 300 / speedEl.value
writeText()
function writeText() {
content.innerText = text.slice(0, idx)
idx++
if (idx > text.length) {
idx = 1
}
setTimeout(writeText, speed)
}
speedEl.addEventListener('input', (e) => (speed = 300 / e.target.value))
CSS
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@500&display=swap');
:root {
--green-color: #136a10;
}
body {
font-family: 'Inter', sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
overflow: hidden;
background-color: #000;
color: var(--green-color);
font-weight: bold;
}
.content {
font-size: 20px;
}
.speed {
position: absolute;
bottom: 20px;
background-color: rgba(57, 150, 39, 0.189);
border-radius: 3px;
padding: 10px 20px;
}
input {
width: 50px;
padding: 5px;
font-size: 18px;
background-color: var(--green-color);
border: none;
text-align: center;
color: #fff;
border-radius: 5px;
}
input:focus {
outline: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Otomatik Metin Efekti</title>
</head>
<body>
<h1 id="header">Hi there, I am Ozan</h1>
<div class="content" id="content"></div>
<div class="speed">
<label for="speed">Hız:</label>
<input
type="number"
name="speed"
id="speed"
value="1"
step="1"
min="1"
max="5"
/>
</div>
<script src="app.js"></script>
</body>
</html>
效果如下👇
未经作者授权,禁止转载
本文来自博客园,作者:CoderWGB,转载请注明原文链接:https://www.cnblogs.com/wgb1234/articles/16361650.html
THE END

浙公网安备 33010602011771号