<link href="https://fonts.googleapis.com/css2?family=Quicksand:[email protected];400;500;600;700&display=swap" rel="stylesheet">
<div class="logo">
<a href="https://codeorum.com/" class="codeorum"><img alt="codeorum" src="https://codeorum.com/images/codeorum-logo-dark.svg"></a>
<a href="https://zovko.pro/" class="mirko-zovko"><img alt="mirko-zovko" src="https://codeorum.com/images/zovko-logo-dark.svg"></a>
</div>
<div class="container">
<h1>Ultra simple slider/slideshow with pure js and animation effect </h1>
<!-- Nav menu container -->
<nav class="menu" id="myMenu">
<ul>
<!-- active link and hover indicator, menu slider -->
<div class="m-active"></div>
<!-- menu links -->
<li><a class="active" href="#">Home</a></li> <!-- active menu -->
<li><a href="#">Gallery</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</div>
body{
margin: 0;
padding: 0;
background: #FFC371;
background: linear-gradient(to right, #9cecfb, #65c7f7, #0052d4);
font-family: 'Quicksand', sans-serif;
color: #262626;
}
*{
box-sizing: border-box;
}
.logo {
position: fixed;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
left: 10px;
top: 10px;
flex-direction: row;
z-index: 1000;
a {
display: block;
height: 30px;
width: auto;
cursor: pointer;
margin-right: 10px;
img {
height: 100%;
width: auto;
}
}
}
.container{
display: flex;
flex-direction: column;
width: 100%;
max-width: 1000px;
margin: 0 auto;
padding: 50px 10px 100px 10px;
h1{
text-align: center;
margin-bottom: 30px;
font-weight: 500;
}
h2{
font-weight: 500;
}
}
// Nav menu container
.menu{
display: block;
width: 100%;
height: auto;
margin-top: 50px;
position: relative;
// unsorted list style with material shadow
ul{
position: relative;
display: flex;
margin: 0;
padding: 0 30px;
list-style: none;
width: 100%;
flex-direction: row;
align-items: center;
background-color: #262626;
border-radius: 6px;
box-shadow: 0 20px 25px -5px rgba(0,0,0,.15),0 10px 10px -5px rgba(0,0,0,.1);
@media screen and (max-width: 768px) {
padding: 0 15px;
}
// list element with z-index so that text of the menu can be infront of the menu effect slider
li{
z-index: 10;
// link style
a{
display: block;
text-decoration: none;
padding: 15px 20px;
color: #eee;
font-weight: 500;
font-size: 18px;
@media screen and (max-width: 768px) {
font-size: 14px;
padding: 10px 15px;
}
}
}
// active link and hover indicator, menu slider
.m-active{
position: absolute;
top: -10px;
bottom: -10px;
width: 0px;
background-color: #097ce7;
transition: all 0.2s linear;
// top m-active element part
&:after{
content: '';
display: block;
position: absolute;
background-color: #085cac;
width: 100%;
height: 10px;
transform: skew(-45deg, 0);
left: -5px;
z-index: -1;
}
// bottom m-active element part
&:before{
content: '';
display: block;
position: absolute;
background-color: #085cac;
width: 100%;
height: 10px;
transform: skew(45deg, 0);
left: -5px;
z-index: -1;
bottom: 0;
}
}
}
}
View Compiled
function initMenu(elem){
// function that returns m-active element on active link
returnToActiveElement();
function returnToActiveElement(){
// find active link and get its width and left offset
var active_menu = document.querySelectorAll(elem + " .active");
Array.prototype.forEach.call(active_menu, function (e) {
var width = e.offsetWidth;
var left = e.offsetLeft;
// find m-active element and give him width and left offset of active link
var active_menu_slider = document.querySelectorAll(elem + " .m-active");
Array.prototype.forEach.call(active_menu_slider, function (el) {
el.style.width = width+"px";
el.style.left = left+"px";
});
});
}
// get all links of the menu
var menu_list = document.querySelectorAll(elem + " a");
Array.prototype.forEach.call(menu_list, function (e) {
// mouseenter function, getting width and left offset of the hover link
e.addEventListener("mouseenter", function( event ) {
var width = e.offsetWidth;
var left = e.offsetLeft;
// find m-active element and give him width and left offset of hover link
var active_menu_slider = document.querySelectorAll(elem + " .m-active");
Array.prototype.forEach.call(active_menu_slider, function (el) {
el.style.width = width+"px";
el.style.left = left+"px";
});
}, false);
// on mouseleave return m-active element to the active link
e.addEventListener("mouseleave", function( event ) {
returnToActiveElement();
}, false);
// on link click, make that link active
e.addEventListener("click", function( event ) {
if (!event.target.matches(elem+' a')) return;
else{
if(!event.target.classList.contains('active')){
var active_menu = document.querySelectorAll(elem + " .active");
Array.prototype.forEach.call(active_menu, function (el) {
el.classList.remove("active");
});
event.target.classList.add("active");
}
}
});
});
// on window resize call returnToActiveElement funciton to change the style
window.addEventListener("resize", returnToActiveElement);
}
// start menu
initMenu("#myMenu");
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.