JavaScript의 scroll 이벤트는 사용자가 스크롤바를 위아래로 움직여 상호작용할 때 발생합니다.
다음은 JavaScript의 스크롤 이벤트에 대한 코드입니다 -
예시
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
height: 150vh; /*To have a scrollbar*/
}
.result{
font-size: 18px;
color: blueviolet;
font-weight: 500;
}
</style>
</head>
<body>
<h1>Scroll events in JavaScript</h1>
<div class="result"></div>
<h3>Scroll the scrollbar to see some text in the above paragraph</h3>
<script>
let resEle = document.querySelector(".result");
window.addEventListener("scroll", () => {
resEle.innerHTML = "The scroll event has fired";
});
</script>
</body>
</html> 출력

조금 아래로 스크롤하면 -
