0

enter image description here

I have created a top bar with multiple categories of products. It shows the divs of the categories that are selected once you click the links at the top bar.The user can select more than one category. This is working correctly (the function is down below).

The problem is I want the window to scroll to the top of the div of the last category selected.

For example: If I select sideboards, dining tables and chairs in that order all three category divs must be showing but the window must scroll to the top of the div of the chairs category which was the last one selected. If there is only one category selected it must scroll to the top of the div of this one category selected.

Here is a fiddle with the code: http://jsfiddle.net/jtrhw59v/2/

// function to filter categories
$(function(){

var filters = $('.works-filter'); // find the filters
var works = $('.workItem'); // find the portfolio items
var showAll = $('.showAll'); // identify the "show all" button

var cFilter, cFilterData; // declare a variable to store the filter and one for the data to filter by
var filtersActive = []; // an array to store the active filters

$(".works-filter").on("click", function () { // if filters are clicked
    cFilter = $(this);
    cFilterData = cFilter.attr('data-filter'); // read filter value

    highlightFilter();
    applyFilter();
});

function highlightFilter () {
    var filterClass = 'works-filter-active';
    if (cFilter.hasClass(filterClass)) {
        cFilter.removeClass(filterClass);
        removeActiveFilter(cFilterData);
    } else if (cFilter.hasClass('showAll')) {
        $('.works-filter').removeClass(filterClass);
        filtersActive = []; // clear the array
        cFilter.addClass(filterClass);
    } else {
        $('.showAll').removeClass(filterClass);
        cFilter.addClass(filterClass);
        filtersActive.push(cFilterData);
    }
}

function applyFilter() {
    // go through all portfolio items and hide/show as necessary
    $('.workItem').each(function(){
        var i;
        var classes = $(this).attr('class').split(' ');
        if (cFilter.hasClass('showAll') || filtersActive.length == 0) { // makes sure we catch the array when its empty and revert to the default of showing all items
            $('.workItem').addClass('show-workItem'); //show them all
        } else {
            $(this).removeClass('show-workItem');
            for (i = 0; i < classes.length; i++) {
                if (filtersActive.indexOf(classes[i]) > -1) {
                    $(this).addClass('show-workItem');
                }
            }
        }
    });
}

// remove deselected filters from the ActiveFilter array
function removeActiveFilter(item) {
    var index = filtersActive.indexOf(item);
    if (index > -1) {
        filtersActive.splice(index, 1);
    }
}

});
mlila_p
  • 111
  • 14

1 Answers1

0

Sorry only just saw your reply, I think if you use hide instead of class remove/add the scroll should work for you, here is a basic example,

$(function() {

  let cFilter, cFilterData; // declare a variable to store the filter and one for the data to filter by
  let filtersActive = []; // an array to store the active filters

  $(".works-filter").each(function(index, item) {
    filtersActive.push($(item).attr('data-filter'))
  })

  $(".works-filter").on("click", function() { // if filters are clicked
    cFilter = $(this);
    cFilterData = cFilter.attr('data-filter'); // read filter value
    if (filtersActive.indexOf(cFilterData) === -1) {
      filtersActive.push(cFilterData)
      $('.' + cFilterData).show();
      $('html, body').animate({
        scrollTop: $('.' + cFilterData).offset().top - 50
      }, 2000);
    } else {
      filtersActive = filtersActive.slice(filtersActive.indexOf(cFilterData) + 1, filtersActive.length)
      $('.' + cFilterData).hide();
    }

    for (let i = 0; i < filtersActive.length; i++) {
      if ($('.' + filtersActive[i]).is(':hidden')) {
        $('.' + filtersActive[i]).show();
      }
    }


  });

});
.works-filter {
  display: inline-block;
  text-decoration: none;
  transition: all .35s ease;
}

.works-filter:hover {
  cursor: pointer;
  /* text-decoration: underline; */
  transition: all .35s ease;
}

.works-filter-active {
  /* text-decoration: underline; */
  font-weight: 600;
  text-decoration: underline;
  transition: all .35s ease;
}

.works-filter-active::before {
  font-weight: 600;
  text-decoration: underline;
  text-transform: none;
  transition: all .35s ease;
}

.workItem {
  display: none;
}

.show-workItem {
  display: block;
  transition: all .35s ease;
}

