本文介绍了jQuery幻灯片重播开始或结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jsFiddle

使用下面的代码,我正在运行一个jQuery内容幻灯片,但是当im在开始或最后一张幻灯片时,下一个和上一个箭头消失时,我希望这些幻灯片可以继续返回到开始或最后一个幻灯片,具体取决于当前的幻灯片,如果您明白我的意思.

Using the below code I've got a jQuery content slideshow working but the next and previous arrows disappear when im either at the start or last slide, I would like the slides to continue back to the start or the last depending on the current slide if you get what I mean.

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() }
}

推荐答案

您必须删除对函数manageControls而不是行

You have to remove the calls to your function manageControls and instead of the line

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/小提琴.

See http://jsfiddle.net/WTvsM/1/ for a updated version of your fiddle.

这篇关于jQuery幻灯片重播开始或结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:59