Advertisement
avr39ripe

jsColorPickerBasic

Apr 5th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Study</title>
  6.     <style>
  7.         html,
  8.         body {
  9.             display: flex;
  10.             width: 95%;
  11.             height: 95%;
  12.             min-width: 810px;
  13.             min-height: 410px;
  14.             align-items: center;
  15.             justify-content: center;
  16.             margin: 8px 15px;
  17.         }
  18.  
  19.         .centered {
  20.             display: flex;
  21.             flex-direction: row;
  22.             align-items: center;
  23.             justify-content: center;
  24.             padding: 50px;
  25.         }
  26.  
  27.         #main {
  28.             width: 100%;
  29.             height: 50%;
  30.             flex-direction: column;
  31.         }
  32.  
  33.         #rgbContainer {
  34.             width: 100%;
  35.             height: 50%;
  36.             background-color: grey;
  37.             border: 2px solid black;
  38.         }
  39.  
  40.         .colorBlock {
  41.             display: flex;
  42.             flex-direction: column;
  43.             align-items: center;
  44.             justify-content: center;
  45.             width: 30%;
  46.             height: 95%;
  47.             margin: auto;
  48.             border: 4px solid white;
  49.         }
  50.  
  51.         #red {
  52.             background-color: red;
  53.         }
  54.  
  55.         #green {
  56.             background-color: green;
  57.         }
  58.  
  59.         #blue {
  60.             background-color: blue;
  61.         }
  62.  
  63.     </style>
  64. </head>
  65. <body>
  66.     <div id="main" class="centered">
  67.         <div id="rgbContainer" class="centered" tabindex="1">
  68.             <div id="red" class="colorBlock">
  69.             </div>
  70.             <div id="green" class="colorBlock">
  71.             </div>
  72.             <div id="blue" class="colorBlock">
  73.             </div>
  74.         </div>
  75.     </div>
  76.     <script>
  77.         'use strict'
  78.  
  79.         let colorR = 0;
  80.         let colorG = 0;
  81.         let colorB = 0;
  82.  
  83.         function onWheelR(event) {
  84.             event.preventDefault();
  85.             const rgbContainer = document.getElementById('rgbContainer');
  86.             colorR += event.deltaY * -0.1;
  87.             colorR = Math.min(Math.max(0, colorR), 255);
  88.  
  89.             rgbContainer.style.backgroundColor = `rgb(${colorR}, ${colorG}, ${colorB})`;
  90.  
  91.             console.log(`${event.deltaY} <-> ${event.deltaY * -0.1} -> ${colorR}`);
  92.         }
  93.  
  94.         function onWheelG(event) {
  95.             event.preventDefault();
  96.             const rgbContainer = document.getElementById('rgbContainer');
  97.             colorG += event.deltaY * -0.1;
  98.             colorG = Math.min(Math.max(0, colorG), 255);
  99.  
  100.             rgbContainer.style.backgroundColor = `rgb(${colorR}, ${colorG}, ${colorB})`;
  101.  
  102.             console.log(`${event.deltaY} <-> ${event.deltaY * -0.1} -> ${colorG}`);
  103.         }
  104.  
  105.         function onWheelB(event) {
  106.             event.preventDefault();
  107.             const rgbContainer = document.getElementById('rgbContainer');
  108.             colorB += event.deltaY * -0.1;
  109.             colorB = Math.min(Math.max(0, colorB), 255);
  110.  
  111.             rgbContainer.style.backgroundColor = `rgb(${colorR}, ${colorG}, ${colorB})`;
  112.  
  113.             console.log(`${event.deltaY} <-> ${event.deltaY * -0.1} -> ${colorB}`);
  114.         }
  115.  
  116.         function onWheel(event) {
  117.             event.preventDefault();
  118.             const rgbContainer = document.getElementById('rgbContainer');
  119.             const targetId = event.target.id;
  120.  
  121.             if (targetId == 'red') {
  122.                 colorR += event.deltaY * -0.1;
  123.                 colorR = Math.min(Math.max(0, colorR), 255);
  124.             } else if (targetId == 'green') {
  125.                 colorG += event.deltaY * -0.1;
  126.                 colorG = Math.min(Math.max(0, colorG), 255);
  127.             }
  128.             else if (targetId == 'blue') {
  129.                 colorB += event.deltaY * -0.1;
  130.                 colorB = Math.min(Math.max(0, colorB), 255);
  131.             }
  132.  
  133.             rgbContainer.style.backgroundColor = `rgb(${colorR}, ${colorG}, ${colorB})`;
  134.             //console.log(`${event.deltaY} <-> ${event.deltaY * -0.1} -> ${colorB}`);
  135.         }
  136.  
  137.         //const red = document.getElementById('red');
  138.         //const green = document.getElementById('green');
  139.         //const blue = document.getElementById('blue');
  140.         //red.addEventListener('wheel', onWheelR);
  141.         //green.addEventListener('wheel', onWheelG);
  142.         //blue.addEventListener('wheel', onWheelB);
  143.  
  144.         const rgbContainer = document.getElementById('rgbContainer');
  145.         rgbContainer.addEventListener('wheel', onWheel);
  146.  
  147.     </script>
  148. </body>
  149. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement