Skip to content

Instantly share code, notes, and snippets.

@syads321
Created July 2, 2014 01:56
Show Gist options
  • Save syads321/8a01a10e33e7b52b7274 to your computer and use it in GitHub Desktop.
Save syads321/8a01a10e33e7b52b7274 to your computer and use it in GitHub Desktop.
Animframe
var raf = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame;
Bridge.animLoop = function( render, element ) {
var running, lastFrame = +new Date;
function loop( now ) {
if ( running !== false ) {
raf ?
raf( loop, element ) :
// fallback to setTimeout
setTimeout( loop, 16 );
// Make sure to use a valid time, since:
// - Chrome 10 doesn't return it at all
// - setTimeout returns the actual timeout
now = now && now > 1E4 ? now : +new Date;
var deltaT = now - lastFrame;
// do not render frame when deltaT is too high
if ( deltaT < 160 ) {
running = render( deltaT, now );
}
lastFrame = now;
}
}
loop();
};
$(document).ready(function () {
var t = null;
var itemwidth = $('span.news').outerWidth();
var multiplier = 0.2;
var interval = 60 * multiplier;
var cacheinterval = interval;
Bridge.animLoop(function() {
interval--
if (interval < 0) {
console.log('runns')
interval = cacheinterval;
if ($('span.news').position().left < (0 - itemwidth)) {
$('span.news').css('left', $('span.news').parent().width() + 'px');
} else {
$('span.news').css('left', $('span.news').position().left - 10 + 'px');
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment