Last active
October 9, 2015 10:27
-
-
Save benshimmin/3488473 to your computer and use it in GitHub Desktop.
Ridiculous jQuery carousel, in CoffeeScript - supports "blobs" and a timer
This file contains hidden or 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
carousel = (sel) -> | |
[$car, ci, ia] = [ $(sel), ".carousel-item", "is-active" ] | |
[$inner, len] = [ $car.find("> div"), $car.find(ci).length ] | |
[cur, blobs] = [ 0, [] ] | |
$inner.css "width" : len * (width = parseInt($car.find(ci) \ | |
.first().addClass(ia).css("width"), 10)) | |
rotate = -> | |
move.apply blobs[if ++cur <= len - 1 then cur else cur = 0], [false] | |
ti = setTimeout rotate, 3000 | |
move = (clicked = true) -> | |
pos = (parseInt $(@).attr("class").match(/\d/g).join(""), 10) - 1 | |
$inner.animate { "left" : pos * -width }, 250 | |
$car.find(".carousel-blob, #{ci}").removeClass ia | |
$.each [ $(@), $($car.find(ci)[pos]) ], (i, e) -> $(e).addClass ia | |
clearTimeout ti | |
ti = setTimeout rotate, 3000 | |
if clicked then cur = pos | |
do -> | |
$car.append $controls = $("<div class=\"carousel-controls\">") | |
((i) -> | |
$controls.append $blob = $("<div class=\"carousel-blob\ blob-#{i}\">") | |
$blob.addClass ia if i is 1 | |
blobs.push $blob.on "click", move | |
) i for i in [1..len] |
This file contains hidden or 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
<!-- compile the above, and then load it like this: --> | |
<script type="text/javascript" src="carousel.js"></script> | |
<script type="text/javascript"> | |
$(function() { carousel(".carousel"); }); | |
</script> | |
<!-- You'll need some CSS for classes including .carousel-item, | |
.carousel-controls, and .carousel-blob --> | |
<!-- Markup vaguely like this: --> | |
<div class="carousel"> | |
<div class="carousel-inner"> | |
<div class="carousel-item"> ... </div> | |
<div class="carousel-item"> ... </div> | |
<div class="carousel-item"> ... </div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment