Last active
August 29, 2015 14:10
-
-
Save thedgbrt/1b963f28096ae3ae93e0 to your computer and use it in GitHub Desktop.
Add easing to setTimeout loop
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
// self-invoking function | |
// main idea from here http://stackoverflow.com/questions/12081547/applying-easing-to-settimeout-delays-within-a-loop | |
// easing functions from here https://github.com/danro/jquery-easing/blob/master/jquery.easing.js | |
(function(){ | |
var time = 0; | |
var diff = 12; | |
var minTime = 1; | |
var maxTime = 100; | |
var elList = document.querySelectorAll("li, h1, h2, h3, h4, h5, h6, p"); | |
if(elList.length>0){ | |
var i = 0; | |
function myLoop () { | |
setTimeout(function () { | |
var el = elList[i]; | |
var elOldClass = el.className; | |
el.className = elOldClass.concat(" appear"); | |
if (i < elList.length-1){ | |
time = noEasing(i, minTime, maxTime, diff); | |
myLoop(); | |
} | |
i++; | |
}, time); | |
} | |
myLoop(); | |
} | |
function noEasing (t, b, c, d) { | |
return c * t / d + b; | |
} | |
function easeInOutQuad(t, b, c, d) { | |
if ((t/=d/2) < 1) return c/2*t*t + b; | |
return -c/2 * ((--t)*(t-2) - 1) + b; | |
} | |
function easeInOutExpo(t, b, c, d) { | |
if (t==0) return b; | |
if (t==d) return b+c; | |
if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; | |
return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment