This function will be used to animate the cloud's movement across the viewport by getting the element's left position and adding to it incrementally on each scroll. It receives in the function call the variable's element and increment. In the function, you will first work with the element's style left property using the split method on the string p.
We need to add some failsafe fallback here in case the value is not a number. This could happen if there isn't any value for the style's left property. To verify whether this value is a number, first use the parseInt function on the value you just created and check that it's not NaN (Not a Number). If this logical step passes, return the value of the bounding rectangle's left property of the element, with the string px appended to it.
If the logical test fails, using else, get the integer value of the element's style left property by using the parseInt function and then the absolute value of it, and then add the increment and finally append the string px to it and return it. That was easy enough. Take a look at the code:
function setElementLeftPosition(element,increment){
if
(isNaN(parseInt(element.style.left.split("p")[0])))
{
return ((element.getBoundingClientRect().left)+increment) + "px"
}
else {
return ((Math.abs(parseInt(element.style.left.split("p")[0]))) + increment) + "px";
}
}There are still some errors due to missing functions, but the finish line is in sight. We have two more functions to write, and then the big reveal happens.