jQuery Isotope — sort by category, but specify which comes first

旧城冷巷雨未停 提交于 2019-12-06 08:18:42

问题


I'm looking for a type of sort behaviour, one that would match a specific category and bring this to the front of the listing. Like a filter would, except without removing other items.

Imagine these category items in an Isotope grid:

  • Red
  • Green
  • Blue
  • Red
  • Green
  • Blue

If I sort by category, it does so alphabetically to show:

  • Blue
  • Blue
  • Green
  • Green
  • Red
  • Red

What I'd like is to float a specific category to the front, e.g. "Green", like so:

  • Green
  • Green
  • Red
  • Red
  • Blue
  • Blue

Is this possible?

I've tried adding a getSortData function for each category like so:

      green : function( $elem ) {
        var $greenItems = $elem.filter('.green');            
        var $otherItems = $elem.filter(':not(.green)');
        return $greenItems.add($otherItems);
      }
      // repeat for blue, red, etc

But that didn't work.


回答1:


This worked for me:

green : function( $elem ) {
    var isGreen = $elem.hasClass('green');
    return (!isGreen?' ':'');
},
// etc

Demo: http://jsfiddle.net/Yvk9q/9/



来源:https://stackoverflow.com/questions/12551662/jquery-isotope-sort-by-category-but-specify-which-comes-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!