Created
August 10, 2015 05:45
-
-
Save zipcode/9d615320d52c0c2e97ec to your computer and use it in GitHub Desktop.
Pulse inserted nodes in the DOM
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
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