Skip to content

Instantly share code, notes, and snippets.

@ryandao
Created July 29, 2014 11:57
Show Gist options
  • Select an option

  • Save ryandao/b2774ec6be699cc5cd57 to your computer and use it in GitHub Desktop.

Select an option

Save ryandao/b2774ec6be699cc5cd57 to your computer and use it in GitHub Desktop.
Javascript for simple marquee effect
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
windowX = w.innerWidth || e.clientWidth || g.clientWidth,
windowY = w.innerHeight|| e.clientHeight|| g.clientHeight;
function cumulativeOffsetLeft(el) {
var left = 0;
while (el) {
left += el.offsetLeft || 0;
el = el.offsetParent;
}
return left;
}
function marquee(el, options) {
options = options || {};
var dir = options.dir || 'right';
var relativeToParent = options.relativeToParent || false;
var step = dir === 'left' ? -2 : 2;
var offsetWidth = el.offsetWidth;
var offsetLeft, outerWidth;
el.style.position = "relative";
if (relativeToParent) {
el.parentNode.style.overflow = "hidden";
offsetLeft = 0;
outerWidth = el.parentNode.offsetWidth;
} else {
offsetLeft = cumulativeOffsetLeft(el);
outerWidth = windowX;
}
window.setInterval(function() {
var left = parseInt(el.style.left) || 0;
left = left + step;
var cond = dir === 'left' ? (left + offsetLeft + offsetWidth < 0) : (left + offsetLeft > outerWidth);
if (cond) { left = 0; }
el.style.left = left + "px";
}, 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment