비전공자 개발일기

Javascript - Auto Text Effect 본문

Javascript

Javascript - Auto Text Effect

HiroDaegu 2021. 10. 11. 09:23
728x90
SMALL

입력값(1 ~ 10)에 따른 글자 출력 속도 조절

<!DOCTYPE html>
<html lang="ko">
<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">
  <title>Auto Text Effect</title>
  <link rel="stylesheet" href="style.css">
  <script defer src="main.js"></script>
</head>
<body>
  <h1 id="text">Starting...</h1>
  <div>
    <label for="speed">Speed:</label>
    <input type="number" name="speed" id="speed" value="1" min="1" max="10" step="1">
  </div>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

* {
  box-sizing: border-box;
}

body {
  background-color: #e9967a;
  font-family: 'Roboto', sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}

div {
  position: absolute;
  bottom: 20px;
  background: rgba(0, 0, 0, .1);
  padding: 10px 20px;
  font-size: 18px;
}

input {
  width: 50px;
  padding: 5px;
  font-size: 18px;
  background-color: #e9967a;
  border: none;
  text-align: center;
}

input:focus {
  outline: none;
}
const textEl = document.getElementById("text");
const speedEl = document.getElementById("speed");
const text = "We Love Programming!";
let index = 1;
let speed = 300 / speedEl.value;

writeText();

function writeText() {
  textEl.innerText = text.slice(0, index);

  index++;

  if(index > text.length) {
    index = 1;
  }

  setTimeout(writeText, speed)
}

speedEl.addEventListener("click", (e) => speed = 300 / e.target.value)
728x90
LIST

'Javascript' 카테고리의 다른 글

Javascript - Content Placeholder  (0) 2021.10.13
Javascript - Button Ripple Effect  (0) 2021.10.12
Javascript - Animated Countdown  (0) 2021.10.10
Javascript - Animated Navigation  (0) 2021.10.09
Javascript - 3D Background Boxes  (0) 2021.10.07