-
Notifications
You must be signed in to change notification settings - Fork 14
/
014.颜色转换.html
80 lines (73 loc) · 2.41 KB
/
014.颜色转换.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./assets/global.css">
<style>
body {
width: 100vw;
height: 100vh;
transition: all 1s ease-in;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
#rgbColorDiv {
font-size: 40px;
margin-bottom: 20px;
color: rgba(0, 0, 0, .9);
text-shadow: 2px 0 rgba(255, 0, 0, .9)
}
#hexColorInput {
background-color: rgba(255, 255, 255, .7);
outline: none;
border: 1px solid rgba(0, 0, 0, .3);
line-height: 40px;
font-size: 20px;
padding-left: 20px;
width: 60vmin;
margin-bottom: 200px;
}
</style>
</head>
<body>
<div id="rgbColorDiv"></div>
<input id="hexColorInput" type="text" placeholder="#6cf">
<script type="module">
import { Convertor } from "https://unpkg.com/@3r/tool/lib/convertor.js"
let rgbColorDiv = document.getElementById('rgbColorDiv')
let hexColorInput = document.getElementById('hexColorInput')
hexColorInput.addEventListener('input', hexColorInputFunc)
rgbColorDiv.addEventListener('click', copyToClipboard)
hexColorInputFunc({ target: { value: '#6cf' } })
/**
* 输入事件
*/
function hexColorInputFunc(e) {
try {
let rgb = Convertor.hexToRgb(e.target.value)
rgbColorDiv.textContent = rgb;
document.body.style.backgroundColor = rgb;
} catch (error) {
console.log(error);
}
}
/**
* 复制到剪贴板
*/
function copyToClipboard(e) {
let input = document.querySelector('.copy-input') || document.createElement('input');
input.className = "copy-input";
input.value = e.target.textContent;
input.style = `position: fixed;top: -100%;`
document.body.append(input)
input.select();
document.execCommand("copy");
console.log("复制完成");
}
</script>
</body>
</html>