π― Unit 4 Activities
Table of Contents
π» ACTIVITY PROGRAM SETUP INSTRUCTIONS
- Go to the public template repository for our class: BWL-CS HTML/CSS/JS Template
- Click the button above the list of files then select
Create a new repository
- Specify the repository name:
CS1-Unit4-Activity#
Replace
#
with the specific activity number. - Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositories
section of GitHub! π - Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
and wait for the environment to load, then youβre ready to code!
π When class ends, donβt forget to SAVE YOUR WORK! Codespaces are TEMPORARY editing environments, so you need to COMMIT changes properly in order to update the main repository for your program.
There are multiple steps to saving in GitHub Codespaces:
- Navigate to the
Source Control
menu on the LEFT sidebar - Click the button on the LEFT menu
- Type a brief commit message at the top of the file that opens, for example:
updated main.py
- Click the small
βοΈ
checkmark in the TOP RIGHT corner - Click the button on the LEFT menu
- Finally you can close your Codespace!
π‘ ACTIVITY #1: Light & Dark Mode Toggle
To demonstrate how JavaScript can be implemented in HTML/CSS webpages, we will create a usable light/dark mode toggle button using variables, functions, conditionals, and JavaScriptβs DOM (Document Object Model) library.
PART A: Build a Simple HTML Page
- In
index.html
add the following starter code inside the<body>
section:
<div class="container">
<h1 id="main-heading">Welcome to My Page</h1>
<p id="description">This page supports light and dark mode!</p>
<button id="toggle">Switch to Dark Mode</button>
</div>
- We give the
<div>
aclass
calledcontainer
so we can select it in CSS.- Thereβs a
<button>
element with anid
calledtoggle
so we can target it in JavaScript.- Note that the other elements have their own
id
values as wellβ¦
PART B: Add Basic Styles in CSS
- In
styles.css
apply the following styling rules to the whole page:body { font-family: sans-serif; margin: 0; padding: 5px; }
- Leave out the colors so we can set them via JavaScript!
- Center the div that contains the main content:
.container { width: 60%; margin: auto; text-align: center; }
- Style the button:
button { padding: 2px; margin-top: 5px; font-size: 24px; }
</div>
PART C: Write JavaScript for Theme Toggling
- Create a boolean (
true
/false
) variable to remember if weβre in dark mode or not:let isDarkMode = false;
- Before you can change anything on the page, you need to select the HTML elements you want to work with.
const body = document.querySelector('body'); const heading = document.querySelector('#main-heading'); const description = document.querySelector('#description'); const toggleButton = document.querySelector('#toggle');
- The
querySelector()
function lets you select any element using its tag,class
, orid
. - Weβre selecting the button, the body (for background), the heading, and the paragraph. All of these βselectionsβ are stored in named variables to be accessed later.
const
(constant) is used as the keyword to declare variables here instead oflet
, becausequerySelector
returns just a REFERENCE to the elementβs location, which does not change.
- The
- Define a function that toggles the color theme mode:
function toggleMode() { // Code statements that modify the page for each mode }
- Inside the function, start by flipping the value of
isDarkMode
:// Flip the boolean with the NOT (!) operator isDarkMode = !isDarkMode;
- Next inside the function, add conditional statements to handle each mode depending on whether
isDarkMode
istrue
or not:if (isDarkMode) { // Apply DARK MODE styles and text } else { // Apply LIGHT MODE styles and text }
- In the
if (isDarkMode)
block, change the style properties and text content of the elements we selected earlier:// Apply DARK MODE styles and text body.style.backgroundColor = 'black'; body.style.color = 'white'; heading.style.color = '#ffcc00'; description.textContent = 'Dark mode is now enabled!'; toggleButton.textContent = 'Switch to Light Mode';
- In the
else
block, change the style properties and text content for light mode:// Apply LIGHT MODE styles and text body.style.backgroundColor = 'white'; body.style.color = 'black'; heading.style.color = '#003366'; description.textContent = 'Light mode is now enabled!'; toggleButton.textContent = 'Switch to Dark Mode';
- When the user clicks on the button, we want to trigger the
toggleMode()
function. AFTER the function definition, include this statement that adds an event listener to register button clicks:toggleButton.addEventListener('click', toggleMode);
- This tells the browser: βWhen the button is clicked, call the
toggleMode()
function.β - We donβt use parentheses here (
toggleMode()
β β). Just pass the function name so it runs when clicked.
- This tells the browser: βWhen the button is clicked, call the
π JavaScript Skills To Use!
Concept | Example | What It Does |
---|---|---|
querySelector() | const element = document.querySelector('#btn'); | Function that selects an HTML element |
Event Listener | element.addEventListener('click', function); | Runs a function when something is clicked |
.style.property | element.style.color = 'red'; | Changes a CSS property with JavaScript |
.textContent | element.textContent = 'Hello!'; | Changes what text is displayed in an element |
Boolean Toggle | isDarkMode = !isDarkMode; | Switches true β false |
Refer to the notes page for detailed explanations: π Notes 4.4: HTML DOM Operations
PART D: Add Creative Features
You are required to attempt at least TWO of the creative features suggested below to earn full credit on this project!
- Change Images Based on the Mode
- πΌοΈ Add an image of a sunny landscape to the HTML:
<img id="image" src="day.jpg">
- Since you added a new HTML element to the document, you should create a JavaScript named variable for it as well. Call it
imageElement
and select it from thedocument
the same way we selected theheading
,toggleButton
, etc. at the top of your script.HINT: Scroll up and look for all the
const
declarations. Follow the same pattern to select your image! - Inside your
toggleMode
function, switch the imageβssrc
attribute:if (isDarkMode) { imageElement.src = 'night.jpg'; } else { imageElement.src = 'day.jpg'; }
HINT: You already have an
if
andelse
block, so do not just copy-paste the code above. Figure out where you need to add the specific line that changes thesrc
for each mode.
- πΌοΈ Add an image of a sunny landscape to the HTML:
- Customize the Pageβs Appearance
- π¨ If you loved the CSS unit, dive back into it and enhance the overall style of your page!
- First, refine the base styles in
style.css
by setting more properties (border
,text-shadow
,box-shadow
,font-family
, etc.) - Next, add JavaScript statements in the
script.js
toggle function to make even more visual differences between the two themes. Refer here: HTML DOM Style Object Properties to see what else you can change inside yourtoggleMode
function. - Decorate with emojis/icons, for example, you could replace text with π and π icons for a fun visual toggle. BONUS: Animate the icons using CSS
@keyframes
. - Make the background and text color changes fade smoothly by adding this
transition
:body { transition: background-color 0.4s ease, color 0.4s ease; }
- Provide Multiple Color Themes
- Design and enable more than two color theme options. β οΈ Note that every additional theme will require its own HTML
<button>
and JavaScriptfunction
! - Check out trending color palettes on Coolors.co to use as inspiration.
- Pick gradients from SheCodes.io to use for
background
instead of a plain color.
- Design and enable more than two color theme options. β οΈ Note that every additional theme will require its own HTML
- Keyboard Shortcut to Toggle Theme
- β¨οΈ Add an event listener for keyboard input:
function handleKeydown(event) { if (event.key == 't') { toggleMode(); } } document.addEventListener('keydown', handleKeydown);
- β¨οΈ Add an event listener for keyboard input:
- Save Userβs Preference with
localStorage
- π₯ When a user selects dark mode, save their preference so it stays even after a refresh.
- Try this:
localStorage.setItem('theme', 'dark'); // or 'light'
- And on page load:
const savedTheme = localStorage.getItem('theme'); if (savedTheme == 'dark') { toggleMode(); // or set dark styles manually }