Skip to content

Instantly share code, notes, and snippets.

@mtn
Created July 30, 2019 04:31
Show Gist options
  • Save mtn/70186f54fb9f66e263b06572ca9424e0 to your computer and use it in GitHub Desktop.
Save mtn/70186f54fb9f66e263b06572ca9424e0 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Scroll demo</title>
<style>
#canvas {
height: 1200px;
background-color: rgb(69, 111, 225)
}
#it {
height: 100px;
width: 100px;
position: fixed;
top: 0px;
left: 0px;
background-color: red;
}
</style>
</head>
<body>
<div id="canvas"></div>
<div id="it"></div>
</body>
<script defer>
const [canvasRed, canvasGreen, canvasBlue] = [69, 111, 225];
const [itRed, itGreen, itBlue] = [255, 0, 0];
const canvas = document.querySelector('#canvas');
const it = document.querySelector('#it');
window.addEventListener('scroll', () => {
const y = 1 + (window.scrollY || window.pageYOffset) / 150;
const [cR, cG, cB] = [canvasRed/y, canvasGreen/y, canvasBlue/y].map(Math.round);
const [itR, itG, itB] = [itRed/y, itGreen/y, itBlue/y].map(Math.round);
canvas.style.backgroundColor = `rgb(${cR}, ${cG}, ${cB})`;
it.style.backgroundColor = `rgb(${itR}, ${itG}, ${itB})`;
})
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment