Created
February 16, 2013 18:42
-
-
Save epitron/a77a93aa9ced4d257ccf to your computer and use it in GitHub Desktop.
A simple Carousel in CoffeeScript.
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
$ -> | |
page_size = 4 | |
container = $(".texts-container") | |
all_texts = -> container.find(".texts") | |
total_pages = -> all_texts().length | |
# Show the first page. | |
current = null | |
width = null | |
initialize_texts = -> | |
current = all_texts().first() | |
width = current.width() | |
current.css top: "0px", left: "0px" | |
initialize_texts() | |
window.refresh_texts = -> | |
load_texts 0, (response)-> | |
#console.log response | |
container.html(response) | |
initialize_texts() | |
load_texts = (first_page, cb)-> | |
amount = 3 | |
params = filter_params() | |
params.offset = first_page * page_size | |
params.limit = amount * page_size | |
if $("#gallery .checkbox-container").hasClass("checked") | |
params.language = "all" | |
else | |
params.language = lang | |
$.get "/pages/texts", params, cb | |
load_more_texts = -> | |
load_texts total_pages(), (response)-> | |
texts = $(response) | |
if texts.length > 0 | |
$(".texts-container").append(texts) | |
# Spin the carousel with rotate(1) or rotate(-1) | |
rotate = (factor)-> | |
return if all_texts().filter(":animated").any() | |
if factor >= 0 | |
# forwards | |
console.log("forwards") | |
target = current.next() | |
sign = "-" | |
else | |
# backwards | |
console.log("backwards") | |
target = current.prev() | |
sign = "+" | |
if target.any() | |
target.css left: "#{width*factor}px", top: "0px" | |
current.animate left: "#{sign}=#{width}px" | |
target.animate left: "#{sign}=#{width}px" | |
if target.nextAll().length is 0 | |
load_more_texts() | |
current = target | |
# Buttons | |
$("#text_advice .next-text").click -> rotate(1) | |
$("#text_advice .prev-text").click -> rotate(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment