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: 300px;
background: pink;
}
.block:nth-child(even) {
background-color: orange;
}
.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;
}
.nav-item:hover, .nav-item.selected {
background-color: blue;
}
JavaScript
$(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 = $('.nav-wrap').height() / 2,
newhei = divwid - navhei;
$('.nav-wrap').css({
'top': newhei
});
}
function hoverCurrentItem() {
var h = $(".block:first").height();
var sIndex = Math.floor($(window).scrollTop() / h);
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: $('#block-' + $(this).attr('data-hook')).offset().top - 0
}, "slow");
});