Advertisement
n128

Random Background Color

Jun 22nd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.06 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="utf8">
  5.         <title>Random Background Color</title>
  6.        
  7.         <style>
  8.             html, body {
  9.                 min-width: 100vw;
  10.                 min-height: 100vh;
  11.                 margin: 0 auto;
  12.                 padding: 0;
  13.                 display: flex;
  14.                 justify-content: center;
  15.                 align-items: center;
  16.             }
  17.            
  18.             .container {
  19.                 min-width: 90%;
  20.                 position: relative;
  21.                 text-align: center;
  22.             }
  23.            
  24.             .colorInfo {
  25.                 font-size: 17pt;
  26.             }
  27.             .colorInfo .currentColor {
  28.                 font-size: 20pt;
  29.                 font-weight: bold;
  30.             }
  31.            
  32.             .button {
  33.                 padding: 7px;
  34.                 border-radius: 5px;
  35.                 border: none;
  36.                 font-size: 13pt;
  37.                
  38.                 background-color: #111;
  39.                 color: #fff;
  40.             }
  41.             .button:hover {cursor: pointer}
  42.             .button:active {background: #000}
  43.            
  44.             @media (min-height: 800px) {
  45.                 .colorInfo {
  46.                     font-size: 32pt;
  47.                 }
  48.                
  49.                 .colorInfo .currentColor {
  50.                     font-size: 35pt;
  51.                 }
  52.            
  53.                 .button {
  54.                     padding: 19px;
  55.                     font-size: 25pt;
  56.                 }
  57.             }
  58.         </style>
  59.     <head>
  60.     <body>
  61.        
  62.         <main role="main">
  63.             <div class="container">
  64.                 <p class="colorInfo">The background color is: <span id="currentColor" class="currentColor">#FFFFFF</span></p>
  65.                
  66.                 <button id="btnChangeBackground" class="button">Change the background color</button>
  67.             </div>
  68.         </main>
  69.        
  70.         <script type="text/javascript">
  71.             const button = document.querySelector('#btnChangeBackground')
  72.             const currentColor = document.querySelector('#currentColor')
  73.             const body = document.body
  74.             const hexNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']
  75.            
  76.             button.addEventListener('click', changeBackground)
  77.            
  78.             function changeBackground() {
  79.                 let hex = generateRandomHex()
  80.                                
  81.                 body.style.backgroundColor = hex
  82.                 currentColor.textContent = hex;
  83.             }
  84.            
  85.             function generateRandomHex() {
  86.                 let random,
  87.                     hex = '#';
  88.                 for (let i = 0, l = hexNumbers.length; i < 6; i++) {
  89.                     random = Math.round(Math.random() * (l - 1))
  90.                     hex += hexNumbers[random]
  91.                 }
  92.                 return hex
  93.             }
  94.         </script>
  95.     </body>
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement