我编写了一个小函数,该函数基本上将currentPosition值用作参数,如果该数字等于幻灯片的总数,并且如果该数字不等于1,则很遗憾,尽管我的递增数字似乎从未从1移出。谁能告诉我我哪里可能出问题了?

var currentPosition = 0;
            var slideWidth = 320;
            var numberOfSlides = sliderSlides.children.length;
            console.log('Number of slides = ', numberOfSlides);

            //Remove scrollbar in js
            sliderCtn.style.overflow = 'hidden';

            //Set .slides width equal to total width of all slides
            sliderSlides.style.width = slideWidth * numberOfSlides + 'px';

            // Direction click handlers
            sliderDirNext[det.clickEvent] = function(e) {
                next(currentPosition);
                sliderSlides.style.left = slideWidth*(-currentPosition) + 'px';
                console.log('Slide width ', slideWidth);
                stopEvent(e);
            };

 function next(currentPosition){
                    if( currentPosition === numberOfSlides ) {
                        return false;
                    } else {
                        currentPosition += 1;
                        console.log('Next current position = ', currentPosition);
                        return true;
                    }
                }

最佳答案

解。但是,如果您提供更多的上下文,则很有可能会更好:

window.currentPosition = 0;

function next(){
    if( currentPosition === numberOfSlides ) {
        return false;
    } else {
        currentPosition += 1;
        console.log('Next current position = ', currentPosition);
        return true;
    }
}




OP更新后:

鉴于其余逻辑都是合理的,只需删除next函数currentPosition参数即可解决您的问题。

关于javascript - currentPosition似乎从未离开过1?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9570083/

10-10 06:29