溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

CSS3中怎么實(shí)現(xiàn)單選框動(dòng)畫特效

發(fā)布時(shí)間:2021-06-25 16:00:26 來(lái)源:億速云 閱讀:163 作者:Leah 欄目:web開發(fā)

本篇文章為大家展示了CSS3中怎么實(shí)現(xiàn)單選框動(dòng)畫特效,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

<div class="radio-1">
    <input type="radio" name="radio-1" id="radio-1-1" checked="checked">
    <label for="radio-1-1"></label>
 
    <input type="radio" name="radio-1" id="radio-1-2">
    <label for="radio-1-2"></label>
 
    <input type="radio" name="radio-1" id="radio-1-3">
    <label for="radio-1-3"></label>
</div>

這里,我們指定 input 標(biāo)簽的 type 值為 radio,并且一下所有的 radio 的 name 值都相同,這樣才可以實(shí)現(xiàn)單選效果。對(duì)于這里的 label 中的 for 屬性,為什么這么設(shè)置一開始我也不明白,后來(lái)搜索了一下這個(gè)屬性的定義,反正大概的意思就是說(shuō),只要設(shè)置了這個(gè)屬性,當(dāng)我們點(diǎn)擊label 元素的時(shí)候,瀏覽器會(huì)自動(dòng)把焦點(diǎn)轉(zhuǎn)移到 radio 上去。下面用 CSS 對(duì)HTML設(shè)置效果。

代碼如下:

   .radio-1 {
        width: 900px;
        padding: 3% 0%;
        margin: 10px auto;
        background-color: darkseagreen;
        text-align: center;
    }
 
    .radio-1 label {
        display: inline-block;
        position: relative;
        width: 28px;
        height: 28px;
        border: 1px solid #cccccc;
        border-radius: 100%;
        cursor: pointer;
        background-color: #ffffff;
        margin-right: 10px;
    }

這里我們首先看一下對(duì) label 元素的設(shè)定,其中大部分屬性我都在以前的文章中介紹過(guò)了,唯一一個(gè)陌生的屬性就是 cursor,這個(gè)屬性是設(shè)定鼠標(biāo)樣式的,設(shè)置成 pointer 之后,當(dāng)我們的鼠標(biāo)放到 label 元素上時(shí),鼠標(biāo)樣式就變成了一只手(在我電腦上是這樣)。好了,下面繼續(xù)來(lái)看

代碼如下:

    .radio-1 label:after {
        content: "";
        position: absolute;
        width: 20px;
        height: 20px;
        top: 4px;
        left: 4px;
        background-color: #666;
        border-radius: 50%;
        transform: scale(0);
        transition: transform .2s ease-out;
    }

這里我們用到了 after 選擇器,為什么設(shè)置這個(gè)屬性?就是為了設(shè)置如上圖所示的小黑點(diǎn)。首先我們?cè)O(shè)置 content 屬性為空,意思就是我們不需要填充任何內(nèi)容,因?yàn)槲覀冎皇窍朐O(shè)置背景色為黑色,僅此而已。還有,剛開始的時(shí)候我們?cè)O(shè)置 transform 的 scale 值為 0 ,其達(dá)到的效果就是將小黑點(diǎn)隱藏。

代碼如下:

    .radio-1 [type="radio"]:checked + label {
        background-color: #eeeeee;
        transition: background-color .2s ease-in;
    }
 
    .radio-1 [type="radio"]:checked + label:after {
        transform: scale(1);
        transition: transform .2s ease-in;
    }

注意這里使用了 + 符號(hào),是什么意思呢?它的學(xué)名叫做 相鄰?fù)x擇器,意思就是選擇緊接在另一個(gè)元素后的元素,而且二者有相同的父元素,在這里的意思就是選中在radio 后出現(xiàn)的 label ,有人要問(wèn)了,這么設(shè)置干嘛,直接設(shè)置 label 就是了。想象一下,在一個(gè) 非常龐大的系統(tǒng)中,我們可能多次使用到 label 元素,為了避免混淆,這樣設(shè)置將更加準(zhǔn)確。這里我們看到了 transition 屬性,這個(gè)屬性是用于設(shè)置過(guò)渡效果的。最后,將我們的 radio 隱藏掉,就大功告成了。

代碼如下:


   .radio-1 [type="radio"]{
       display: none;
   }

這是我們的第二個(gè)特效

CSS3中怎么實(shí)現(xiàn)單選框動(dòng)畫特效

其實(shí)看到這個(gè)動(dòng)畫的第一感覺就是,和上一個(gè)一模一樣,除了將 transform 屬性設(shè)置成 rotate,下面我就不再解釋了,只要你結(jié)合上一個(gè)例子,就可以很容易做出這么一個(gè)效果,我們直接上代碼

代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Radio</title>
    <style>
        .radio-2 {
            width: 900px;
            padding: 3% 0;
            margin: 50px auto;
            background-color: darkseagreen;
            text-align: center;
        }
 
        .radio-2 label {
            display: inline-block;
            width: 28px;
            height: 28px;
            overflow: hidden;
            border: 1px solid #eeeeee;
            border-radius: 100%;
            margin-right: 10px;
            background-color: #ffffff;
            position: relative;
            cursor: pointer;
        }
 
        .radio-2 label:after {
            content: "";
            position: absolute;
            top: 4px;
            left: 4px;
            width: 20px;
            height: 20px;
            background-color: #666666;
            border-radius: 50%;
            transform: rotate(-180deg);
            transform-origin: -2px 50%;
            transition: transform .2s ease-in;
        }
 
        .radio-2 [type="radio"] {
            display: none;
        }
 
        .radio-2 [type="radio"]:checked + label:after{
            transform: rotate(0deg);
            transition: transform .2s ease-out;
        }
    </style>
</head>
<body>
<div class="radio-2">
    <input type="radio" name="radio-2" id="radio-2-1" checked="checked">
    <label for="radio-2-1"></label>
 
    <input type="radio" name="radio-2" id="radio-2-2">
    <label for="radio-2-2"></label>
 
    <input type="radio" name="radio-2" id="radio-2-3">
    <label for="radio-2-3"></label>
</div>
 
</body>
</html>

上述內(nèi)容就是CSS3中怎么實(shí)現(xiàn)單選框動(dòng)畫特效,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI