I've been attempting to replicate a filter similar to the one showcased here.
In an HTML file, I wrote some code that perfectly mimics the appearance. However, the JavaScript functionality is not working as expected. When I click on a different option from the default one, nothing happens.
$(document).ready(function() {
$("#filter-bar li").click(function() {
$("#filter-bar li").removeClass("active");
$(this).addClass("active");
$("#filter-bar").removeClass().addClass($(this).attr("data-target"));
});
})
body {
background-color: #eee;
margin: 0;
padding: 0;
font-family: Tahoma;
}
h2 {
text-align: center;
}
#filter-bar {
width: 100%;
margin: 0;
padding: 0;
height: 36px;
display: inline-flex;
}
#wrapper-filter {
background-color: #fff;
width: 570px;
height: auto;
margin: 30px auto;
border-radius: 30px;
box-sizing: border-box;
}
#filter-bar li {
width: 190px;
background-color: transparent;
text-align: center;
list-style-type: none;
z-index: 10;
cursor: pointer;
font-family: Open Sans, sans-serif;
font-weight: 100;
font-size: 15px;
line-height: 36px;
}
.pill {
position: absolute;
width: 190px;
height: 38px;
background-color: #39c;
border-radius: 30px;
color: #444;
z-index: 10;
border: 5px solid #eee;
box-sizing: border-box;
}
.filter-option {
transition: color 500ms;
}
#filter-bar.option-1 .pill {
margin-left: 0px;
transition: margin-left 200ms ease;
}
#filter-bar.option-2 .pill {
margin-left: 187px;
transition: margin-left 200ms ease;
}
#filter-bar.option-3 .pill {
margin-left: 380px;
transition: margin-left 200ms ease;
}
.option-1.active,
.option-2.active,
.option-3.active {
color: #fff;
transition: color 200ms;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>Animated filter selector</h2>
<div id="wrapper-filter">
<ul id="filter-bar">
<span class="pill"></span>
<li class="filter-option option-1 active" data-target="option-1">Books</li>
<li class="filter-option option-2" data-target="option-2">Shoes</li>
<li class="filter-option option-3" data-target="option-3">Toys</li>
</ul>
</div>
It seems like the JavaScript script isn't being recognized at all, resulting in no action when it should be changing elements on the page. Perhaps it wasn't placed correctly? As a frontend novice, I'm unsure of what exactly went wrong. Can anyone offer assistance?