Rotating Page NavBar
Code Breakdown
Introduction
This project adds an interesting take on the standard Navigation Bar. Typically you would have something like a horizontal nav bar at the top of the page, or a vertical menu that displays when you click on a "Hamburger" icon.
Instead, with this concept, when you click on the "hamburger" icon, the entire page is going to rotate counter-clockwise 20°, and then display a hidden vertical nav bar in the left bottom portion of the screen. We set the pivot point for the rotation to be the top left corner of the page, so when the page rotates, it opens up that bottom left area that the page has rotated away from.
The complete source code for this project can be downloaded here.
the HTML
With the html, we create a "parent" container <div> that holds the circle at the top left corner, and our "main page" content.
The circle will hold our icons that display to either open or close our "nav bar". And the main page consists of a basic page containing an <h1>, <h3>, a couple or "lorem ipsum" paragraphs, and an image.
After the "parent" container, we have an unordered list inside a <nav> container, that become our actual nav links. It's important to note that this <nav> container is after the main "parent" container and is a separate entity unto itself. It is not part of the "parent" container.
Initially, this <nav> container is hidden and when the page rotates, it becomes visible in the bottom left corner of the page. This functionality is controlled with JavaScript by adding or removing the show-nav class to the main container <div>.
We are utilizing font-awesome in this page so we need the following link in the head section of our page:
- link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.1/css/all.min.css">
and here is the html code:
- HTML
- // main "parent" container
- <div class="container">
-
- // container for the "nav" circle
- <div class="circle-container">
- <div class="circle">
- // font-awesome coding
- <button id="open"><i class="fas fa-bars"></i></button>
- <button id="close"><i class="fas fa-times"></i></button>
- </div>
- <div class="circle">
- </div>
- // container for the "main page" content
- <div class="content">
- <h1>Rotating Navigation</h1>
- <p>lorem3</p>
- <h3>Mad Dog Chico</h3>
- <img src="images/100_0145.JPG" alt="Chico" />
- <p>lorem2</p>
- </div>
- // container for the "nav bar" content
- <nav>
- <ul>
- // font-awesome coding
- <li><i class="fas fa-home">Home</i></li>
- <li><i class="fas fa-user-alt">About</i></li>
- <li><i class="fas fa-envelope">Contact</i></li>
- <li></li>
- <li>*non-functional links<br/>display purposes only</li>
- </ul>
- <ul>
- </nav>
- </div>
the CSS
The css for this is very interesting. We start with some basic styling for the body, headings, paragraphs, image, and nav links, for appearance purposes only.
We're giving the overall <body> a background color of #333 and the main container <div> a background color of #fafafa. This gives us a white background for the page, and when we rotate, we see a dark background with white text, for the navigation links.
Let's start with the styling for the nav links:
- css
- nav {
- position: fixed;
- bottom: 40px;
- left: 0;
- z-index: 100;
- }
- nav ul {
- list-style-type: none;
- padding-left: 30px;
- }
- nav ul li {
- text-transform: uppercase;
- color: #fff;
- margin: 35px 0;
- transform: translateX(-300%);
- transition: transform 0.4s ease-in;
- }
- nav ul li i {
- font-size: 16px;
- letter-spacing: 0.25em;
- }
- nav ul li:nth-of-type(3) {
- margin-left: 15px;
- transform: translateX(-500%);
- }
- nav ul li:nth-of-type(2) {
- margin-left: 30px;
- transform: translateX(-1100%);
- }
- nav ul li:last-child {
- margin-left: 60px;
- text-transform: lowercase;
- font-size: 13px;
- font-style: italic;
- line-height: 1.5;
- }
nav: sets the nav <div> to a "fixed" position 40px up from the bottom of the page, with a z-index of 100 to place it on top of all other elements on the page.
nav ul: removes "bullets" from the list and move it right 30px.
nav ul li: text-transform/color/margin are setting the styling for each list item in the <nav> ul, which is our navigation links. And this is where it gets interesting:
- as stated above we're going to use a "show-nav" class to either show or hide this <nav> section. As a default, it is hidden. So we have the lines:
- transform: translateX(-300%);
- sets the default "hidden" position to be 300px to the left of the page, effectively hiding the section.
- transition: transform 0.4s ease-in;
- sets an "ease-in" transition on the transform causing a gradual change when the <nav> section is shown or hidden.
- transform: translateX(-300%);
nav ul li i: is setting the styling for the font-awesome images for the nav links.
nav ul li:nth-of-type(2), and nav ul li:nth-of-type(3) are just setting different horizontal positions for the nav links so that when displayed, the links are offset from to the right, instead of in a vertical column.
and nav ul li:last-child is setting some styling for the information <li> indicating that the <nav> section is non-functional.
Next we need to style our main "container" which holds the main body of our page. This styling defines the page rotation that happens when our JavaScript instructs the page to rotate.
Remember that the JavaScript will control this page rotation through "click" events. And when a "click" event is detected, this styling determines how the page actually rotates.
Here's the code:
- css
- .container {
- background-color: #fafafa;
- width: 100vw;
- padding: 50px 75px;
- font-size: 16px;
- transform-origin: top left;
- transition: transform 0.5s linear;
- }
the JavaScript