Preston
Preston

Reputation: 2195

Page moves to top of screen when sorting items

I'm creating an sortable portfolio with jQuery.

The problem is; every time I click on an filter of my portfolio items, then the page scrolls to the top.

Is there an way to prevent that?

Can see the problem here: http://alsite.com.br/flex/produtos.html

my script:

<script type="text/javascript">
    $(document).ready(function(){
        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();     

    });

</script>

my portfolio code:

<div id="portfolio">
    <div id="portfolio-sort">
        <a href="#" id="all">TODOS</a>
        <a href="#" data-cat="a">ACM</a>
        <a href="#" data-cat="b">MDF</a>
        <a href="#" data-cat="c">AÇO INOX</a>
        <a href="#" data-cat="b">PVC ESPANDIDO</a>
        <a href="#" data-cat="c">AÇO GALVANIZADO</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>

</div>

Upvotes: 0

Views: 74

Answers (2)

Sven van Zoelen
Sven van Zoelen

Reputation: 7229

You need to prevent the link from being an link :)

$('a', '#portfolio-sort').on('click', function(e) {
    // stop being an link!
    e.preventDefault();

    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'));
    }
});

More info: click

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128856

Simply change:

$('a', '#portfolio-sort').on('click', function() { ...

To:

$('a', '#portfolio-sort').on('click', function(e) { ...

And add:

e.preventDefault();

--

$('a', '#portfolio-sort').on('click', function() {
    e.preventDefault();

    ...
});

--

This prevents the link from functioning as a link (and taking you to produtos.html#).

Upvotes: 1

Related Questions