What You'll Be Creating |
The Demo
The full demo that we’ll be building and discussing for the remainder of this tutorial is also available on CodePen.
The Setup
Using the markup from part I we’ll begin by adding a container div for structural purposes along with correlating classes for CSS and JavaScript hooks.
- <div class="app">
- <header class="dragaebel-lcontainer" role="banner">
- <a href="/" class="js-draglogo">…</a>
- <a href="#menu" class="dragaebel-toggle js-dragtoggle" id="menu-button">…</a>
- <nav class="dragaebel-nav js-dragnav" id="menu" role="navigation">…</nav>
- </header>
- <main role="main">
- <div class="dragaebel-lcontainer js-dragsurface"></div>
- </main>
- </div>
Accessibility
With the the foundation in place it’s time to add a layer of ARIA on top to lend semantic meaning to screen readers and keyboard users.
- <nav aria-hidden="true">…</nav>
Indicates that the element and all of its descendants are not visible or perceivable to any user as implemented by the author. […] Authors MUST set aria-hidden=”true” on content that is not displayed, regardless of the mechanism used to hide it. This allows assistive technologies or user agents to properly skip hidden elements in the document. ~W3C WAI-ARIA SpecAuthors should be careful what content they hide, making this attribute a separate discussion outside the scope of this article. For those curious, the specification defines the attribute in further length and is somewhat grokkable; something I don’t usually say that often about specification jargon.
The CSS
Our CSS is where the magic really begins. Let’s take the important parts from the demo that bear meaning and break it down.
- body {
- // scroll fix
- height: 100%;
- overflow: hidden;
- // end scroll fix
- }
- .app {
- // scroll fix
- overflow-y: scroll;
- height: 100vh;
- // end scroll fix
- }
- .dragaebel-nav {
- height: 100vh;
- overflow-y: auto;
- position: fixed;
- top: 0;
- right: 0;
- }
The overflow scroll fix helps to control how the primary container and navigation behave when either one contains overflowing content. For example, If the container is scrolled—or the menu—the other will not scroll when the user reaches the end of the intially scrolled element. It’s a weird behavior, not typically discussed, but makes for a better user experience.
Viewport Units
Viewport units are really powerful and play a vital role in how the primary container holds overflowing content. Viewport units have wonderful support across browsers these days and I highly suggest you start using them. I’ve used vh units on the nav, but I could have used a percentage instead. During development it was discovered that div.app must use vh units since percentage won’t allow for the overflowing content to maintain typical scrolling behavior; the content results in being clipped. Overflow is set to scroll in preparation in case the menu items exceeed the height of the menu or the height of the viewport becomes narrow.
- // Allow nav to open when JS fails
- .no-js .dragaebel-nav:target {
- margin-right: 0;
- }
- .dragaebel-nav {
- margin-right: -180px;
- width: 180px;
- }
The primary navigation is moved to the right via a negative margin which is also the same as the nav’s width. For the sake of brevity I’m writing Vanilla CSS, but I’m sure you could write something fancier in a pre-processor of your choice.
Written by Dennis Gaebel
If you found this post interesting, follow and support us.
Suggest for you:
JavaScript for Absolute Beginners
JavaScript For Beginners - Learn JavaScript From Scratch
JavaScript for Beginners
JavaScript Bootcamp - 2016
ES6 Javascript: The Complete Developer's Guide
No comments:
Post a Comment