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

Finally, moving the earth

Create a new function called moveEarth and give it the variable earth. First, define the earth variable's style fontSize property to be equal to the window's innerHeight property multiplied by the number of row DIV elements, and subtract from it the element identified by the id rocket's bounding client rectangle's bottom property and then divide it by the window's innerHeight value; multiply the whole thing by 100, and then append the string vw so that the size is pinned to the viewport width. The function and its first line look like this:

    function moveEarth(earth){
        earth.style.fontSize=(((window.innerHeight * (document.getElementsByClassName("row").length) - document.getElementById("rocket").getBoundingClientRect().bottom)/window.innerHeight) * 100)+"vw";
}

The second line of the moveEarth function will set the earth's style left value to be equal to exactly the value of the fontSize previously defined, except change 100 at the end to the number of row DIV elements multiplied by 2 and instead of vw, append the string px. Take a look:

earth.style.left = (((window.innerHeight * (document.getElementsByClassName("row").length) - document.getElementById("rocket").getBoundingClientRect().bottom) / window.innerHeight) + document.getElementsByClassName("row").length * 2) + "px";

The next line defines the earth's style height property. Define it as the value of the window's innerHeight property multiplied by the number of row DIV elements minus the window's pageYOffset value, and divide the result by the window's innerHeight value plus 1/2, and then append the string % to the end. This is illustrated in the following sample:

earth.style.height = (window.innerHeight * (document.getElementsByClassName("row").length)-window.pageYOffset) / window.innerHeight+.5 + '%';

The rest of this function will be used to set style values of the earth's first child element identified by the tag name I. The first property to define is transform.

Start with the string rotate (and end with the string deg), and in between, the value is 15 plus the result of the window's innerHeight value multiplied by the result of the number of row elements minus the rocket element's bounding client rectangle's bottom property, which is then divided by the window's innerHeight value, and then, that result is multiplied by 4. The code looks like this:

earth.getElementsByTagName("i")[0].style.transform = "rotate(" + (15 + (((window.innerHeight * (document.getElementsByClassName("row").length) - document.getElementById("rocket").getBoundingClientRect().bottom)/window.innerHeight)) * 4) + "deg)";

Next, define the right property of the style as the window's pageYOffset value divided by its innerHeight value and multiplied by the integer 45, then append the string %. Whew, that was much easier!

earth.getElementsByTagName("i")[0].style.right = (window.pageYOffset / window.innerHeight * 45) + '%';

The next line defines the bottom property of the style. Its definition is the same as the left property, but change the integer 45 to 200:

earth.getElementsByTagName("i")[0].style.bottom = (window.pageYOffset / window.innerHeight * 200) + '%';

The final line of code we need to write in this function just happens to be the final line of code we need to write for the application. It sets the style's opacity property of the element, and it's pretty simple compared to most of the previous. It equals 1.3 minus the product of the window's pageYOffset value divided by the window's innerHeight value, multiplied by the number of row DIV elements. And you're done, look at the code here:

earth.getElementsByTagName("i")[0].style.opacity = 1.3-(window.pageYOffset / (window.innerHeight * (document.getElementsByClassName("row").length)));
} //this closes the function you were working on.

Now for the big reveal! Open the file in your browser or refresh it. You will see a number of new things happening. The biggest difference from the previous view is that the earth is no longer a big blue blob, but a big blue-and-green ball that gracefully falls away from the rocket as it exits the stratosphere. And you will see how the rocket arcs in a curve as it shoots away from the earth. In fact, there are a number of moving parts that operate as a function of the updating scrolls.

This project was tonnes of fun for me to create, as I hope it was for you. Again, like I said in the beginning of this chapter, I would like to see what you make of this code, so check out the GitHib code and fork it.

Finally, moving the earth
Finally, moving the earth