jQuery Isotope hash-history:美化hash-URL

时间:2011-09-26 13:38:45

标签: jquery jquery-isotope

我正在使用Isotope和哈希历史记录。它工作得很好,但我对网址不满意 - 我想简化&清理它。

目前正在使用:

var href = $this.attr('href').replace( /^#/, '' );
var option = $.deparam( href, true );`

在此标记上:

<li><a href="#filter=.work/">Work</a></li>

所以,我的问题是:如何使用以下标记使其工作?

<li><a href="#/work/">Work</a></li>

使用过滤器,所有其他Isotope选项都被锁定,所以我希望删除对过滤器的引用。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我正在使用它:

function getHashFilter() {
  var hash = location.hash;
  // get filter=filterName
  var matches = location.hash.match(/^#?(.*)$/)[1]; // this is to get the name of the class
  var hashFilter = matches;
  return hashFilter;

}

$( function() {
  var $container = $('. isotope');

  // bind filter button click
  var $filters = $('#filters').on( 'click', 'span', function() {
    var filterAttr = $( this ).attr('data-filter');
    // set filter in hash
    location.hash = '/' + filterAttr.substr(1); // adding the slash for the url
  });

  var isIsotopeInit = false;

  function onHashchange() {
    var hashFilter = getHashFilter();
    if ( !hashFilter && isIsotopeInit ) {
      return;
    }
    isIsotopeInit = true;
    // filter isotope
    $container.isotope({
      itemSelector: '.element-item',
      filter: '.' + hashFilter.substr(1) //adding the . so it can match the data-filter
    });
    // set selected class on button
    if ( hashFilter ) {
      $filters.find('.is-checked').removeClass('is-checked');
      $filters.find('[data-filter=".' + hashFilter + '"]').addClass('is-checked');
    }
  }

  $(window).on( 'hashchange', onHashchange );
  // trigger event handler to init Isotope
  onHashchange();
});

我意识到*在此之后将不起作用。因此,您需要添加另一个类/数据过滤器来显示所有内容。