JSFiddle - React, Tailwind, and code Playground
by jupiterjackrabbit
HTML
<div id="block-one" class="block"></div>
<div id="block-two" class="block"></div>
<div id="block-three" class="block"></div>
<div id="block-four" class="block"></div>
<div id="block-five" class="block"></div>
<ul class="nav-wrap">
<li class="nav-item" data-hook="one"></li>
<li class="nav-item" data-hook="two"></li>
<li class="nav-item" data-hook="three"></li>
<li class="nav-item" data-hook="four"></li>
<li class="nav-item" data-hook="five"></li>
</ul>
CSS
.block {
position: relative;
top: 0;
left: 0;
width: 100%;
height: 500px;
background: pink;
}
.block:nth-child(even) {
background-color: orange;
height: 900px;
}
.nav-wrap {
position: fixed;
width: auto;
height: auto;
right: 30px;
}
.nav-item {
list-style: none;
width: 20px;
height: 20px;
background-color: red;
margin-bottom: 10px;
cursor: pointer;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
transition:all .3s linear;
-o-transition:all .3s linear;
-moz-transition:all .3s linear;
-webkit-transition:all .3s linear;
}
.nav-item:hover, .nav-item.selected {
background-color: blue;
}
JavaScript
jQuery(document).ready(function ($) {
Resize();
});
//Every resize of window
$(window).resize(function () {
Resize();
});
//Dynamically assign height
function Resize() {
// Handler for .ready() called.
var divwid = $(window).height() / 2,
navhei = $('#side-nav').height() / 2,
newhei = divwid - navhei;
$('#side-nav').css({
'top': newhei
});
}
var blockHeight = [];
$("section").each(function() {
blockHeight.push($(this).height());
});
function hoverCurrentItem() {
var sIndex = 0;
var totalHeight = 0;
var scrolled = $(window).scrollTop();
for (var i in blockHeight) {
totalHeight += blockHeight[i];
if (totalHeight > scrolled) {
break;
}
else {
sIndex++;
}
}
var $sItem = $(".nav-item").eq(sIndex);
if (!$sItem.hasClass("selected")) {
$(".selected").removeClass("selected");
$sItem.addClass("selected");
}
}
hoverCurrentItem();
$(window).scroll(function(e) {
hoverCurrentItem()
});
$('.nav-item').click(function () {
$('html, body').animate({
scrollTop: $('section-' + $(this).attr('data-hook')).offset().top - 0
}, "slow");
});