Created
September 27, 2021 10:58
-
-
Save MariusBongarts/dcbe16e82bb5ca200af5adbaf605d2cb to your computer and use it in GitHub Desktop.
Background Color Changer
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
| const COLOR_PICKER_ID = "myColorPicker"; | |
| function createColorPicker() { | |
| const inputElement = document.createElement("input"); | |
| inputElement.type = "color"; | |
| inputElement.style.position = "absolute"; | |
| inputElement.id = COLOR_PICKER_ID; | |
| inputElement.style.display = `none`; | |
| inputElement.onclick = (e) => e.stopImmediatePropagation(); | |
| return inputElement; | |
| } | |
| document.body.appendChild(createColorPicker()); | |
| function getColorPicker() { | |
| return document.getElementById(COLOR_PICKER_ID); | |
| } | |
| function showColorPicker(x, y) { | |
| const colorPicker = getColorPicker(); | |
| colorPicker.style.display = `block`; | |
| colorPicker.style.left = `${x}px`; | |
| colorPicker.style.top = `${y}px`; | |
| } | |
| function hideColorPicker() { | |
| getColorPicker().style.display = `none`; | |
| } | |
| const renderColorPicker = (clickEvent) => { | |
| showColorPicker(clickEvent.clientX, clickEvent.clientY); | |
| getColorPicker().onchange = (event) => { | |
| clickEvent.target.style.backgroundColor = event.target.value; | |
| hideColorPicker(); | |
| }; | |
| }; | |
| document.addEventListener("dblclick", (event) => { | |
| renderColorPicker(event); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment