<section>
    <div class="grid">
        <div class="item" id="active">
            <h1>EyeDropper</h1>
            <p>点击颜色模块开启拾色器(吸管)模式获取当前光标位置颜色</p>
            <p>
                你当前所选的是:
                <mark id="name">颜色名</mark>
                <mark id="hex" style="text-decoration: underline;">HEX</mark>
            </p>
        </div>
        <div class="item" onclick="getColor(this)" style="background: Orange;">Orange</div>
        <div class="item" onclick="getColor(this)" style="background: BlueViolet;">BlueViolet</div>
        <div class="item" onclick="getColor(this)" style="background: Chartreuse;">Chartreuse</div>
        <div class="item" onclick="getColor(this)" style="background: DeepPink;">DeepPink</div>
        <div class="item" onclick="getColor(this)" style="background: ForestGreen;">ForestGreen</div>
        <div class="item" onclick="getColor(this)" style="background: GreenYellow;">GreenYellow</div>
        <div class="item" onclick="getColor(this)" style="background: Indigo;">Indigo</div>
        <div class="item" onclick="getColor(this)" style="background: LawnGreen;">LawnGreen</div>
        <div class="item" onclick="getColor(this)" style="background: Maroon;">Maroon</div>
        <div class="item" onclick="getColor(this)" style="background: olive;">olive</div>
        <div class="item" onclick="getColor(this)" style="background: OrangeRed;">OrangeRed</div>
        <div class="item" onclick="getColor(this)" style="background: WhiteSmoke;">
            <p>WhiteSmoke</p>
            <p>或移出工作区👉</p>
        </div>
    </div>
</section>
html,body{
    margin: 0;
}
.grid{
    display: grid;
    grid-template-columns: 25vw 25vw 25vw 25vw ;
    grid-template-rows: 25vh 25vh 25vh 25vh; 
}
.grid .item:first-of-type{
    background-color: #ef342a;
    cursor: pointer;
    color: black;
    text-shadow: 0 0 0.2em rgb(23, 252, 61), 0 0 0.2em rgb(36, 223, 19),0 0 0.2em rgb(0, 255, 85);
    grid-column: 1 / 3;
    grid-row: 1 / 3;
}
.grid .item:first-of-type::after{
    content: "";
}
.grid .item{
    position: relative;
    cursor: crosshair;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    border: 1px #f5f5f5 solid;
    font-size: 12px;
    padding: 1em;
    z-index: 0;
}
.grid .item::after{
    position: absolute;
    left: 0;
    top: 0;
    color: rgba(0,0,0,.05);
    content: "DeathGhost.cn";
    transform: scale(2) rotate(45deg);
    z-index: 1;
}
const active = document.querySelector('#active');
let colorName = document.querySelector('#name');
let colorHex = document.querySelector('#hex');
async function getColor(e) {
    if (!window.EyeDropper) {
        alert('浏览器不支持 EyeDropper!');
        return false;
    }
    const eyeDropper = new EyeDropper();
    const res = await eyeDropper.open().catch(error => console.warn('已取消!'));
    if (res) {
        // HEX
        active.style.background = res.sRGBHex;
        colorName.textContent = e.style.background;
        colorHex.textContent = res.sRGBHex;
    }
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.