顺利过滤的投资组合

时间:2013-08-25 04:07:03

标签: jquery css3

我正在尝试使用这个可过滤的投资组合,但是根据我的需要,转换不够顺畅。

JS

var Portfolio = {
sort: function(items) {
    items.show();
    $('#portfolio-content').find('div.portfolio-item').not(items).fadeOut(500);
},
showAll: function(items) {
    items.fadeIn(500);
},
doSort: function() {
    $('a', '#portfolio-sort').on('click', function() {

        var $a = $(this);
        if (!$a.is('#all')) {

            var items = $('div[data-cat=' + $a.data('cat') + ']', '#portfolio-content');

            Portfolio.sort(items);

        } else {

            Portfolio.showAll($('div.portfolio-item', '#portfolio-content'));


        }

    });
}
};

Portfolio.doSort();

HTML

<div id="portfolio">
<div id="portfolio-sort">
    <a href="#" id="all">ALL</a>
    <a href="#" data-cat="a">A</a>
    <a href="#" data-cat="b">B</a>
    <a href="#" data-cat="c">C</a>
</div>
<div id="portfolio-content">
    <div class="portfolio-item" data-cat="a">A</div>
    <div class="portfolio-item" data-cat="b">B</div>
    <div class="portfolio-item" data-cat="c">C</div>
    <div class="portfolio-item" data-cat="c">C</div>
    <div class="portfolio-item" data-cat="b">B</div>
    <div class="portfolio-item" data-cat="a">A</div>
    <div class="portfolio-item" data-cat="a">A</div>
    <div class="portfolio-item" data-cat="c">C</div>
    <div class="portfolio-item" data-cat="b">B</div>
</div>

CSS

#portfolio {
    width: 100%;
    max-width: 600px;
    margin: 2em auto;
}

#portfolio-sort {
    text-align: center;
    padding-bottom: 3px;
    border-bottom: 1px solid #999;
    margin-bottom: 1em;
}

#portfolio-sort a {
    color: #fff;
    background: #a00;
    display: inline-block;
    padding: 3px 9px;
    text-decoration: none;
    margin-right: 1em;
}

#portfolio-content {
    overflow: hidden;
}

div.portfolio-item {
    float: left;
    margin: 0 3% 1em 0;
    width: 29%;
    height: 10em;
    line-height: 10;
    text-align: center;
}

div[data-cat="a"] {
    background: #ccc;
}

div[data-cat="b"] {
    background: #666;
    color: #fff;
}

div[data-cat="c"] {
    background: #333;
    color: #fff;
}

有没有办法使用CSS3或jQuery使过滤更流畅?

以下是完整的代码:http://jsfiddle.net/gabrieleromanato/KkxyS/

1 个答案:

答案 0 :(得分:0)

我对你的JS进行了一些更改:

var Portfolio = {
    sort: function (items) {
        Portfolio.hideAll($('#portfolio-content *'));
        Portfolio.showAll(items);
    },
    showAll: function (items) {
        items.fadeIn(700);
    },
    hideAll: function (items) {
        items.hide();
    },
    doSort: function () {
        $('a', '#portfolio-sort').on('click', function () {
            var $a = $(this);
            if (!$a.is('#all')) {
                var items = $('div[data-cat=' + $a.data('cat') + ']', '#portfolio-content');
                Portfolio.sort(items);
            } else {
                var items = $('#portfolio-content *');
                Portfolio.hideAll(items);
                Portfolio.showAll(items);
            }
        });
    }
};

Portfolio.doSort();

而且,这里是jsfiddle:http://jsfiddle.net/KkxyS/56/

希望,它适合你。