Skip to content

Instantly share code, notes, and snippets.

@alifarukm
Created November 18, 2018 15:25
Show Gist options
  • Save alifarukm/e6b4fde7b53e661e00477060749a8356 to your computer and use it in GitHub Desktop.
Save alifarukm/e6b4fde7b53e661e00477060749a8356 to your computer and use it in GitHub Desktop.
Random rgb color generator javascript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Gist</title>
<style>
button {
background-color: aquamarine;
font-family: 'Courier New', Courier, monospace;
width: 20%;
margin: 20%;
}
body {
padding: 0;
margin: 0;
background-color: black;
}
.wrapper {
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div class="wrapper"><button>Click Me!</button></div>
<script>
let btn = document.querySelector('button');
function random(number) {
return Math.floor(Math.random() * (number + 1));
}
function bgChange() {
var rndCol =
'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')';
document.body.style.backgroundColor = rndCol;
}
btn.addEventListener('click', bgChange);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment