Ship
Ship
We now want to bring a Ship
into the equation.
Challenge
Create a new css rule that targets elements with an id of ship
and sets the background-image to the ship.png
file. Instantiate a new Ship
object in JavaScript and then add a new method to the controller that selects's a #ship
div and positions it under the .port
element that corresponds to the ship's currentPort
.
To complete this challenge, you will need to:
- Create a new instance of
Ship
inside the inline JS inindex.html
, passing in your itinerary. - Create a new
div
with the idship
as a child of the#viewport
div, and as a sibling of the.ports
element. - In
style.css
add a new#ship
ruleset. You want to use theship.png
background image, and specify a width and height that matches that image's dimensions. You should also specify aposition
ofabsolute
as later on we will want to control our element's X/Y co-ordinates within the viewport. - Add a
renderShip
method to yourController.prototype
, which has a single parameter ofship
. - Back in
index.html
, callcontroller.renderShip
and pass in yourship
from before. - In the
renderShip
method, find the index of the ship'scurrentPort
inside of itsitinerary
. Usedocument.querySelector
with an attribute selector to find a.port
element that has aportIndex
data attribute which corresponds to this index. - Prior to appending to the DOM, set the
top
andleft
CSS properties of your ship element to the offsetTop and offsetLeft values for the port element. - Add or subtract from your
offsetLeft
and/oroffsetTop
values to position your ship so it's centered directly underneath the port.