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

純CSS實(shí)現(xiàn)開關(guān)按鈕切換效果簡潔易用

segmentfault   發(fā)布時間:2023-10-11 14:33:58   作者:Winn   我要評論
這篇文章主要為大家介紹了純CSS實(shí)現(xiàn)開關(guān)按鈕切換效果簡潔易用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

效果圖預(yù)覽

完整代碼如下

<!DOCTYPE html>
<html>
<head>
<title>純css編寫開關(guān)按鈕點(diǎn)擊切換</title>
<style type="text/css">
    #toggle-button{ 
        display: none; 
    }
    .button-label{
        position: relative;
        display: inline-block;
        width: 80px;
        background-color: #ccc;
        border: 1px solid #ccc;
        border-radius: 30px;
        cursor: pointer;
    }
    .circle{
        position: absolute;
        top: 0;
        left: 0;
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background-color: #fff;
    }
    .button-label .text {
        line-height: 30px;
        font-size: 18px;
        /*
        用來阻止頁面文字被選中,出現(xiàn)藍(lán)色
        可以將下面兩行代碼注釋掉來查看區(qū)別
        */
       -webkit-user-select: none;
       user-select: none;
    }
    .on { 
        color: #fff; 
        display: none; 
        text-indent: 10px;
    }
    .off { 
        color: #fff; 
        display: inline-block; 
        text-indent: 53px;
    }
    .button-label .circle{
        left: 0;
        transition: all 0.3s;/*transition過度,時間為0.3秒*/
    }
    /*
    以下是checked被選中后,緊跟checked標(biāo)簽后面label的樣式。
    例如:div+p 選擇所有緊接著<div>元素之后的<p>元素
    */
    #toggle-button:checked + label.button-label .circle{
        left: 50px;
    }
    #toggle-button:checked + label.button-label .on{ 
        display: inline-block; 
    }
    #toggle-button:checked + label.button-label .off{ 
        display: none; 
    }
    #toggle-button:checked + label.button-label{
        background-color: #33FF66;
    }
</style>
</head>
<body>
    <input type="checkbox" id="toggle-button">
   <!--label中的for跟input的id綁定。作用是在點(diǎn)擊label時選中input或者取消選中input-->
    <label for="toggle-button" class="button-label">
        <span class="circle"></span>
        <span class="text on">開</span>
        <span class="text off">關(guān)</span>
    </label>
</body>
</html>

知識點(diǎn)

1. label中的for跟input的id綁定。作用是在點(diǎn)擊label時選中input或者取消選中input

2. (:checked + 緊鄰其后面標(biāo)簽) 的選擇器。例如:#toggle-button:checked + label 解釋:當(dāng)id為toggle-button的checked為選中狀態(tài)時,就選擇緊鄰其后的label標(biāo)簽

3. user-select: none;這個屬性用來阻止頁面文字被選中,如果不添加此屬性,點(diǎn)擊切換開關(guān)時,開/關(guān) 二字有時候會被選中,出現(xiàn)藍(lán)色背景,如下圖:

以上就是純css編寫開關(guān)按鈕點(diǎn)擊切換效果實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于css開關(guān)按鈕點(diǎn)擊切換的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論