Skip to content

Instantly share code, notes, and snippets.

@zipcode
Created August 10, 2015 05:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zipcode/9d615320d52c0c2e97ec to your computer and use it in GitHub Desktop.
Save zipcode/9d615320d52c0c2e97ec to your computer and use it in GitHub Desktop.
Pulse inserted nodes in the DOM
function pulse(node, time) {
if (!node) throw new Error("node was falsy");
if (node.constructor && node.constructor == NodeList) {
var nodes = Array.prototype.slice.call(node);
nodes.forEach(function (node) { pulse(node, time); });
return;
}
if (!node.style) return;
if (time === undefined) {
time = 4000;
}
var start = null;
var original = node.style.backgroundColor;
function step(timestamp) {
if (start == null) start = timestamp;
var progress = timestamp - start;
if (progress < time) {
node.style.backgroundColor = "rgba(255, 0, 0, " + 0.5*(1-progress/time) + ")";
window.requestAnimationFrame(step);
} else {
node.style.backgroundColor = original;
}
}
window.requestAnimationFrame(step);
}
if (observer !== undefined) {
observer.disconnect();
}
var observer = new MutationObserver(function (records) {
records.forEach(function (record) {
if (!record.addedNodes) return;
pulse(record.addedNodes);
});
});
observer.observe(document.body, { childList: true, subtree: true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment