Skip to content

Instantly share code, notes, and snippets.

@atsukoba
Created January 8, 2020 11:39
Show Gist options
  • Save atsukoba/64c25d2e4cae2e51e0103a479ddd94b7 to your computer and use it in GitHub Desktop.
Save atsukoba/64c25d2e4cae2e51e0103a479ddd94b7 to your computer and use it in GitHub Desktop.
animation with element width value sample
# animation with element width value sample with JQuery
# generate style dom
generateCss = (name, diff) ->
# sample: marquee like animation
style = $("
<style>
@keyframes #{name} {
0% {
transform: translate(0%);
}
25% {
transform: translate(0%);
}
50% {
transform: translate(calc(-#{diff}px));
}
75% {
transform: translate(calc(-#{diff}px));
}
100% {
transform: translate(0%);
}
}
</style>
");
$("body").append(style);
childElements = $("CHILD_ELEMENT");
for i in [0..childElements.length]
elm = $(childElements[i]);
childWidth = elm.innerWidth();
parentWidth = elm.parent().innerWidth();
diff = childWidth - parentWidth;
if diff > 0
generateCss("elm#{i}", diff);
elm.css("animation-name", "elm#{i}");
elm.css("animation-duration", "#{.05 * diff}s")
elm.css("animation-timing-function", "linear");
elm.css("animation-iteration-count", "infinite");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment