JSFiddle - React, Tailwind, and code Playground

by zakariamouhid

HTML

<nav id="nav">
  Nav
</nav>

CSS

body {
  min-height: 5000px;
}
nav {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  padding: 10px;
  text-align: center;
  font-family: sans-serif;
  background-color: #FF5722;
  font-weight: bold;
  color: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  margin-top: 0;
  transition: margin-top 0.5s;
}
nav[data-show="0"] {
  margin-top: -100%;
}

JavaScript

(function (nav) {
	"use strict";
  var lastY = window.screenTop;
  nav.setAttribute("data-show", "1");
  window.addEventListener("scroll", function () {
  	if (window.pageYOffset - lastY > 0) {
    	nav.setAttribute("data-show", "0");
    } else {
    	nav.setAttribute("data-show", "1");
    }
  	lastY = window.pageYOffset;
  });
}(document.getElementById("nav")));