Goran Tesic
Goran Tesic

Reputation: 739

Multiple sliders sliding at the same time when only the active one should be sliding

I have multiple sliders, each in its own section->article. My problem is this: When I hit the next or previous button on one of the sliders all sliders start sliding at the same time.

html:

<section id="section01">
        <article class="article01">
            <div class="wrapper">
                <ul class="slider">
                    <li class="slide"></li>
                    <li class="slide"></li>
                    <li class="slide"></li>
                </ul>
            </div>
            <img class="previous" src="images/arrow-transparent.png">
            <img class="next" src="images/arrow-transparent.png">
        </article>
</section>

css

.wrapper{
    height: 342px;
    margin: 0 auto;
    overflow: hidden;
    width: 918px;
}

.slider{
    height: 342px;
    position: relative;
    width: 5000px;
}

.slide{
    float: left;
    height: 342px;
    position: relative;
    width: 306px;
}

js

/* slider */
$(function(){
    var i = 0;
    $('.previous').click(function(){
        var currentSlides = $(this).parent().find('.slide'),
        parent = $(this).parent().find('.wrapper ul li');
        i = --i -2 % currentSlides.length;
        if(i - 3 >= -3) {
            $('.slider').animate({'left' : -(parent.eq(i).position().left)}, 600);
        } else {
            i = 0;
        }
    });

    $('.next').click(function(){
        var currentSlides = $(this).parent().find('.slide'),
        parent = $(this).parent().find('.wrapper ul li'),
        current = i;
        i = ++i + 2 % currentSlides.length;
        if(i + 3 < currentSlides.length) {
            $('.slider').animate({'left' : -(parent.eq(i).position().left)}, 600);
        } else {
            i = current;
        }
    }); 
});

How do I fix this?

Upvotes: 0

Views: 298

Answers (2)

Goran Tesic
Goran Tesic

Reputation: 739

I decided to completely ditch the javascript code from my original post and ended up using this instead:

    $(function() {
    $('article .wrapper').each(function(){
        var slider = $(this).find('.slider'),
            parent = $(this),
            step =855,
            left = parseInt(slider.css('left'), 10),
            max = parent.width() - slider.width(),
            min = 0;

        parent.find(".previous").click(function() {
            if (left < 0) {
                var newLeft = left + step;
                left = (newLeft<min) ? newLeft : min;
                slider.animate({
                    "left": left + 'px'
                }, "slow");
            }
        });

        parent.find(".next").click(function() {
            if (left > max) {
                var newLeft = left - step;
                left = (newLeft>max) ? newLeft : max;
                slider.animate({
                    "left": left + 'px'
                }, "slow");
            }
        });
    });
});

I also slightly modified widths of several elements in css and now it all works like a charm.

Upvotes: 0

Dutts
Dutts

Reputation: 6191

This is because you are selecting all slider class elements so this will animate them all.

What if you select slide instead?

$(function(){
    var i = 0;
    $('.previous').click(function(){
        var currentSlides = $(this).parent().find('.slide'),
        parent = $(this).parent().find('.wrapper ul li');
        i = --i -2 % currentSlides.length;
        if(i - 3 >= -3) {
            $('.slide').animate({'left' : -(parent.eq(i).position().left)}, 600);
        } else {
            i = 0;
        }
    });

Upvotes: 1

Related Questions