Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 심장질환
- Vue.js
- 뇌출혈
- 프로그래밍
- javascript
- 추가납입
- PythonProgramming
- 수수료
- 보험료
- 보험
- 파이썬
- 급성심근경색증
- 웹개발
- Java
- 가입
- 인출수수료
- jQuery
- python
- 납입
- 중도인출
- 리스트
- 교보
- 자바스크립트
- 특약
- 변환
- 사망
- 프론트엔드
- 교보생명
- 문자열
- 코딩
Archives
- Today
- Total
SeouliteLab
jQuery .scroll() 메서드: 스크롤 이벤트 처리하기 본문
예제 1: 페이지 상단에서 아래로 스크롤할 때 메시지 표시
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery .scroll() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
$("#scrollMessage").text("스크롤을 내립니다.");
} else {
$("#scrollMessage").text("");
}
});
});
</script>
<style>
body {
height: 2000px; /* 스크롤이 발생할 정도의 높이 설정 */
}
#scrollMessage {
position: fixed;
top: 10px;
left: 10px;
background-color: lightblue;
padding: 10px;
border: 1px solid blue;
}
</style>
</head>
<body>
<div id="scrollMessage"></div>
</body>
</html>
설명: 이 예제는 페이지를 스크롤할 때 스크롤 위치를 감지하여 페이지 상단에서 아래로 스크롤할 때 메시지를 표시합니다.
예제 2: 스크롤 이벤트로 요소 이동하기
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery .scroll() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$(window).scroll(function(){
if ($(this).scrollTop() > 500) {
$("#floatingBox").css("top", "100px");
} else {
$("#floatingBox").css("top", "10px");
}
});
});
</script>
<style>
body {
height: 2000px; /* 스크롤이 발생할 정도의 높이 설정 */
}
#floatingBox {
position: fixed;
top: 10px;
left: 10px;
width: 100px;
height: 100px;
background-color: lightblue;
border: 1px solid blue;
}
</style>
</head>
<body>
<div id="floatingBox"></div>
</body>
</html>
설명: 이 예제는 페이지를 스크롤할 때 요소를 동적으로 이동시킵니다.
예제 3: 스크롤 이벤트 해제하기
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery .scroll() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
var scrollHandler = function() {
console.log("스크롤 중...");
};
$(window).scroll(scrollHandler);
$("#unbindButton").click(function(){
$(window).off("scroll", scrollHandler);
console.log("스크롤 이벤트 핸들러가 해제되었습니다.");
});
});
</script>
</head>
<body>
<button id="unbindButton">스크롤 이벤트 핸들러 해제</button>
</body>
</html>
설명: 이 예제는 스크롤 이벤트 핸들러를 등록하고, 버튼을 클릭하여 해당 핸들러를 해제하는 방법을 보여줍니다.
예제 4: 스크롤 이벤트로 배경 색상 변경하기
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery .scroll() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
$("body").css("background-color", "lightgray");
} else {
$("body").css("background-color", "white");
}
});
});
</script>
<style>
body {
height: 2000px; /* 스크롤이 발생할 정도의 높이 설정 */
transition: background-color 0.5s;
}
</style>
</head>
<body>
<p>내용 내용 내용 내용...</p>
</body>
</html>
설명: 이 예제는 페이지를 스크롤할 때 배경 색상을 변경합니다.