Friday, December 27, 2019

Lab 41 Click and Drag

In this lab, I followed the instruction from JavaScript30 and recreated the Click and Drag 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_41.css"> inside the <head> tags to link the CSS file with my HTML file. Fourthly, I typed in <script src="Lab_41.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 41</h1><h2>Wei Jian Zhen</h2><h3>12/24/2019</h3></div>. Next, I typed in numerous <div> tags and most of them had a number inside them. For the CSS, I typed in html {width: 100%; height: auto;} body {background-color: #00FFA8; min-width: 100%; min-height: 100%;} to make the webpage responsive to screen size changes and have a background color. #heading {text-align: center; font-size: 16px;} to make the heading of the lab centered. #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. .items {height: 800px; padding: 100px; width: 100%; border: 1px solid white; overflow-x: scroll; overflow-y: hidden; white-space: nowrap; user-select: none; cursor: pointer; transition: all 0.2s; transform: scale(0.98); will-change: transform; position: relative; background: rgba(255,255,255,0.1); font-size: 0; perspective: 500px;} .items.active {background: rgba(255,255,255,0.3); cursor: grabbing; cursor: -webkit-grabbing; transform: scale(1);} .item { width: 200px; height: calc(100% - 40px); display: inline-flex; align-items: center; justify-content: center; font-size: 80px; font-weight: 100; color: rgba(0,0,0,0.15); box-shadow: inset 0 0 0 10px rgba(0,0,0,0.15);} .item:nth-child(9n+1) { background: dodgerblue;} .item:nth-child(9n+2) { background: goldenrod;} .item:nth-child(9n+3) { background: paleturquoise;} .item:nth-child(9n+4) { background: gold;} .item:nth-child(9n+5) { background: cadetblue;} .item:nth-child(9n+6) { background: tomato;} .item:nth-child(9n+7) { background: lightcoral;} .item:nth-child(9n+8) { background: darkslateblue;} .item:nth-child(9n+9) { background: rebeccapurple;} .item:nth-child(even) { transform: scaleX(1.31) rotateY(40deg); } .item:nth-child(odd) { transform: scaleX(1.31) rotateY(-40deg); } to make the divs align, colorize the divs, and give sizes to the divs. For the JavaScript I typed:

const slider = document.querySelector('.items');
  let isDown = false;
  let startX;
  let scrollLeft;
  slider.addEventListener('mousedown', (e) => {
    isDown = true;
    slider.classList.add('active');
    startX = e.pageX - slider.offsetLeft;
    scrollLeft = slider.scrollLeft;
  });
  slider.addEventListener('mouseleave', () => {
    isDown = false;
    slider.classList.remove('active');
  });
  slider.addEventListener('mouseup', () => {
    isDown = false;
    slider.classList.remove('active');
  });
  slider.addEventListener('mousemove', (e) => {
    if (!isDown) return;  // stop the fn from running
    e.preventDefault();
    const x = e.pageX - slider.offsetLeft;
    const walk = (x - startX) * 3;
    slider.scrollLeft = scrollLeft - walk;
  });

This JavaScript allows the user to move the divs horizontally by clicking and dragging the divs across the screen. Lastly, I made the copyright notice with <p> and <div> tags. Knowing how to make click and drag objects in a webpage is useful to a web designer because it allows for more interactivity and better transitions for the user who may do business to the website.

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


No comments:

Post a Comment