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

Updating elements on the scroller

To fix the error, start by creating a new function called updateElement. You may recall that we added this to the body's onscroll method. Now that we have distributed our objects in a random fashion on the screen and created a smooth scroll effect, we need to work on making things change at different rates as we scroll through the rows. This is the meat and potatoes of the parallax effect. In the end, when you scroll through the page, the objects can be programmed to move at different velocities.

This part will involve some fancy footwork and will be explained in a few steps, but it will be lightweight and easy to extend later as you please. The following code contains a function that will be run on every scroll, so we want to keep it as light as possible.

Inside the new updateElement function, in the first line, we will create a call to another function, sending an anonymous callback function. This function will get the elements we need to move and send the function to apply the styles. The anonymous callback function will take two variables, theObject and increment, and set the theObject variable's style position to be relative and the style's left value to send variables to another function called setElementPosition, which we will define shortly:

function updateElement() {
    function (theObject,increment){
        theObject.style.position = "relative";
        theObject.style.left = setElementLeftPosition(theObject,increment);
        });
    }

Next, we will call two more functions. The functions create some unique movements on each scroll that cannot be boiled down to a general function like the clouds and other moving objects. The first, called moveEarth, will send as a variable the element identified by the id earth, and the second function, called moveRocket, will take as its variable the element identified by the id rocket:

        moveEarth(document.getElementById("earth"));

        moveRocket(document.getElementById("rocket"));

The last part of this function will loop through the Font Awesome I elements inside the DIV element identified by the id stars, and each one will set the style opacity to equal the product of the window's pageYOffest value divided by the window's innerHeight property, multiplied by the total number of elements identified by the class row, and subtracted from 1, and from this final value, 0.3 will be subtracted. This for loop is the last part of the updateElement function. I know it's not easy to read such a description of the math, but you can look here at the code sample to see what I'm building:

for (var i = 0; i < document.getElementById("stars").getElementsByTagName("i").length; i++)
{
document.getElementById("stars").getElementsByTagName("i")[i].style.opacity = (1-(window.pageYOffset/(window.innerHeight*(document.getElementsByClassName("row").length))) -.3);
}
} //closes the function

Next, we need to fill in the functions we just referenced; otherwise, the parallax scroller will not be fully operational. The first one, called getMovingElements, takes the variable callback and starts by looping through the elements in the document identified by the row class name. What you will do in this section is determine whether each of the row elements is currently in the viewport. You could add or remove object sets here and control their detection and vertical and horizontal movement.

Inside the for loop, we need to test whether the row it is looping through is actually inside the viewport. Start with this conditional statement: "if the window's pageYOffset property added to the window's innerHeight property is greater than the current row element's offSetTop (distance from top) property and if the window's pageYOffset value is less than the current row element's offSetTop value added to two-thirds of the value of the window's innerHeight property, do the following":