A landing page implemented as part of Professional Web Development Track offered by egFWD Initiative through Udacity.
To use this project, First clone the repository on your local device using the commands below:
git clone https://github.com/ibrahimelmokhtar/js-sample-landing-page.git
(Back to top)
In this section, I will explain how the code works and how everything is put together.
This project has the structure shown below:
css
- styles.css
index.html
js
- app.js
README.md
This website has a Responsive Design. This means: "All features are usable across modern desktop, tablet, and phone browsers."
When a section is in the viewport, the following actions will happen:
- this section will be highlighted indicating it's the active section.
- This active section will be highlighted in the Navbar.
At the beginning, there are FOUR sections that have been added to the page.
Navigation is built dynamically as an unordered list. The document
object has an event listener
which is listening for DOMContentLoaded
event. When the DOM
content is loaded, the following actions are performed:
- Collect the required data from each displayed section using
id
anddata-nav
attributes. - Clear Navbar list, if exists; to start with empty Navbar list.
- Create a
fragment
element using.createDocumentFragment()
function to append the created navbar list elements into it. - Loop through the collected data to create
li
elements; and set eachinnerHTML
value to a specifica
element; then append thisli
element tofragment
. - Append the created
fragment
to Navbar after adding all desiredli
elements to it.
It shows which section is being viewed while scrolling through the page. The document
object has an event listener
which is listening for scroll
events. While scrolling, the following actions are performed:
- Obtain ALL displayed sections into single array to manipulate them easily.
- Get the
top
value of current viewport using.getBoundingClientRect()
function. - Loop through all sections to check if the current section passes specific conditions.
- if true, Set the current section to
active
via manipulating itsclassList
.
When clicking an item from the navigation menu, the link should scroll smoothly to the appropriate section.
Navigation menu has an event listener
which is listening for click
events. When clicked, the following actions are performed:
- Prevent the default action; By using
.preventDefault()
function. - Set the
scroll behavior
tosmooth
. - Check if an appropriate element is clicked; By checking its
href
value. - if it's an appropriate element, then use
.scrollIntoView()
function to scroll into the appropriate section.
By highlighting the navbar link to the appropriate section, It shows which section is being viewed while scrolling through the page. The document
object has an event listener
which is listening for scroll
events. While scrolling, the following actions are performed:
NOTE: Using the same algorithm explained previously at Section Active State.
- Obtain ALL displayed sections into single array to manipulate them easily.
- Get the
top
value of current viewport using.getBoundingClientRect()
function. - Loop through all sections to check if the current section passes specific conditions.
- if true, extract the navbar list item of the current section.
- Set this list item to
active
via manipulating itsclassList
.
NOTE: The idea behind this implementation is formulated after reading this great post:
Detecting when a visitor has stopped scrolling with vanilla JavaScript.
The document
object has an event listener
which is listening for scroll
events. While scrolling, the following actions are performed:
- Clear the
global
scrolling timeout ID usingclearTimeout()
function. - Remove a specific class,
hide
, from navbarclassList
which will show the navbar. - Set the
global
scrolling timeout ID usingsetTimeout()
function. - The
setTimeout()
function will execute a specific function after a desired period of time,HIDE_TIMEOUT
, which will Add the classhide
to navbarclassList
.
The Add Section
button has a list of event listeners
, each is listening to a specific event
as following:
pointerenter
event: Show the button's text, which isAdd Section
, with some transition appearance.pointerleave
event: Hide the button's text.click
event:- Get the
main
element and ALL the displayed sections inside it. - Obtain the last section data,
id
anddata-nav
attributes, if there is ANY section; if NOT, Create a virtual section withid='section0'
anddata-nav='Section 0'
to continue the next step without any errors. - Update the last section's collected data by increasing its values by 1.
- Create a new
section
element then set the newid
anddata-nav
attributes to it. - Set the
innerHTML
value of this newsection
element to a specific valueLorem ipsum
. - Append this new section to the
main
element.
- Get the
The Delete Section
button has a list of event listeners
, each is listening to a specific event
as following:
pointerenter
event: Show the button's text, which isDelete Section
, with some transition appearance.pointerleave
event: Hide the button's text.click
event:- Get ALL the displayed sections inside the
main
element. - Check the number of the obtained sections; As you may want to leave the page with a specific/minimum number of sections displayed on it, this number is set through the
MIN_SECTION_THRESHOLD
global variable. - Remove the last
section
from themain
element.
- Get ALL the displayed sections inside the
The Go to Top
button has a list of event listeners
, each is listening to a specific event
as following:
pointerenter
event: Show the button's text, which isGo to Top
, with some transition appearance.pointerleave
event: Hide the button's text.click
event:- Set
(x, y)
, also known as(left, top)
, to(0, 0)
. - Set the
scroll behavior
tosmooth
. - use
.scrollTo()
function to scroll to the appropriate position, which is the start of the window.
- Set