Table of Contents for
Web Design Blueprints

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Web Design Blueprints by Benjamin LaGrone Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. Web Design Blueprints
  4. Web Design Blueprints
  5. Credits
  6. About the Author
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Conventions
  13. Reader feedback
  14. Customer support
  15. 1. Responsive Web Design
  16. Getting familiar with the basics
  17. Using media queries for responsive design
  18. Working with responsive media
  19. Building responsive layouts
  20. Summary
  21. 2. Flat UI
  22. Flat UI color
  23. Creating a flat UI layout
  24. Summary
  25. 3. Parallax Scrolling
  26. Color classes
  27. Using SVG font icons
  28. Getting the fonts
  29. That's no moon!
  30. OMG, it's full of stars!
  31. Clouds, birds, and airplanes
  32. The rocket
  33. Terra firma
  34. Next up, the CSS
  35. Styling the objects with CSS
  36. Styling the ground objects
  37. Writing the JavaScript effects
  38. Setting the row height
  39. Spreading the objects
  40. Spreading the clouds
  41. Loading the page functions
  42. Smoothening the scroll
  43. Updating elements on the scroller
  44. Collecting the moving elements
  45. Creating functions for the element types
  46. Setting the left positions
  47. Creating the rocket's movement function
  48. Finally, moving the earth
  49. Summary
  50. 4. Single Page Applications
  51. Getting to work
  52. Getting the old files
  53. Object and function conventions
  54. Creating utility functions
  55. Working with the home structure
  56. Setting up other sections
  57. Performing housekeeping
  58. Creating a callBack function for the API
  59. Summary
  60. 5. The Death Star Chapter
  61. Dropping in the parallax game
  62. Loading elements from JSON
  63. What can be done in the shared levels service
  64. Editing the home JavaScript
  65. Creating the other pages – credits and leaderboard
  66. Creating the second level
  67. Summary
  68. Index

Spreading the objects

Let's continue. Next, we will want to create a function that will spread the objects randomly up and down each row. It will be used on the stars in the space row, the clouds in the sky row, and the buildings and trees at the bottom. The next function will need you to create a new function called spreadObjects() and have it receive the variables x, vm, hm, ha, p, and e.

These variable names represent the data they will receive: x for an array of objects, vm for the vertical multiplier, hm for the horizontal multiplier, va for vertical addition, ha for horizontal addition, p for position type, and e for the extension (px or %).

With x, we will loop through the array of objects and, for each object in the loop, set its style position to the variable p, the style to be a random number multiplied by vm with va added to it, and finally, the e added to the end. We will do the same for the object style left property and vm, vh, and e. Look at this code for the function:

    function spreadObjects(x, vm, hm, va, ha, p, e){
        for (var I = 0; I < x.length; i++){
            x[i].style.position = p;
            x[i].style.top = Math.floor((Math.random()*vm)+va)+e;
            x[i].style.left = Math.floor((Math.random()*hm)+ha)+e;
        }
    }

Back to the original onload function; call spreadObjects() with the first variable being the array of the object when you get the element by the stars id attribute, and from that, get the elements by their I tag names. The subsequent variables will be 150, 100, 1, 1, fixed, and %.

Taking a look at the code, I can tell you what will happen: the function will distribute the stars throughout the element they are within 100% from the left and 150% from the top, in a fixed position.

Take a look at the function call:

spreadObjects(document.getElementById("stars").getElementsByTagNam
e("i"), 150, 100, 1, 1, "fixed", "%");

We are going to call this function a few more times now. All three of the first variables passed start with getting the element by the ground id attribute. The first will get the elements inside it that have the fa-tree class attribute.

These are only going to be distributed horizontally, not vertically, so the next variable is 0 and then 14, and then, we need some math to determine the inner height of the window, since the elements will be a few pixels from the absolute bottom, divided by 28, then 1 for the horizontal addition, with the position type relative, followed finally by px for the extension.

The next two function calls will get the elements that have the class name right from within the ground element, select the first of the array, and then get the elements that have the class name small and then large. The numbers are mostly the same, but the innerHeight values are divided by 13 and 15 respectively. I'll show this to you in the following code because I'm trying to keep the explanation brief:

spreadObjects(document.getElementById("ground").getElementsByClass
Name("fa-tree"), 0, 14, -(window.innerHeight/28), 1, "relative", "px");

spreadObjects(document.getElementById("ground").getElementsByClass
Name("right")[0].getElementsByClassName("small"), 0, 14,-
(window.innerHeight/13), 1, "relative", "px");

spreadObjects(document.getElementById("ground").getElementsByClass
Name("right")[0].getElementsByClassName("large"), 0, 14,-
(window.innerHeight/15), 1, "relative", "px");

You may be wondering why we are not creating variables and inserting them into the function calls. The answer is we could, but then our code would take up more room, and we would essentially be duplicating something that's already in the DOM, which is unnecessary bloat.

Now, look at your browser, and see how the stars have been scattered throughout space.

Spreading the objects