다음은 CSS를 사용하여 스크롤할 때 배경 이미지를 변경하는 코드입니다 -
예시
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1> <style> body, html { height: 100%; margin: 0; font-family: Arial, Helvetica, sans-serif; } * { box-sizing: border-box; } div { height: 40%; background-position: center; background-repeat: no-repeat; background-size: cover; } .image1 { background-image: url("https://i.picsum.photos/id/827/400/400.jpg"); } .image2 { background-image: url("https://i.picsum.photos/id/817/400/400.jpg"); } .image3 { background-image: url("https://i.picsum.photos/id/811/400/400.jpg"); } .image4 { background-image: url("https://i.picsum.photos/id/871/400/400.jpg"); } .image5 { background-image: url("https://i.picsum.photos/id/471/400/400.jpg"); } .image6 { background-image: url("https://i.picsum.photos/id/271/400/400.jpg"); } h1{ position:fixed; top:10%; left: 40%; background-color: rgba(0, 0, 0, 0.527); padding: 40px; color: white; } </style> </head> <body> <div class="image1"></div> <div class="image2"></div> <div class="image3"></div> <div class="image4"></div> <div class="image5"></div> <div class="image6"></div> <h1>Fixed Text</h1> </body> </body> </html>
출력
위의 코드는 다음과 같은 출력을 생성합니다 -