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>

效果如下👇

posted @ 2022-06-09 23:42  CoderWGB  阅读(38)  评论(0)    收藏  举报