index.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <link href="../reset.css" rel="stylesheet"/>
  6. <link href="./style.css" rel="stylesheet"/>
  7. <title>MixItUp Demo - Checkboxes with Reset Checkbox</title>
  8. </head>
  9. <body>
  10. <div class="controls">
  11. <div class="checkbox-group">
  12. <div class="checkbox">
  13. <label class="checkbox-label">All</label>
  14. <input type="checkbox" value="all" checked/>
  15. </div>
  16. <div class="checkbox">
  17. <label class="checkbox-label">Green</label>
  18. <input type="checkbox" value=".green"/>
  19. </div>
  20. <div class="checkbox">
  21. <label class="checkbox-label">Blue</label>
  22. <input type="checkbox" value=".blue"/>
  23. </div>
  24. <div class="checkbox">
  25. <label class="checkbox-label">Pink</label>
  26. <input type="checkbox" value=".pink"/>
  27. </div>
  28. </div>
  29. <button type="button" class="control" data-sort="default:asc">Asc</button>
  30. <button type="button" class="control" data-sort="default:desc">Desc</button>
  31. </div>
  32. <div class="container">
  33. <div class="mix green"></div>
  34. <div class="mix green"></div>
  35. <div class="mix blue"></div>
  36. <div class="mix pink"></div>
  37. <div class="mix green"></div>
  38. <div class="mix blue"></div>
  39. <div class="mix pink"></div>
  40. <div class="mix blue"></div>
  41. <div class="gap"></div>
  42. <div class="gap"></div>
  43. <div class="gap"></div>
  44. </div>
  45. <script src="../mixitup.min.js"></script>
  46. <script>
  47. // In this example, we must bind a 'change' event handler to
  48. // our checkboxes, then interact with the mixer via
  49. // its .filter() API methods.
  50. var containerEl = document.querySelector('.container');
  51. var checkboxGroup = document.querySelector('.checkbox-group');
  52. var checkboxes = checkboxGroup.querySelectorAll('input[type="checkbox"]');
  53. var allCheckbox = checkboxGroup.querySelector('input[value="all"]');
  54. var mixer = mixitup(containerEl);
  55. checkboxGroup.addEventListener('change', function(e) {
  56. var selectors = [];
  57. var checkbox;
  58. var i;
  59. if (e.target === allCheckbox && e.target.checked) {
  60. // If the "all" checkbox was checked, uncheck all other checkboxes
  61. for (i = 0; i < checkboxes.length; i++) {
  62. checkbox = checkboxes[i];
  63. if (checkbox !== allCheckbox) checkbox.checked = false;
  64. }
  65. } else {
  66. // Another checkbox was changed, uncheck "all".
  67. allCheckbox.checked = false;
  68. }
  69. // Iterate through all checkboxes, pushing the
  70. // values of those that are checked into an array
  71. for (i = 0; i < checkboxes.length; i++) {
  72. checkbox = checkboxes[i];
  73. if (checkbox.checked) selectors.push(checkbox.value);
  74. }
  75. // If there are values in the array, join it into a string
  76. // using your desired logic, and send to the mixer's .filter()
  77. // method, otherwise filter by 'all'
  78. var selectorString = selectors.length > 0 ?
  79. selectors.join(',') : // or '.' for AND logic
  80. 'all';
  81. mixer.filter(selectorString);
  82. });
  83. </script>
  84. </body>
  85. </html>