JSFiddle - React, Tailwind, and code Playground

by lasha

HTML

<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

<div class="container">   
    <div class="one">content 1</div>
    <div class="two">content 2</div>
    <div class="three">content 3</div>
</div>

CSS

body { background: #CCC; }

.one, .two, .three {
    height: 500px;
    border: 1px solid #000;
    margin-top: 50px;
}

.one { background: #F00; }
.two { background: #0F0; }
.three { background: #00F; }

ul {
    position: fixed;
    margin: 0 0 10px;
    padding: 3px 5px;
    top: 0;
    left: 0;
    background: #FFF;
    width: 100%;
}

ul li {
    display: inline-block;
    margin-right: 20px;
}

JavaScript

$(function(){
    // $(window).scrollTop(300);
    $("ul li").on("click", function(e){
        var $this = $(this);
        var index = $this.index();
    
        var topOffset = $(".container")
                           .find("div")
                           .eq(index)
                           .offset().top;
        
        $("html,body").animate({
            scrollTop: topOffset - 30
        }, 300);
    });
});