Script Valley
JavaScript in the Browser: DOM, Events & APIs
Styles, Classes, and Layout via JavaScript/Assessment

Practice & Assessment

Test your understanding of Styles, Classes, and Layout via JavaScript

Multiple Choice Questions

5
1

element.style.color returns an empty string. What does this mean?

2

Which classList method accepts a second boolean argument to force-add or force-remove a class?

3

Why is requestAnimationFrame preferred over setInterval for JavaScript animations?

4

getBoundingClientRect().top returns 0. What does this mean?

5

When should you use IntersectionObserver instead of a scroll event listener?

Coding Challenges

1
1

Scroll-Triggered Fade-In Gallery

Given 12 image placeholder divs (.gallery-item) styled with opacity:0, use IntersectionObserver with a threshold of 0.3 to add class .visible (opacity:1, transition:opacity 0.4s) when each enters the viewport. Unobserve each element after it becomes visible. Input: user scrolling the page. Output: items fade in as they enter the viewport and stay visible. Constraint: no scroll event listeners. Estimated time: 20-25 minutes.

Medium

Mini Project

1

Animated Sidebar Navigation

Build a collapsible sidebar. Requirements: a hamburger button (#toggle-btn) toggles class .sidebar-open on a #sidebar element using classList. The sidebar slides in via CSS transition (transform: translateX). A dark overlay (#overlay) appears when the sidebar is open; clicking it closes the sidebar. All open/close buttons use event delegation on document. Implement a scroll progress bar (#scroll-bar) at the top of the page: on each scroll event update its width using element.style.width calculated from window.scrollY and document.body.scrollHeight minus window.innerHeight. Use requestAnimationFrame to throttle scroll handler updates.

Medium
Practice & Assessment โ€” Styles, Classes, and Layout via JavaScript โ€” JavaScript in the Browser: DOM, Events & APIs โ€” Script Valley โ€” Script Valley