2

このShopifyテーマにカルーセルがあり、5秒ごとに次のスライドに自動的にスクロールするように編集しようとしています(クリックして次のスライドに移動する必要はありません)。

これが私がカルーセルに使用しているものです:

//Carousel interaction
(function() {
    if (document.getElementById('carousel-nav')) {
        var slides = document.getElementById('carousel-images'),
            slidesItems = slides.getElementsByTagName('li'),
            nav = document.getElementById('carousel-nav'),
            navItems = nav.getElementsByTagName('li'),
            current = 0;

        function showSlide(i) {
            if (i != current && slidesItems[i]) {
                slide = slidesItems[i];
                slide.className += ' show';
                setTimeout (function() {
                    slide.className = slide.className.replace('show', 'appear');                    
                }, 1);
                setTimeout(function() {
                    slidesItems[current].className = slidesItems[current].className.replace('current', '');
                    slide.className = slide.className.replace('appear', 'current');
                    current = i;
                }, 300);
                navItems[i+1].className += ' current';
                navItems[current+1].className = navItems[current+1].className.replace('current', '');
                if (i == 0) {
                    if (navItems[0].className.indexOf('disabled') == -1) {
                        navItems[0].className += ' disabled';
                    }
                } else {
                    navItems[0].className = navItems[0].className.replace(' disabled', '');
                }
                var l = navItems.length - 1;
                if (i == slidesItems.length - 1) {
                    if (navItems[l].className.indexOf('disabled') == -1) {
                        navItems[l].className += ' disabled';
                    }
                } else {
                    navItems[l].className = navItems[l].className.replace(' disabled', '');
                }
            }
        }   

        nav.onclick = function(e) {
            e = e || window.event; e = e.target || e.srcElement;
            e = getParentByTagName(e, 'A');
            if (e) {
                var action = e.getAttribute('data-action');
                if (action == 'prev') {
                    showSlide(current - 1);
                } else if (action == 'next') {
                    showSlide(current + 1);
                } else {
                    showSlide(parseInt(action));
                }
            }
            return false;
        }
    }
})();

何か案は?私は少し困惑しています。

ありがとう!

4

4 に答える 4

3

考えが正しいので、最後の答えをフォローアップさせてください。あなたがしたいのは、自己永続的なループを作成することです。これを行う1つの方法は、ifステートメントの最後でスライド関数を再度呼び出すことです。これを行うには、2つのパラメーターを指定してsetInterval()を使用します。1つ目は関数(1を追加している間)で、2つ目はミリ秒単位(この場合は10に設定します)です。秒(10000ミリ秒)。

コードは次のようになります。

//Carousel interaction
(function() {
if (document.getElementById('carousel-nav')) {
    var slides = document.getElementById('carousel-images'),
        slidesItems = slides.getElementsByTagName('li'),
        nav = document.getElementById('carousel-nav'),
        navItems = nav.getElementsByTagName('li'),
        current = 0;

    function showSlide(i) {
        if (i != current && slidesItems[i]) {
            slide = slidesItems[i];
            slide.className += ' show';
            setTimeout (function() {
                slide.className = slide.className.replace('show', 'appear');                    
            }, 1);
            setTimeout(function() {
                slidesItems[current].className = slidesItems[current].className.replace('current', '');
                slide.className = slide.className.replace('appear', 'current');
                current = i;
            }, 300);
            navItems[i+1].className += ' current';
            navItems[current+1].className = navItems[current+1].className.replace('current', '');
            if (i == 0) {
                if (navItems[0].className.indexOf('disabled') == -1) {
                    navItems[0].className += ' disabled';
                }
            } else {
                navItems[0].className = navItems[0].className.replace(' disabled', '');
            }
            var l = navItems.length - 1;
            if (i == slidesItems.length - 1) {
                if (navItems[l].className.indexOf('disabled') == -1) {
                    navItems[l].className += ' disabled';
                }
            } else {
                navItems[l].className = navItems[l].className.replace(' disabled', '');
            }
        }
    }   

    nav.onclick = function(e) {
        e = e || window.event; e = e.target || e.srcElement;
        e = getParentByTagName(e, 'A');
        if (e) {
            var action = e.getAttribute('data-action');
            if (action == 'prev') {
                showSlide(current - 1);
            } else if (action == 'next') {
                showSlide(current + 1);
            } else {
                showSlide(parseInt(action));
            }
        }
        return false;
    }

    setInterval(current+1, 10000);
}
})();
于 2012-10-23T17:25:42.750 に答える
1

これは、のshowSlide()ようなものを使用して、5秒ごとにメソッドを起動するだけの問題ですsetTimeout()

次のスライドに進むには、を起動しますshowSlide(current + 1)

于 2012-10-23T04:42:20.340 に答える
1

構文は少しずれています。これはそれを行います:

setInterval(function() { showSlide(current + 1) }, 5000);

また、コードのさらに下に配置する必要があるため、nav.onclickブロックの外にありますが、元の関数には残ります。以下の例を参照してください。

nav.onclick = function(e) {
            e = e || window.event; e = e.target || e.srcElement;
            e = getParentByTagName(e, 'A');
            if (e) {
                var action = e.getAttribute('data-action');
                if (action == 'prev') {
                    showSlide(current - 1);
                } else if (action == 'next') {
                    showSlide(current + 1);
                } else {
                    showSlide(parseInt(action));
                }
            }
            return false;
        }

    }
        setInterval(function() { showSlide(current + 1) }, 5000);
    })();
于 2012-11-06T05:47:54.967 に答える
0

ボタンがクリックされた後にスクロールを停止したい場合は、必ずそのsetIntervalを変数に追加し、clearIntervalを使用してクリック時に停止してください。以下のコードを参照してください。

var rotation = setInterval(function() { showSlide(current + 1) }, 7000);

        nav.onclick = function(e) {
            e = e || window.event; e = e.target || e.srcElement;
            e = getParentByTagName(e, 'A');
            if (e) {
                var action = e.getAttribute('data-action');
                if (action == 'prev') {
                    showSlide(current - 1);
                } else if (action == 'next') {
                    showSlide(current + 1);
                } else {
                    showSlide(parseInt(action));
                }
            }
            clearInterval(rotation);
            return false;
        }
于 2013-11-20T23:59:14.440 に答える