Created
March 30, 2012 05:07
-
-
Save x86ed/2246670 to your computer and use it in GitHub Desktop.
A carousel from a recent site I worked on
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Carousel(id) { | |
var properties = { | |
$next: $(".js-next", id), | |
$prev: $(".js-prev", id), | |
$nav: $("ul.js-nav", id), | |
$players: $("a.playVideo", id), | |
$items: $("ul.js-carousel-items", id), | |
$item:$("li.item", id) | |
}; | |
var settings = { | |
timing: 1000, | |
index: 0, | |
count: properties.$items.find("li").length - 1 | |
}; | |
function move(direction, amount, instant) { | |
if (parseInt($("img.logo").css("font-size")) == 0) { | |
heroVideo.hidePlayer(); | |
} | |
instant = typeof (instant) != 'undefined' ? instant : false; | |
for (var i = amount; i > 0; i--) { | |
properties.$items.animate({ left: '+=' + direction * properties.$items.find("li.item").width() }, settings.timing, function () { | |
var popIndex = direction < 0 ? 0 : properties.$items.find('li').length - 1; | |
var shellCasing = $(properties.$items.find("li")[popIndex]).detach(); | |
properties.$items.css('left', -properties.$items.find("li.item").width()); | |
if (direction < 0) { | |
shellCasing.appendTo(properties.$items); | |
} else { | |
shellCasing.prependTo(properties.$items); | |
} | |
if (settings.index + direction >= 0) { | |
settings.index += (direction); | |
} | |
if (settings.index + direction > properties.$nav.length - 1) { | |
settings.index = 0; | |
} | |
settings.count += direction; | |
if (settings.count < 0) { | |
settings.count = properties.$items.find('li').length - 1; | |
} | |
if (settings.count > properties.$items.find('li').length - 1) { | |
settings.count = 0; | |
} | |
updateNav(settings.count); | |
}); | |
} | |
} | |
function moveTo(startAt, endAt) { | |
var amt = Math.abs(endAt - startAt); | |
var dir = endAt - startAt > 0 ? 1 : -1; | |
if (endAt - startAt) { | |
move(dir, amt); | |
} else { | |
move(dir, amt, true); | |
} | |
} | |
function prev() { | |
move(1, 1); | |
} | |
function next() { | |
move(-1, 1); | |
} | |
function updateNav(setNav) { | |
properties.$nav.find("li a").removeClass("current"); | |
$(properties.$nav.find("li a")[setNav]).addClass("current"); | |
} | |
function hideControls() { | |
properties.$next.stop(true).animate({ right: "-45px" }, "fast"); | |
properties.$prev.stop(true).animate({ left: "-45px" }, "fast"); | |
properties.$nav.stop(true).fadeOut("fast"); | |
} | |
function showControls() { | |
properties.$next.stop(true).animate({ right: "0" }, "fast"); | |
properties.$prev.stop(true).animate({ left: "0" }, "fast"); | |
properties.$nav.stop(true).fadeIn("fast"); | |
} | |
function init() { | |
var shellCasing = $(properties.$items.find("li")[properties.$items.find('li').length - 1]).detach(); | |
shellCasing.prependTo(properties.$items); | |
properties.$items.css('left', -properties.$items.find("li.item").width()); | |
properties.$next.click(function (e) { | |
e.preventDefault(); | |
next(); | |
}); | |
properties.$players.click(function (e) { | |
e.preventDefault(); | |
heroVideo.LoadNPlay($(this)); | |
}); | |
properties.$prev.click(function (e) { | |
e.preventDefault(); | |
prev(); | |
}); | |
properties.$item.click(function () { | |
heroVideo.LoadNPlay($("a.playVideo", $(this))); | |
}) | |
properties.$items.find("li").each(function (index) { | |
var classy = index == (properties.$items.find("li").length - 1) ? 'class="current"' : ""; | |
$('<li><a href="#" ' + classy + 'rel="' + (properties.$items.find("li").length - index - 1) + '" > </a></li>').appendTo(properties.$nav) | |
}); | |
$(id).hover(function () { showControls(); }, function () { hideControls(); }); | |
$("body").bind('videoComplete', function () { heroVideo.hidePlayer(); }); | |
properties.$items.css("width", properties.$items.find("li").length * properties.$items.find("li").width() + "px"); | |
} | |
init(); | |
return { | |
prev: prev, | |
next: next, | |
move: move, | |
moveTo: moveTo, | |
updateNav: updateNav | |
} | |
}; | |
function Video(isIpad) { | |
var hidePlayer = function () { | |
Flash.stopVideo(); | |
$("#carouselVideo").css("left", "-754px"); | |
$("a.playVideo").fadeIn("fast"); | |
$("img.logo").animate({ fontSize: "10px" }, "fast"); | |
} | |
var loadNPlay = function (video) { | |
$("#carouselVideo").css('left', 0); | |
$("img.logo").animate({ fontSize: 0 }, "fast"); | |
$("a.playVideo").fadeOut("fast", function () { | |
if (!$("a.playVideo").index($(this))) { | |
Flash.loadVideo(video.attr("href")); | |
} | |
}); | |
} | |
if (isIpad) { | |
hidePlayer = function () { | |
$("#carouselVideo").css("left", "-754px"); | |
$("#carouselVideo video").remove(); | |
$("a.playVideo").fadeIn("fast"); | |
$("img.logo").animate({ fontSize: "10px" }, "fast"); | |
} | |
function onCanPlay() { | |
var $TheVid = $("#carouselVideo video") | |
$TheVid.removeEventListener('canplaythrough', onCanPlay, false); | |
$TheVid.removeEventListener('load', onCanPlay, false); | |
//video is ready | |
$TheVid.play(); | |
} | |
loadNPlay = function (video) { | |
$("#carouselVideo").css('left', 0); | |
$("img.logo").animate({ fontSize: 0 }, "fast"); | |
$(video).fadeOut("fast", function () { | |
$('<video autoplay="autoplay" controls="controls"></video>').appendTo("#carouselVideo"); | |
var $TheVid = $("#carouselVideo video") | |
$TheVid.attr("src", video.attr("href")); | |
$TheVid.play(); | |
if ($TheVid.readyState !== 4) { | |
$TheVid.addEventListener('canplaythrough', onCanPlay, false); | |
$TheVid.addEventListener('load', onCanPlay, false); | |
setTimeout(function () { | |
$TheVid.pause(); / | |
}, 1); | |
} | |
}); | |
} | |
} | |
return { | |
hidePlayer: hidePlayer, | |
LoadNPlay: loadNPlay | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment