screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Scroll Position Example</title> <style> body { height: 200vh; /* Make the body taller to enable scrolling */ margin: 0; font-family: Arial, sans-serif; } #scrollPosition { position: fixed; top: 10px; right: 10px; padding: 10px; background-color: #f1f1f1; border: 1px solid #ddd; border-radius: 5px; } </style> </head> <body> <h1>Javascript get scroll position</h1> <div id="scrollPosition">Scroll Position: 0</div> <script> // Function to update the scroll position function updateScrollPosition() { var scrollPosition = window.scrollY; document.getElementById('scrollPosition').innerText = "Scroll Position: " + scrollPosition; } // Attach the function to the scroll event window.addEventListener('scroll', updateScrollPosition); // Initial update updateScrollPosition(); </script> </body> </html>