var ScrollAnimator = function() { var M = {}, e, Q = false; var G = $(window), O = $(document), K = false, F = { x: 0, y: 0 }, n = 0, A = scrollTop = 0; var o; function x() { requestAnimFrame(x); if(Math.ceil(A) !== Math.floor(scrollTop)) { A += M.tweenSpeed * (scrollTop - A); $("#status").html(scrollTop); for(var d in animation) { var w = animation[d]; if(A >= w.startAt && A <= w.endAt) { p(w); C(w); } else { l(w); } } if(typeof M.onUpdate === "function") { M.onUpdate(); } } } function C(U) { var w = (U.startAt - A) / (U.startAt - U.endAt); var W = U.endAt - U.startAt; var V = A - U.startAt; var S = {}; if(U.keyframes) { for(i = 1; i < U.keyframes.length; i++) { var R = U.keyframes[i], T = U.keyframes[i - 1], d = (T.position - w) / (T.position - R.position); if(d > 0 && d < 1) { if(R.onProgress && typeof R.onProgress === "function") { R.onProgress(d, W, V); } for(property in R.properties) { S[property] = h(T.properties[property], R.properties[property], d, 1, R.ease); } } } } U._elem.css(S); if(U.onProgress && typeof U.onProgress === "function") { U.onProgress.call(U, w, W, V); } } function p(d) { if(!d._started) { if(d.onStartAnimate && typeof d.onStartAnimate === "function") { d.onStartAnimate.call(d); } else { d._elem.css("display", "block"); } d._started = true; } } function l(d) { if(d._started && d.endAt < A || d._started && d.startAt > A) { if(d.onEndAnimate && typeof d.onEndAnimate === "function") { d.onEndAnimate.call(d); } else { d._elem.css("display", "none"); } d._started = false; } } function a() { for(var T in animation) { var W = animation[T]; if(W._elem == undefined) { W._elem = $("#" + W.id); } for(var w in W.keyframes) { var S = W.keyframes[w]; if(S.position == 0) { var U = W.keyframes[Number(w) + 1]; for(V in U.properties) { if(S.properties[V] == undefined) { if(/left|top/.test(V)) { S.properties[V] = W._elem.position()[V]; } } } } var d = Number(w); while(d > 0) { var R = W.keyframes[d]; for(var V in R.properties) { if(S.properties[V] == undefined) { S.properties[V] = R.properties[V]; } } d--; } if(typeof S.onInit == "function") { S.onInit(W); } } } } function q() { if(M.onResize && typeof M.onResize === "function") { M.onResize(); } var d = M.container; e = { wWidth: M.container.width(), wHeight: M.container.height(), wCenter: { left: M.container.width() / 2, top: M.container.height() / 2 } }; r(); a(); u(); } function r() { for(var d in animation) { var w = animation[d]; if(w._started) { delete w._elem; delete w._started; } } } function H(d) { q(); } function s(d) { F.x = d.touches[0].pageX; F.y = d.touches[0].pageY; n = scrollTop; } function g(d) {} function t(d) { d.preventDefault(); offset = {}; offset.x = F.x - d.touches[0].pageX; offset.y = F.y - d.touches[0].pageY; scrollTop = Math.max(0, n + offset.y); I(); } function v(R, S, w, d) { if($("html").hasClass("scrollEnabled")) { scrollTop -= S * M.scrollSpeed; if(scrollTop < 0) { scrollTop = 0; } I(); } } function I() { if(scrollTop < 0) { scrollTop = 0; } else { if(scrollTop > M.maxScroll) { scrollTop = M.maxScroll; } } if(typeof o == "number") { clearTimeout(o); } o = setTimeout(function() { y(); }, 1000); } function y() { for(var d in M.animationSections) { if(M.animationSections[d] - scrollTop > 0 && M.animationSections[d] - scrollTop < M.autoSectionLock) { P(M.animationSections[d]); } } } function h(V, d, S, w, T) { var U = d - V; var R = S / w; if(!T) { T = TWEEN.Easing.Linear.EaseNone; } return T(R) * U + V; } function k() { return "ontouchstart" in window; } function L(w) { var R = { maxScroll: 1000, tickSpeed: 30, scrollSpeed: 20, useRAF: true, tweenSpeed: 0.3, freezeTouchScroll: false, autoSectionLock: 0 }; M = $.extend(R, w); animation = M.animation; K = k(); if(K) { var d = M.container[0]; d.addEventListener("touchstart", s, true); d.addEventListener("touchmove", t, true); d.addEventListener("touchend", g, true); } O.on("mousewheel", v); G.on("resize", H); window.requestAnimFrame = (function() { if(M.useRAF) { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(S) { window.setTimeout(S, M.tickSpeed); }; } else { return function(S) { window.setTimeout(S, M.tickSpeed); }; } })(); q(); return this; } function u() { if(!Q && M.startAt) { A = scrollTop = M.startAt; } scrollTop++; if(!Q) { x(); Q = true; } if(M.onStart && typeof M.onStart === "function") { M.onStart(); } } function f() { return e; } function b() { return A; } function E() { return M.maxScroll; } function c() { return M.scrollSpeed; } function P(d) { scrollTop = d; } function D() { setInterval(z, 100); } function z() { scrollTop += 5; if(scrollTop > M.maxScroll) { scrollTop = A = 0; } } function B() { A = scrollTop; } function j() { M.freezeTouchScroll = true; } function N() { M.freezeTouchScroll = false; } function m() { if(M.debugId == false || M.debugId == undefined) { console.log("debug on"); M.debugId = true; $("#status").show(); } else { console.log("debug off"); M.debugId = false; $("#status").hide(); } for(var d in animation) { var w = animation[d]; if(M.debugId == true) { w._elem.css("border", "1px dashed red"); w._elem.prepend('
' + w.id + "
"); } else { w._elem.css("border", ""); $("body").find(".debugid").remove(); } } } function J() { return M.debug; } return { init: L, start: u, getPageInfo: f, getScrollTop: b, getMaxScroll: E, getScrollSpeed: c, autoScroll: D, stopScroll: B, scrollTo: P, freezeTouchScroll: j, unfreezeTouchScroll: N, isDebug: J, toggleDebug: m }; };