.filter-item {
  padding-right: 2rem;
}

.filter-item:hover {
  font-weight: 600;
}

.inline_nav_wrapper {
  position: relative;
  height: 50px;
  overflow-x: hidden;
  overflow-y: hidden;
}

.inline_nav {
  margin-top: 25px;
  height: 80px;
  box-sizing: border-box;
  white-space: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
}

.title {
  color: grey;
  font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h4 class="title">FILTER:</h4>

<div style="position: sticky;top: 0;z-index: 10000;background: #fff;color:blue;">
  <div class="row mx-0 inline_nav_wrapper header-shadow">
    <div class="col-12 px-0 inline_nav cata-sub-nav" id="filters" style="margin-top: 0.2rem;"> <span class="text-right works-filter flex-grow-1 w-sm-100 my-2 showAll filter-item" data-filter="all">ALL PRODUCTS</span>
      <span class="works-filter flex-grow-1 w-sm-100 my-2 filter-item nav-item-filter active-filter" data-filter="club-selection">THE CLUB SELECTION</span>
      <span class="works-filter flex-grow-1 w-sm-100 my-2 filter-item nav-item-filter active-filter" data-filter="chairs">CHAIRS</span>
      <span class="works-filter flex-grow-1 w-sm-100 my-2 filter-item nav-item-filter active-filter" data-filter="armchairs">ARMCHAIRS</span>
      <span class="works-filter flex-grow-1 w-sm-100 my-2 filter-item nav-item-filter active-filter" data-filter="sofas">SOFAS</span>
      <span class="works-filter flex-grow-1 w-sm-100 my-2 filter-item nav-item-filter active-filter" data-filter="benches">BENCHES & STOOLS</span>
      <span class="works-filter flex-grow-1 w-sm-100 my-2 filter-item nav-item-filter active-filter" data-filter="bar-chairs">BAR CHAIRS</span>
      <span class="works-filter flex-grow-1 w-sm-100 my-2 filter-item nav-item-filter active-filter" data-filter="center-tables">CENTER TABLES</span>
    </div>
  </div>
</div>


<h4 class="title">ALL CATEGORIES:</h4>

<div class="row mx-0">
  <div class="col-12">
    <div class="col-12">

      <div class="col-sm-12 col-md-6 my-3 
              show-workItem all">
        <p>ALL PRODUCTS</p>
        <img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80"
          class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
        <img
          src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
      </div>

      <div class="col-sm-12 col-md-6 my-3 workItem show-workItem all club-selection">
        <p>THE CLUB SELECTION</p>
        <img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80"
          class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
        <img
          src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
      </div>


      <div class="col-sm-12 col-md-6 my-3 workItem show-workItem all chairs">
        <p>CHAIRS</p>
        <img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80"
          class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
        <img
          src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
      </div>


      <div class="col-sm-12 col-md-6 my-3 workItem show-workItem all armchairs">
        <p>ARMCHAIRS</p>
        <img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80"
          class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
        <img
          src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
      </div>


      <div class="col-sm-12 col-md-6 my-3 workItem show-workItem all sofas">
        <p>SOFAS</p>
        <img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80"
          class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
        <img
          src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
      </div>


      <div class="col-sm-12 col-md-6 my-3 workItem show-workItem all benches">
        <p>BENCHES & STOOLS</p>
        <img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80"
          class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
        <img
          src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
      </div>


      <div class="col-sm-12 col-md-6 my-3 workItem show-workItem all bar-chairs">
        <p>BAR CHAIRS</p>
        <img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80"
          class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
        <img
          src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
      </div>

      <div class="col-sm-12 col-md-6 my-3 workItem show-workItem all center-tables">
        <p>CENTER TABLES</p>
        <img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80"
          class="img-fluid" style="max-width: 140px;"><img src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
        <img
          src="https://images.unsplash.com/photo-1595433707802-6b2626ef1c91?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80" class="img-fluid" style="max-width: 140px;">
      </div>
    </div>
  </div>
</div>

hopefully it isn't too late and this helps

Patrick Hume
  • 2,064
  • 1
  • 3
  • 11
  • thank you so much for your help! I had to drop the project for now, but will definitely try this later on. thank you again for helping me – mlila_p Aug 30 '22 at 13:10