欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用JavaScript實現(xiàn)隨機顏色生成器

 更新時間:2022年08月09日 09:05:57   作者:海擁  
這篇文章主要為大家詳細介紹了如何使用JavaScript+CSS實現(xiàn)一個隨機顏色生成器,文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下

在線演示地址

項目基本結(jié)構(gòu)

目錄結(jié)構(gòu)如下:

1.顏色生成器的基本結(jié)構(gòu)

我使用了以下的 HTML 和 CSS 代碼創(chuàng)建了這個顏色生成器的基本結(jié)構(gòu)。在添加所有信息的頁面上創(chuàng)建了一個小框,框的背景顏色為白色。

<div class="container">

</div>
*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    border: none;
    outline: none;
    font-family: sans-serif;
}

body{
    background-color: #0574c8;
}

.container{
    background-color: white;
    width: 60vmin;
    padding: 2.5em 1.1em;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    font-size: 3vmin;
    border-radius: 10px;
}

2.使用 HTML 添加標(biāo)題

現(xiàn)在,我們使用 HTML 的 h1 標(biāo)簽在此框中添加一個標(biāo)題來增強一下美感,并在 CSS 的幫助下進行了設(shè)計。

<h1>Color Generator</h1>
.container h1{
  font-size: 27px;
  text-align: center;
  margin-top: -20px;
  color: #09599a;
  margin-bottom: 20px;
}

3.創(chuàng)建用于顏色查看的顯示器

HTML 和 CSS 代碼有助于創(chuàng)建顯示,顯示器基本上是通過產(chǎn)生顏色來觀看的。每當(dāng)你點擊生成按鈕時,都可以在此顯示中看到顏色。它的寬度為 100%,高度為 30vmin,并用了箱形陰影來增強美感。

<div id="output-color">
    <span></span>
 </div>
#output-color{
    position: relative;
    height: 30vmin;
    width: 100%;
     box-shadow: 0 0 20px rgba(0,139,253,0.25);
    border: 2px solid #ffffff;
    margin: auto;
    display: grid;
    margin-bottom: 15px;
    place-items: center;
}

#output-color span{
    display: block;
    width: 100%;
    height: 100%;

}

我們使用下面的 CSS 添加一種動畫,只要在顯示器中可以看到這種顏色,就會出現(xiàn)一種動畫。

.show-color{
    animation: pop 0.8s;
}
@keyframes pop{
    0%{
        transform: scale(0);
    }
    100%{
        transform: scale(1);
    }
}

4.創(chuàng)建一個框以查看顏色代碼

現(xiàn)在我們創(chuàng)建一個小盒子,可以看到這個生成顏色的代碼。

 <input type="text" id="output" readonly>
input[type="text"]{
    width: 100%;
    background-color: transparent;

    box-shadow: 0 0 20px rgba(0,139,253,0.65);
    font-size: 1.3em;
    padding: 0.3em 0;

    margin: 1em 0;
    border-radius: 5px;
    color: #000000;
    text-align: center;
}
input[type="text"]::-moz-selection{
    background: transparent;
}
input[type="text"]::selection{
    background: transparent;
}

5.創(chuàng)建生成器和復(fù)制按鈕

現(xiàn)在創(chuàng)建兩個按鈕來生成顏色和復(fù)制顏色,按鈕的寬度為 120 像素,高度取決于填充。

 <div class="btns">
     <button id="gen-btn">Generate</button>
     <button id="copy-btn">Copy</button>
 </div>
.btns{
    display: flex;
    margin-top: 15px;
    justify-content: space-around;
}
.btns button{
    font-size: 1.03em;
    padding: 0.8em 1.7em;
    border-radius: 7px;
    width: 120px;
    font-weight: 600;
    cursor: pointer;
}

以下 CSS 代碼有助于為兩個按鈕添加不同的背景顏色。第一個按鈕外殼添加了藍色,第二種添加了紅色,你也可以根據(jù)自己的喜好更改背景顏色。

#gen-btn{
    background-color: #205e94;
    color: #ffffff;
}
#copy-btn{

    background-color: #d23332;
    color: #ffffff;
}

6.使用 JavaScript 激活隨機顏色生成器

上面我們已經(jīng)設(shè)計了這個項目的基礎(chǔ)結(jié)構(gòu),現(xiàn)在是使用 JavaScript 實現(xiàn)它的時候了。

首先我一一設(shè)置了兩個按鈕的顏色顯示、顏色代碼和ID功能。

let outputColor = document.querySelector("#output-color span");
let output = document.getElementById("output");
let genBtn = document.getElementById("gen-btn");
let copyBtn = document.getElementById("copy-btn");

然后我使用了 HexString。它是一個二進制值,相互結(jié)合形成顏色。接著我們把所有的顏色字符加在一起,之后我們將通過使用 JavaScript 隨機添加來創(chuàng)建漂亮的顏色。

let hexString = "0123456789abcdef";

現(xiàn)在我們已經(jīng)完成了生成顏色的工作,數(shù)學(xué)隨機有助于創(chuàng)建隨機顏色,這是非常簡單的 JavaScript。如果你了解了基本的 JavaScript 就可以輕松地理解它。

let genHexCode = () => {
    let hexCode = "#";
    for( i = 0; i < 6; i++){
        hexCode += hexString[Math.floor(Math.random() * hexString.length)];
    }
    output.value = hexCode;
    outputColor.classList.remove("show-color");
    setTimeout( () => {
        outputColor.classList.add("show-color");
    },10);
    outputColor.style.backgroundColor = hexCode;
}

現(xiàn)在我已經(jīng)激活了復(fù)制按鈕。此按鈕將幫助我們復(fù)制將在上面創(chuàng)建的顏色代碼。

copyBtn.addEventListener("click", () => {
    output.select();
    document.execCommand("copy");

})

現(xiàn)在我們也已經(jīng)激活了生成按鈕,創(chuàng)建一個系統(tǒng)來生成顏色,現(xiàn)在我們指示實施該 genHexCode 系統(tǒng)。只要單擊“生成”按鈕,該系統(tǒng)就會工作,這將創(chuàng)建顏色并且可以在顯示器中看到。

window.onload = genHexCode;
genBtn.addEventListener("click", genHexCode);

到這里我們的隨機顏色生成器就完成了,是不是也沒那么復(fù)雜,還是比較簡單的一個小項目。

完整源碼下載

GitHub 地址

以上就是使用JavaScript實現(xiàn)隨機顏色生成器的詳細內(nèi)容,更多關(guān)于JavaScript隨機顏色生成器的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論