<예시>
1.
2.
<script>
function scrollIntoView(selector) {
const scrollTo = document.querySelector(selector);
scrollTo.scrollIntoView({behavior: "smooth"});
}
</script>
<예시>
1.
2.
// Make home slowly fade to transparent as the window scrolls down
<script>
const home = document.querySelector('.home__container');
const homeHeight = home.getBoundingClientRect().height;
document.addEventListener('scroll', () => {
home.style.opacity = 1 - window.scrollY / homeHeight;
});
</script>
3.
// Show "arrow up" button when scrolling down
<script>
const arrowUp = document.querySelector('.arrow-up');
document.addEventListener('scroll', () => {
if (window.scrollY > homeHeight / 2) {
arrowUp.classList.add('visible');
} else {
arrowUp.classList.remove('visible');
}
});
</script>
4.
// Make navbar transparent when it is on the top
<script>
const navbar = document.querySelector('#navbar');
const navbarHeight = navbar.getBoundingClientRect().height;
document.addEventListener('scroll', () => {
if (window.scrollY > navbarHeight) {
navbar.classList.add('navbar--dark');
} else {
navbar.classList.remove('navbar--dark');
}
});
</script>