亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

css3實現radio選擇效果

標簽:
Html/CSS CSS3

该手记通过简单教程,实现如图所示的radio单选样式效果
图

html结构,div中包含一个radio单选框与一个指向该单选框的label标签

<div class='radio-check'>
    <input type='radio' name='test' id='test1'/>
    <label for='test1' class>苹果</label>
</div>

初始化相关结构样式,设置div定位为relative,radio与label为absolute定位

.radio-check {
    position: relative;
    height: 35px;
}
.radio-check > input {
    position: absolute;
    left: 0;
    top: 0;
    width: 20px;
    height:20px;
    opacity: 0;
}
.radio-check > label {
    position: absolute;
    left: 30px;
    line-height: 20px;
    top: 0px;
}

通过伪类::before与::after,实现选中效果

初始化before与after状态

.radio-check > label:before {
    content: '';
    position: absolute;
    left: -30px;
    top: 0px;
    display: inline-block;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 1px solid #ddd;
    transition: all 0.3s ease;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
}
.radio-check > label:after {
    content: '';
    position: absolute;
    left: -30px;
    top: 0px;
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-top: 6px;
    margin-left: 6px;
    transition: all 0.3s ease;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
}

利用radio选中状态的:checked伪类选择器与兄弟选择器,实现选中效果

.radio-check input[type='radio']:checked + label:before {
    border-color: #4cd764;
}
.radio-check input[type='radio']:checked + label:after {
    background: #4cd764;
}

整体代码示例

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Examples</title>
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport"/>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link href="" rel="stylesheet">
</head>
<style>
.radio-check {
    position: relative;
    height: 35px;
}
.radio-check > input {
    position: absolute;
    left: 0;
    top: 0;
    width: 20px;
    height:20px;
    opacity: 0;
}
.radio-check > label {
    position: absolute;
    left: 30px;
    line-height: 20px;
    top: 0px;
}
.radio-check > label:before {
    content: '';
    position: absolute;
    left: -30px;
    top: 0px;
    display: inline-block;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 1px solid #ddd;
    transition: all 0.3s ease;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
}
.radio-check > label:after {
    content: '';
    position: absolute;
    left: -30px;
    top: 0px;
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-top: 6px;
    margin-left: 6px;
    transition: all 0.3s ease;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
}
.radio-check input[type='radio']:checked + label:before {
    border-color: #4cd764;
}
.radio-check input[type='radio']:checked + label:after {
    background: #4cd764;
}

</style>
<body>
<div class='radio-check'>
    <input type='radio' name='test' id='test1'/>
    <label for='test1' class>苹果</label>
</div>
<div class='radio-check'>
    <input type='radio' name='test' id='test2'/>
    <label for='test2' class>橘子</label>
</div>
<div class='radio-check'>
    <input type='radio' name='test' id='test3'/>
    <label for='test3' class>梨</label>
</div>
<div class='radio-check'>
    <input type='radio' name='test' id='test4'/>
    <label for='test4' class>香蕉</label>
</div>
<div class='radio-check'>
    <input type='radio' name='test' id='test5'/>
    <label for='test5' class>菠萝</label>
</div>
</body>
</html>

其他相关手记:
通过css3实现开关选择按钮
通过css3实现checkbox选择样式

點擊查看更多內容
12人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
PHP開發工程師
手記
粉絲
8
獲贊與收藏
282

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消