initCarousel();
}
-var autoplay = true;
-var autoplayTimer;
+var firstLoad = true,
+ autoplay = true,
+ autoplayTimer,
+ userHasClicked = false,
+ carouselIndex = 0;
function initCarousel() {
// Handle nav clicks
$(document).on('click', '.services-examples-nav a', function() {
- clearInterval(autoplayTimer); // cancel autoplay
+ userHasClicked = true;
+ autoplayStop();
loadSlide($(this).attr('href'));
return false;
});
- // Autoplay
- if (autoplay) {
- var $items = $(".services-examples > div");
- var index = 0;
+ // Handle play / pause on hover
+ $('.services-examples').hover(autoplayStop, autoplayStart);
- var showNext = function () {
- loadSlide('#example' + index);
- // increment the index - if it is beyond the number of li's - reset it to 0
- if (++index >= $items.length) index = 0;
- };
- showNext(); // call once upon loading
-
- // set it up to be called every x ms
- autoplayTimer = setInterval(showNext, 3000);
+ if (autoplay) {
+ autoplayStart();
}
}
$('.services-examples .current').css('z-index', "").hide().removeClass('current');
$(this).css('z-index', "").addClass('current');
});
+}
+
+function autoplayStart() {
+
+ // Only start autoplay if user hasn't clicked any of the labels
+ // This should trigger on first load and also after mouseout when hovering
+ if (!userHasClicked) {
+
+ var $items = $(".services-examples > div");
+
+ var showNext = function () {
+ loadSlide('#example' + carouselIndex);
+ // increment the carouselIndex - if it is beyond the number of li's - reset it to 0
+ if (++carouselIndex >= $items.length) carouselIndex = 0;
+ };
+
+ if (firstLoad) {
+ showNext(); // call once upon loading
+ firstLoad = false;
+ }
+
+ // set it up to be called every x ms
+ autoplayTimer = setInterval(showNext, 3000);
+ }
+
+}
+
+function autoplayStop() {
+ clearInterval(autoplayTimer); // cancel autoplay
+ autoplay = false;
}
\ No newline at end of file