簡體   English   中英

具有自定義圖像的單選按鈕

[英]Radio button with custom image

我已經將此示例用於復選框,但后來我意識到我需要一個用於單選按鈕。

有人可以為單選按鈕自定義此示例嗎?

將復選框檢查圖像更改為自定義圖像

HTML

<input type="radio" class="input_class_checkbox" name="role"> Coach
<input type="radio" class="input_class_checkbox" name="role"> Athlete

jQuery的

$('.input_class_checkbox').each(function(){
    $(this).hide().after('<div class="class_checkbox" />');

});

$('.class_checkbox').on('click',function(){
    $(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))
});

小提琴:

http://jsfiddle.net/cn6kn/

還是有一種一站式解決方案,可將自定義圖像用於單選按鈕和復選框。 我搜索了一些,但所有人都提供了自己的皮膚。

在示例中,我自定義為如下包含背景圖像,但不適用於單選按鈕,所有單選按鈕均保持選中狀態,無論我單擊哪個按鈕。

.class_checkbox {
    width: 36px;
    height: 36px;
    background: url("../images/checkbox.png");
}
.class_checkbox.checked {
    background: url("../images/checkbox-checked.png");
}

在這種情況下使用偽元素 ,我正在使用:: before(:before)

在此處輸入圖片說明

更新: 由於firefox目前尚不支持輸入的偽元素,因此請使用相鄰的兄弟選擇器

在此處輸入圖片說明

 :root{padding: 40px} [name=radio]{ display: none } [for^=radio]{ position: relative; margin: 64px } [for^=radio]:before{ content: ''; position: absolute; left: -15px; top: -15px; width: 32px; height: 32px; background: red } [type=radio]:checked + [for^=radio]:before{ background: green } 
 <input id=radio-1 type=radio name=radio /> <label for=radio-1></label> <input id=radio-2 type=radio name=radio /> <label for=radio-2></label> <input id=radio-3 type=radio name=radio /> <label for=radio-3></label> 

通用兄弟選擇器

在此處輸入圖片說明

 :root{padding: 40px} [name=radio]{ display: none } [for^=radio]{ position: relative; margin: 64px } [for^=radio]:before{ content: ''; position: absolute; left: -15px; top: -15px; width: 32px; height: 32px; background: red } [id=radio-1]:checked ~ [for=radio-1]:before, [id=radio-2]:checked ~ [for=radio-2]:before, [id=radio-3]:checked ~ [for=radio-3]:before{ background: green } 
 <input id=radio-1 type=radio name=radio /> <input id=radio-2 type=radio name=radio /> <input id=radio-3 type=radio name=radio /> <label for=radio-1></label> <label for=radio-2></label> <label for=radio-3></label> 

基礎的

 [type=radio]{ position: relative; margin: 40px } [type=radio]:before{ content: ''; position: absolute; left: -15px; top: -15px; width: 32px; height: 32px; background: red } [type=radio]:checked:before{ background: green } 
 <input type=radio /> 

多個輸入

 [type=radio]{ position: relative; margin: 40px } [type=radio]:before{ content: ''; position: absolute; left: -15px; top: -15px; width: 32px; height: 32px; background: red } [type=radio]:checked:before{ background: green } 
 <input type=radio name=radio /> <input type=radio name=radio /> <input type=radio name=radio /> 

哦,對不起,我第一次誤會了,這樣的事情怎么樣? http://jsfiddle.net/swm53ran/126/

幾乎發生的事情是,舊的單選按鈕css被隱藏了,然后您的css將接管,但仍保持單選按鈕的功能。

label {  
    display: inline-block;  
    cursor: pointer;  
    position: relative;  
    padding-left: 25px;  
    margin-right: 15px;  
    font-size: 13px;  
}  

input[type=radio] {  
    display: none;  
} 

label:before {  
    content: "";  
    display: inline-block;  

    width: 16px;  
    height: 16px;  

    margin-right: 10px;  
    position: absolute;  
    left: 0;  
    bottombottom: 1px;  
    background-color: #aaa;  
    box-shadow: inset 0px 2px 3px 0px rgba(0, 0, 0, .3), 0px 1px 0px 0px rgba(255, 255, 255, .8);  
}  
.radio label:before {  
    border-radius: 8px;  
}  

input[type=radio]:checked + label:before {  
    content: "\2022";  
    color: red;  
    font-size: 30px;  
    text-align: center;  
    line-height: 18px;  
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM