Sea
Sea
We're going to use JavaScript to change the background of our viewport every second so our water moves.
To do this we are going to create a Controller
constructor/prototype, which is responsible for interacting with and updating the DOM based on the state of our application.
Challenge
Use a combination of setInterval
with a duration of 1000
(1000 milliseconds = 1 second) and document.querySelector
to manipulate the viewport background.
You may need the walkthrough for this step as there are many concepts to cover.
To complete this challenge, you will need to:
- Create a new constructor function called
Controller
insrc/controller.js
. - Create a new prototype method for
Controller
calledinitialiseSea
, and call it from within the constructor. - Import the
controller.js
file into your html using ascript
tag at the end of thebody
. - Add another
script
tag underneathm but this time use inline JavaScript to create a new instance ofController
. - Inside your
initialiseSea
method, usesetInterval
to run a callback function every1000
milliseconds. Inside the callback function usedocument.querySelector
to find the#viewport
element and change the background image so it alternates betweenwater0.png
andwater1.png
. You'll probably want to keep some sort of "counter" variable outside of yoursetInterval
to determine which background image to use.