0

jsFiddle

使用下面的代码,我有一个 jQuery 内容幻灯片工作,但是当我在开始或最后一张幻灯片时,下一个和上一个箭头消失,我希望幻灯片继续回到开头或最后一张,具体取决于当前幻灯片你明白我的意思。

jQuery

var currentPosition = 0;
var slideWidth = 560;
var slides = $('.slide');
var numberOfSlides = slides.length;
// Remove scrollbar in JS
$('#slidesContainer').css('overflow', 'hidden');

// Wrap all .slides with #slideInner div
slides.wrapAll('<div id="slideInner"></div>')
// Float left to display horizontally, readjust .slides width
.css({
    'float' : 'left',
    'width' : slideWidth
});

// Set #slideInner width equal to total width of all slides
$('#slideInner').css('width', slideWidth * numberOfSlides);

// Insert controls in the DOM
$('#slideshow')
    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    .append('<span class="control" id="rightControl">Clicking moves right</span>');

// Hide left arrow control on first load
manageControls(currentPosition);

    // Create event listeners for .controls clicks
$('.control')
        .bind('click', function(){
    // Determine new position
    currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

    // Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
    $('#slideInner').animate({
        'marginLeft' : slideWidth*(-currentPosition)
    });
});

// manageControls: Hides and Shows controls depending on currentPosition
function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
    // Hide right arrow if position is last slide
        if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
}
4

1 回答 1

1

您必须删除对您的函数的调用,manageControls而不是该行

currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

使用代码

if(($(this).attr('id')=='rightControl')) {
    if(currentPosition == numberOfSlides-1)
        currentPosition = 0;
    else
        currentPosition++;
} else if($(this).attr('id')=='leftControl'){
    if(currentPosition == 0)
        currentPosition = numberOfSlides-1;
    else
        currentPosition--;
}

请参阅http://jsfiddle.net/WTvsM/1/以获取小提琴的更新版本。

于 2013-03-26T17:00:50.370 回答