0

我正在尝试使用这个可过滤的投资组合,但对于我的需要,过渡不够顺畅。

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/

4

1 回答 1

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/

希望这对你有用。

于 2013-08-25T08:25:10.473 回答