Friday, December 27, 2019

Lab 27 Slide in on Scroll

In this lab, I followed the instruction from JavaScript30 and recreated the Slide in on Scroll activity. First off, I made the HTML, CSS, and JavaScript files. Secondly, the <html>, <head>, <body>, <meta>, and <title> tags were already inserted in the HTML file. Thirdly, I typed in <link rel="stylesheet" type="text/css" href="Lab_27.css"> inside the <head> tags to link the CSS file with my HTML file. Fourthly, I typed in <script src="Lab_27.js" type="text/javascript"></script> inside the <body> tags which linked my JavaScript file with my HTML file. Inside the <body> tags, I typed the heading for the lab which has my name, date, and lab number with <div id="heading"><h1>Lab 27</h1><h2>Wei Jian Zhen</h2><h3>12/24/2019</h3></div>. Next I typed in <p> and <img> tags for the main content of the lab. For the CSS I did html {width: 100%; height: auto;} body {background-color: #00FFA8; min-width: 100%; min-height: 100%;} to give the webpage a background color and make the webpage responsive to screen size changes. #heading {text-align: center; font-size: 16px;} to center the heading of the lab. #copyright {font-size: 16px; font-family: Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", "serif"; font-weight: normal; padding-left: 1%; padding-right: 1%;} to change the font of the copyright notice. Next, I typed in .site-wrap {max-width: 700px;margin: 100px auto;background: white; padding: 40px; text-align: justify;}.align-left {float: left; margin-right: 20px;}.align-right {float: right; margin-left: 20px;}.slide-in {opacity: 0; transition: all .5s;} .align-left.slide-in {transform: translateX(-30%) scale(0.95);} .align-right.slide-in {transform: translateX(30%) scale(0.95);} .slide-in.active {opacity: 1; transform: translateX(0%) scale(1);} to make the text and images be animated when scrolled. For the JavaScript, I put:

// JavaScript Document

function debounce(func, wait = 20, immediate = true) {
      var timeout;
      return function() {
        var context = this, args = arguments;
        var later = function() {
          timeout = null;
          if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
      };
    };
    const sliderImages = document.querySelectorAll('.slide-in');
    function checkSlide() {
      sliderImages.forEach(sliderImage => {
        // half way through the image
        const slideInAt = (window.scrollY + window.innerHeight) - sliderImage.height / 2;
        // bottom of the image
        const imageBottom = sliderImage.offsetTop + sliderImage.height;
        const isHalfShown = slideInAt > sliderImage.offsetTop;
        const isNotScrolledPast = window.scrollY < imageBottom;
        if (isHalfShown && isNotScrolledPast) {
          sliderImage.classList.add('active');
        } else {
          sliderImage.classList.remove('active');
        }
      });
    }
    window.addEventListener('scroll', debounce(checkSlide));

This makes the images and text show when the user scrolls down. Lastly, I made the copyright notice with <p> and <div> tags. Knowing how to make images appear when the user scroll is useful when making websites that contain a lot of text and images.

Link: http://techteach.us/Web2020/ZWeiJian/WCP/Labs/Lab_27/Lab_27.html


No comments:

Post a Comment