Created
December 16, 2015 04:01
-
-
Save sunmockyang/d950bb4bb2fcb1ba80ec to your computer and use it in GitHub Desktop.
Highlight an element and fade highlight after a delay
This file contains hidden or 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 HighlightItem(delay, element) { | |
| var startTime = -1; | |
| var duration = 3000; | |
| var startC = [255, 255, 255, 0]; | |
| var highlightC = [255, 255, 100, 1]; | |
| element.style.backgroundColor = "rgba(" + highlightC.join() + ")"; | |
| function highlightLoop(){ | |
| var t = (Date.now() - startTime) / duration; | |
| if (t > 1) { | |
| return; | |
| } | |
| t = Math.sin(t * (Math.PI) + Math.PI/2) / 2 + 0.5; | |
| var color = [LerpColor(startC[0], highlightC[0], t), LerpColor(startC[1], highlightC[1], t), LerpColor(startC[2], highlightC[2], t), Lerp(startC[3], highlightC[3], t)] | |
| element.style.backgroundColor = "rgba(" + color.join() + ")"; | |
| window.requestAnimationFrame(highlightLoop); | |
| }; | |
| setTimeout(function() { | |
| startTime = Date.now(); | |
| highlightLoop(); | |
| }, delay); | |
| } | |
| function LerpColor(from, to, t) { | |
| return Math.floor(Lerp(from, to, t)); | |
| }; | |
| function Lerp(from, to, t) { | |
| return (to - from) * t + from; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment