Last active
December 21, 2020 15:28
-
-
Save AllanChain/2a03c289e84820b3072d26a0c68418f3 to your computer and use it in GitHub Desktop.
Simple color picker page
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Color Picker</title> | |
<style> | |
:root { | |
--current-color: #000000; | |
} | |
body { | |
text-align: center; | |
color: var(--current-color); | |
} | |
#color-text { | |
width: 100px; | |
color: var(--current-color); | |
outline: slategray auto 1px; | |
margin-right: 10px; | |
} | |
#color-input { | |
width: 100px; | |
} | |
</style> | |
</head> | |
<body> | |
<div> | |
<h2>😎这是示例文本🐶</h2> | |
<p>点击色块调色</p> | |
<p> | |
# | |
<input id="color-text" oninput="check_and_update(event.target.value)" /> | |
</p> | |
<input | |
id="color-input" | |
type="color" | |
oninput="update(event.target.value)" | |
/> | |
</div> | |
<script> | |
const update = (color) => { | |
document.documentElement.style.setProperty("--current-color", color); | |
document.getElementById("color-text").value = color.substring(1); | |
document.getElementById("color-input").value = color; | |
localStorage.setItem("color-picker.color", color); | |
}; | |
const check_and_update = (color) => { | |
if (/[0-9A-Fa-f]{6}/.test(color)) { | |
document.getElementById("color-text").style.outlineColor = "green"; | |
update("#" + color); | |
} else document.getElementById("color-text").style.outlineColor = "red"; | |
}; | |
update(localStorage.getItem("color-picker.color") || "#000000"); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment