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

Creating the rocket's movement function

Our rocket is the craft we are focusing on in our parallax movement. It should slowly move up or down as we scroll. We want it to slowly move towards the top of the viewport as you move up through the row elements. It will eventually reach to the top of the screen and its final target, the moon. As it moves up, it will also slightly rotate to the right as it arcs up into space. Altogether, this will create a really cool effect.

The function will be named moveRocket, and it will take the variable rocket and apply styles to its child elements. It is invoked during the updateElement function, so as you scroll through the page, this function will move the rocket.

In the first line, get the span elements that are children to the rocket element, and apply the style transform equal to the value rotate to the first, and here is some more JavaScript math: the window's innerHeight property multiplied by the number of rows, minus the rocket's bounding client rectangle's bottom value, divided by the window's innerHeight value, all subtracted from the integer 355 and divided by 3. Then, append the string deg to the end. This magic algorithm makes the rocket's rotation a factor of if its location in the scrolling.

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

The next line is similar; in fact, I want you to copy and paste it. Then, change the part on the left of the equals sign to get the elements by the tag name I, selecting 2 in the array, and to the right of the equals sign, change the integer from 355 to 259. This slightly modifies the rotation of the fa-flame I element as it is a different size and orbit.

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

The next line will cause the rockets to move up through the viewport as you scroll up. Instead of selecting the style's transform property, select the style's bottom property. Set it equal to 65 multiplied by the rocket's distance from the bottom divided by the value of the window's innerHeight property multiplied by the number of row elements, and then append the string % to the end. Have a look:

rocket.getElementsByTagName("span")[0].style.bottom = 65 * (document.getElementById("rocket").getBoundingClientRect().bottom)/ (window.innerHeight * (document.getElementsByClassName("row").length)) + '%';

The final function, now that we have moved the heavens, is to move the earth. This function will shrink the earth elements and rotate them as the rocket zooms up into space or as the user scrolls from the bottom to the top. The earth will not be noticeable at first as the rocket will launch from the ground, and the earth is still a very large blue blob obscured in the background.