如何在不丢失功能的情况下将按钮转换为下拉列表?

时间:2018-02-16 12:31:03

标签: javascript isotope

我有一个来自同位素(组合过滤器)的代码和特定的加载类别。这一切都基于按钮,但我需要有列表(选择,选项)。如何在没有丢失任何功能的情况下使用列表,因为它几乎是完美的,除了按钮。让它像这样工作对我来说非常重要,尤其是从开始并显示它们的选定类别。是否有可能不受损失地改变?

对我来说还有一个问题:如果您加载网站,则会看到所有项目的毫秒数。有没有办法删除它?

以下是完整的代码:

// external js: isotope.pkgd.js

// store filter for each group
var filters = {};

// build initial combination filter
var $filters = $('.filters');
$filters.find('.button.is-checked').each( function( i, button ) {
  setGroupFilter( button );
});

function setGroupFilter( button ) {
  var $button = $( button );
  // get group key
  var $buttonGroup = $button.parents('.button-group');
  var filterGroup = $buttonGroup.attr('data-filter-group');
  // set filter for group
  filters[ filterGroup ] = $button.attr('data-filter');
}

// get initial comboFilter
var comboFilter = getComboFilter()

// init Isotope
var $grid = $('.grid').isotope({
  itemSelector: '.color-shape',
  filter: comboFilter,
});


$filters.on( 'click', '.button', function( event ) {
  setGroupFilter( event.currentTarget );
  // combine filters
  comboFilter = getComboFilter();
  // set filter for Isotope
  $grid.isotope({ filter: comboFilter });
});

// change is-checked class on buttons
$('.button-group').each( function( i, buttonGroup ) {
  var $buttonGroup = $( buttonGroup );
  $buttonGroup.on( 'click', 'button', function() {
    $buttonGroup.find('.is-checked').removeClass('is-checked');
    $( this ).addClass('is-checked');
  });
});
  
// flatten object by concatting values
function getComboFilter() {
  var comboFilter = '';
  for ( var prop in filters ) {
    comboFilter += filters[ prop ];
  }
  return comboFilter;
}
.button {
  display: inline-block;
  padding: 0.5em 1.0em;
  background: #EEE;
  border: none;
  border-radius: 7px;
  background-image: linear-gradient( to bottom, hsla(0, 0%, 0%, 0), hsla(0, 0%, 0%, 0.2) );
  color: #222;
  font-family: sans-serif;
  font-size: 16px;
  text-shadow: 0 1px white;
  cursor: pointer;
}

.button:hover {
  background-color: #8CF;
  text-shadow: 0 1px hsla(0, 0%, 100%, 0.5);
  color: #222;
}

.button:active,
.button.is-checked {
  background-color: #28F;
}

.button.is-checked {
  color: white;
  text-shadow: 0 -1px hsla(0, 0%, 0%, 0.8);
}

.button:active {
  box-shadow: inset 0 1px 10px hsla(0, 0%, 0%, 0.8);
}

/* ---- button-group ---- */

.button-group:after {
  content: '';
  display: block;
  clear: both;
}

.button-group .button {
  float: left;
  border-radius: 0;
  margin-left: 0;
  margin-right: 1px;
}

.button-group .button:first-child { border-radius: 0.5em 0 0 0.5em; }
.button-group .button:last-child { border-radius: 0 0.5em 0.5em 0; }

/* ---- isotope ---- */

.grid {
  background: #EEE;
  max-width: 1200px;
}

/* clear fix */
.grid:after {
  content: '';
  display: block;
  clear: both;
}

/* ui group */

.ui-group {
  display: inline-block;
}

.ui-group h3 {
  display: inline-block;
  vertical-align: top;
  line-height: 32px;
  margin-right: 0.2em;
  font-size: 16px;
}

.ui-group .button-group {
  display: inline-block;
  margin-right: 20px;
}

/* color-shape */

.color-shape {
  width: 70px;
  height: 70px;
  margin: 5px;
  float: left;
}
 
.color-shape.round {
  border-radius: 35px;
}
 
.color-shape.big.round {
  border-radius: 75px;
}
 
.color-shape.red { background: red; }
.color-shape.blue { background: blue; }
.color-shape.yellow { background: yellow; }
 
.color-shape.wide, .color-shape.big { width: 150px; }
.color-shape.tall, .color-shape.big { height: 150px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>

<div class="filters">

  <div class="ui-group">
    <div class="button-group js-radio-button-group" data-filter-group="color">
      <button class="button is-checked" data-filter=".red">red</button>
      <button class="button" data-filter=".blue">blue</button>
      <button class="button" data-filter=".yellow">yellow</button>
    </div>
  </div>

  <div class="ui-group">
    <div class="button-group js-radio-button-group" data-filter-group="size">
      <button class="button is-checked" data-filter=".small">small</button>
      <button class="button" data-filter=".wide">wide</button>
      <button class="button" data-filter=".big">big</button>
      <button class="button" data-filter=".tall">tall</button>
    </div>
  </div>

</div>

<div class="grid">
  <div class="color-shape small round red"></div>
  <div class="color-shape small round blue"></div>
  <div class="color-shape small round yellow"></div>
  <div class="color-shape small square red"></div>
  <div class="color-shape small square blue"></div>
  <div class="color-shape small square yellow"></div>
  <div class="color-shape wide round red"></div>
  <div class="color-shape wide round blue"></div>
  <div class="color-shape wide round yellow"></div>
  <div class="color-shape wide square red"></div>
  <div class="color-shape wide square blue"></div>
  <div class="color-shape wide square yellow"></div>
  <div class="color-shape big round red"></div>
  <div class="color-shape big round blue"></div>
  <div class="color-shape big round yellow"></div>
  <div class="color-shape big square red"></div>
  <div class="color-shape big square blue"></div>
  <div class="color-shape big square yellow"></div>
  <div class="color-shape tall round red"></div>
  <div class="color-shape tall round blue"></div>
  <div class="color-shape tall round yellow"></div>
  <div class="color-shape tall square red"></div>
  <div class="color-shape tall square blue"></div>
  <div class="color-shape tall square yellow"></div>
</div>

0 个答案:

没有答案