jQuery实现左右切换焦点图

2020-05-16 18:44:21易采站长站整理

}

// stop any existing timers, additionally update the play/pause button to the correct
// visual state
function stop($carousel)
{
clearInterval(timer);

$carousel.prop('timer', false);
$carousel.find('.pause').addClass('play');

//N.C.: added to stop carousel after one round.
carousel_round = 0;
}

function nextSlide($carousel, index)
{
var $slides = $carousel.find('.carousel-item');

if (index+1 < $slides.size())
{
return {"index":index+1, "element":$slides.eq(index+1)};
}

return {"index":0, "element":$slides.eq(0)};
}

function previousSlide($carousel, index)
{
var $slides = $carousel.find('.carousel-item');

if (index-1 >= 0)
{
return {"index":index-1, "element":$slides.eq(index-1)};
}

return {"index":$slides.size()-1, "element":$slides.eq(-1)};
}

// build the controls for inclusion into the page
var $previousBtn = $('<a />', {"class": "previous", "href": "#", "text": "Previous"});
var $playPauseBtn = $('<a />', {"class": "play pause", "href": "#", "text": "Play/Pause"});
var $nextBtn = $('<a />', {"class": "next", "href": "#", "text": "Next"});
var $controlsDiv = $('<div />', {"class": "carousel-controls"});
$controlsDiv.append($previousBtn);
$controlsDiv.append($playPauseBtn);
$controlsDiv.append($nextBtn);

// loop through each carousel and set some default properties/styles
$('.carousel').each(function()
{
// localize the carousel to this function
var $carousel = $(this);

// set the positioning and a default height, becuase we're going to be taken out of the
// flow once our animation starts
$carousel.css({
"position": "relative"
//"min-height": $carousel.find('.carousel-item').eq(0).outerHeight() //N.C. commented out
});

// store the currently visible slide's index
$carousel.prop('index', 0);

// hide subsequent slides
$carousel.find('.carousel-item:gt(0)').hide();

// append in our controls
$carousel.prepend($controlsDiv.clone(true));

// add the previous/next images
$carousel.find('.main-image').each(function(index)
{
// get the previous image
var $prevImage = $(previousSlide($carousel, index).element).find('.main-image').clone();

// remove the class
$prevImage.removeClass('main-image');

// create a link for the previous image
var $previousAnchor = $('<a />', {
"href": "#",
"class": "prev-image",
"html": $prevImage
});
$previousAnchor.css('opacity', 0.2);

// add in the previous image/anchor
$(this).before($previousAnchor);

// get the next image