123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container text-light">
  <div class="row mt-5">
    <div class="col text-center">
      <h1 class="mb-5">jQuery Multiselect Filter Demo</h1>
      <p>An overlay shows each picture's details.</p>
      <p>Select filter to apply, click again to remove. Click more than one filter to apply multiple filters.</p>
      <p>"All Projects" resets the filter and shows all items.</p>
      <p>All details: <a href="https://ttntm.me/blog/jquery-multiselect-filter/" class="text-warning" target="_blank">ttntm.me</a></p>
      <hr class="my-5 shadow-sm">
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div id="filters" class="d-flex flex-row flex-wrap justify-content-between mb-5">
        <span class="works-filter flex-grow-1 w-sm-100 my-2" data-filter="catA">Category A</span>
        <span class="works-filter flex-grow-1 w-sm-100 my-2" data-filter="catB">Category B</span>
        <span class="works-filter flex-grow-1 w-sm-100 my-2" data-filter="catC">Category C</span>
        <span class="text-right works-filter flex-grow-1 w-sm-100 my-2 showAll" data-filter="all">All Projects</span>
      </div>
    </div>
  </div>
  <div class="row mb-5">
    <div class="col-sm-12 col-md-6 my-3 workItem show-workItem catA">
      <div class="img-container m-0 p-0 shadow-sm">
        <img src="https://picsum.photos/600/400?image=1081" class="img-center img-fluid img-showcase">
        <div class="overlay d-flex flex-column align-items-center justify-content-center">
          <h2><a class="link-overlay" href="#" title="Pepsi Case">Pepsi Case</a></h2>
          <p class="d-block small">Category A</p>
        </div>
      </div>
    </div>
    <div class="col-sm-12 col-md-6 my-3 workItem show-workItem catB">
      <div class="img-container m-0 p-0 shadow-sm">
        <img src="https://picsum.photos/600/400?image=1073" class="img-center img-fluid img-showcase">
        <div class="overlay d-flex flex-column align-items-center justify-content-center">
          <h2><a class="link-overlay" href="#" title="Contoso Case">Contoso Case</a></h2>
          <p class="d-block small">Category B</p>
        </div>
      </div>
    </div>
    <div class="col-sm-12 col-md-6 my-3 workItem show-workItem catC">
      <div class="img-container m-0 p-0 shadow-sm">
        <img src="https://picsum.photos/600/400?image=1078" class="img-center img-fluid img-showcase">
        <div class="overlay d-flex flex-column align-items-center justify-content-center">
          <h2><a class="link-overlay" href="#" title="Cola Case">Cola Case</a></h2>
          <p class="d-block small">Category C</p>
        </div>
      </div>
    </div>
    <div class="col-sm-12 col-md-6 my-3 workItem show-workItem catA catC">
      <div class="img-container m-0 p-0 shadow-sm">
        <img src="https://picsum.photos/600/400?image=1076" class="img-center img-fluid img-showcase">
        <div class="overlay d-flex flex-column align-items-center justify-content-center">
          <h2><a class="link-overlay" href="#" title="Example Case">Example Case</a></h2>
          <p class="d-block small">Category A, Category C</p>
        </div>
      </div>
    </div>
  </div>
  <div class="row mb-1">
    <div class="col text-center">
      <p>Pictures borrowed from https://picsum.photos</p>
    </div>
  </div>
</div>
            
          
!
            
              body {  
  background-image: linear-gradient(to right, #243949 0%, #517fa4 100%);
}
.img-center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.img-showcase {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.img-container {
    display: block;
    position: relative;
}

.link-overlay {
  color: #fff;
  text-decoration: none;
}

.link-overlay:hover {
  color: #000;
  background-color: #fff;
  transition: all 0.75s ease-in-out;
  text-decoration: none;
}

.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;
  transition: all .35s ease;
}

.works-filter-active::before {
  content: "X ";
  text-transform: none;
  transition: all .35s ease;
}

.workItem {
  display: none;
}

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

.overlay {
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  opacity: 0;
  color: #fff;
  transition: all 0.5s ease-in-out;
}

.overlay:hover {
  opacity: 1;
}
            
          
!
            
              $(document).ready(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

    $filters.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')) {
            $filters.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
        $works.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
                $works.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);
        }
    }

});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console