0

ドキュメント上の複数のスライダー要素に影響を与えるスライダー関数を作成しようとしています。setInverval 値でスタックします。

ここでは jsFiddle をテストしています。

1つだけで完璧に機能します。

function repSlider(target) {
        target = $(target);
        targetEl = target.find('.drop_leds');
        targetEl.hide().first().addClass('active').show();
        totalEl = targetEl.length-1;

        setInterval(function() {
            curEl = target.find('.active.drop_leds').index();
            curEl == totalEl ? curEl = 0 : curEl += 1;
            console.log(curEl);//this gives bug
            targetEl.hide().removeClass('active')
            .eq(curEl).addClass('active').fadeIn(444);
        },3000);

}

repSlider('.targetClassA');
repSlider('.targetClassB');

別の方法でも試しましたが、結果は同じです。

$.fn.repSlider = function() {
        target = $(this);
        targetEl = target.find('.sliderEl');
        targetEl.hide().first().addClass('active').show();
        totalEl = targetEl.length-1;

        return window.setInterval(function() {
            curEl = target.find('.active.sliderEl').index();
            curEl == totalEl ? curEl = 0 : curEl += 1;
            console.log(curEl);//this gives bug
            targetEl.hide().removeClass('active')
            .eq(curEl).addClass('active').fadeIn(444);

    },3000);
}

$('.repSliderA').repSlider();
$('.repSliderB').repSlider();
4

1 に答える 1

0

関数からsetInvervalを取得することで解決しました:

ここではjsFiddleが動作しています。

$('.targetClassA > div').hide().first().addClass('active').show();
$('.targetClassB > div').hide().first().addClass('active').show();
function repSlider(target) {
    target = $(target);
    targetEl = target.find('.drop_leds');
    totalEl = targetEl.length-1;

    curEl = target.find('.active.drop_leds').index();
    curEl == totalEl ? curEl = 0 : curEl += 1;
    console.log(curEl);//this gives bug
    targetEl.hide().removeClass('active')
    .eq(curEl).addClass('active').fadeIn(444);

}

var repSliderAInt = setInterval(function() {
    repSlider('.targetClassA');
},3000);
var repSliderBInt = setInterval(function() {
    repSlider('.targetClassB');
},3000);
于 2013-08-15T10:13:19.047 に答える