🎯 Unit 4 Activities

Table of Contents


πŸ’» ACTIVITY PROGRAM SETUP INSTRUCTIONS
  1. Go to the public template repository for our class: BWL-CS HTML/CSS/JS Template
  2. Click the button above the list of files then select Create a new repository
  3. Specify the repository name: CS1-Unit4-Activity#

    Replace # with the specific activity number.

  4. Click

    Now you have your own personal copy of this starter code that you can always access under the Your repositories section of GitHub! πŸ“‚

  5. Now on your repository, click and select the Codespaces tab
  6. 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:

  1. Navigate to the Source Control menu on the LEFT sidebar
  2. Click the button on the LEFT menu
  3. Type a brief commit message at the top of the file that opens, for example: updated main.py
  4. Click the small βœ”οΈ checkmark in the TOP RIGHT corner
  5. Click the button on the LEFT menu
  6. 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

  1. 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> a class called container so we can select it in CSS.
  • There’s a <button> element with an id called toggle 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

  1. 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!
  2. Center the div that contains the main content:
      .container {
       width: 60%;
       margin: auto;
       text-align: center;
      }
    
  3. Style the button:
      button {
       padding: 2px;
       margin-top: 5px;
       font-size: 24px;
      }
    

</div>

PART C: Write JavaScript for Theme Toggling

  1. Create a boolean (true/false) variable to remember if we’re in dark mode or not:
      let isDarkMode = false;
    
  2. 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, or id.
    • 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 of let, because querySelector returns just a REFERENCE to the element’s location, which does not change.
  3. Define a function that toggles the color theme mode:
      function toggleMode() {
       // Code statements that modify the page for each mode
      }
    
  4. Inside the function, start by flipping the value of isDarkMode:
      // Flip the boolean with the NOT (!) operator
      isDarkMode = !isDarkMode; 
    
  5. Next inside the function, add conditional statements to handle each mode depending on whether isDarkMode is true or not:
      if (isDarkMode) {
       // Apply DARK MODE styles and text
      }
      else {
       // Apply LIGHT MODE styles and text
      }
    
  6. 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';
    
  7. 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';
    
  8. 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.

πŸ†• 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!

  1. 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 the document the same way we selected the heading, 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’s src attribute:
       if (isDarkMode) {
         imageElement.src = 'night.jpg';
       }
       else {
         imageElement.src = 'day.jpg';
       }
      

      HINT: You already have an if and else block, so do not just copy-paste the code above. Figure out where you need to add the specific line that changes the src for each mode.

  2. 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 your toggleMode 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;
        }
      
  3. 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 JavaScript function!
    • 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.
  4. Keyboard Shortcut to Toggle Theme
    • ⌨️ Add an event listener for keyboard input:
       function handleKeydown(event) {
         if (event.key == 't') {
         toggleMode();
         }
       }
       document.addEventListener('keydown', handleKeydown);
      
  5. 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
       }