My website has a sticky sidebar with a list of cars and their corresponding categories in a table:
<ul class = "cars">
<li class=""><a href="javascript:void(0)" class="model" data-id="1"> BMW </a></li>
......
<li class=""><a href="javascript:void(0)" class="model" data-id="2"> Mercedes </a></li>
</ul>
The categories are represented as <div>
elements like this:
<div class="element-title" id="car-category-1">BMW</div>
.....
<div class="element-title" id="car-category-2">Mercedes</div>
My goal is to change the active class of the car in the list when scrolling through its corresponding category. Here's the jQuery code I'm using for this functionality:
$(document).on('click', '.model', function () {
var this_id = $(this).data('id');
var gotom = setInterval(function () {
cars_go_to_navtab(this_id);
clearInterval(gotom);
}, 400);
$('.cars li').removeClass('active');
$(this).parent().addClass('active');
});
function cars_go_to_navtab(id) {
var scrolling_div = $('#car-category-' + id);
$('html, body').animate({
scrollTop: scrolling_div.offset().top - 70
}, 500);
}