Css實現checkbox及radio樣式自定義


前言

checkbox和radio樣式自定義在網頁中是很常見的, 比如在進行表單輸入時性別的選擇,用戶注冊時選擇已閱讀用戶協議。隨着用戶對產品體驗要求越來越高,我們都會對checkbox和radio重新設計,checkbox默認的樣式非常丑 ,無法直接修改checkbox和radio的樣式,這里我們借助label標簽來對它進行樣式美化。

先看實現效果圖,如下:

實現思路

1.設置input 屬性hidden對該input進行隱藏,或者通過display:none也可以

<input type="radio" name="type" id="adviceRadio1" value="1" checked hidden/>

2.借助label for標簽通過id綁定input ,這樣在點擊label時實際就是點擊了input

<input type="radio" name="type" id="adviceRadio1" value="1" checked hidden/>
			    <label for="adviceRadio1" class="advice"></label>

3.定義label的樣式,設置未選中狀態的背景圖

.advice{
					height: 12px;
			        width: 12px;
				    display: inline-block;
				    background-image: url('https://caiyunupload.b0.upaiyun.com/newweb/imgs/icon-unchecked.png');
			        background-repeat: no-repeat;
			        background-position: center;
			        vertical-align: middle;
			    	margin-top: -4px;
				}

4.使用相鄰選擇器設置選中狀態label的樣式

input[type="radio"]:checked + .advice{
				    background-image: url('https://caiyunupload.b0.upaiyun.com/newweb/imgs/icon-checked.png');
				}

實現代碼

請選擇反饋的問題:
			<label>
		    	<input type="radio" name="type" id="adviceRadio1" value="1" checked hidden/>
			    <label for="adviceRadio1" class="advice"></label>
			    <span class="radio-name">問題</span>
		    </label>
		    <label>
		    	<input type="radio" name="type" id="adviceRadio2" value="2" hidden/>
			    <label for="adviceRadio2" class="advice"></label>
			    <span class="radio-name">建議</span>
		    </label>
		    <span id="result">1</span>
		    <style type="text/css">
		    	.advice{
					height: 12px;
			        width: 12px;
				    display: inline-block;
				    background-image: url('https://caiyunupload.b0.upaiyun.com/newweb/imgs/icon-unchecked.png');
			        background-repeat: no-repeat;
			        background-position: center;
			        vertical-align: middle;
			    	margin-top: -4px;
				}
				input[type="radio"]:checked + .advice{
				    background-image: url('https://caiyunupload.b0.upaiyun.com/newweb/imgs/icon-checked.png');
				}
		    </style>

以上是radio單選框的實現代碼,checkbox也是類似 將input type定義成checkbox即可

獲取radio及checkbox選中的值

1.獲取radio的值
使用jquery獲取radio的值有3種方式:

$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val();

2.獲取checkbox的值

 var obj = document.getElementsByName("hobby");
		         var check_val = [];
		         for(k in obj){
		         	if(obj[k].checked){
		         		check_val.push(obj[k].value);
		         	}
		         }

遇到的坑

一開始寫的時候,我是使用偽元素的方式實現,先將input進行隱藏 ,然后設置input:after定義它的樣式,代碼如下:

//html
<input type="radio" name="sex" id="male" /><label for="male"> Male</label>

//css
input[type=radio]{
            visibility: hidden;
        }
        input[type=radio]:checked::after{
            background-image: url('./img/sprite.png');
            background-repeat: no-repeat;
            background-position: -59px -10px;
            visibility: visible;
        }
        input[type=radio]::after{
            content: ' ';
            display: block;
            height: 20px;
            width: 20px;
            background-image: url('./img/sprite.png');
            background-repeat: no-repeat;
            background-position: -24px -10px;
            visibility: visible;
        }

但是后來發現這種方式兼容性有問題,在firefox瀏覽器無法顯示,經查資料是因為input不支持偽元素:after,:before 。
火狐瀏覽器無法插入內容DOM元素,偽元素都是在容器內進行渲染的。input無法容納其他元素,因此它不支持偽元素。
input,img,iframe等元素都不能包含其他元素,所以不能通過偽元素插入內容。至於Chrome 中checkbox和radio可以插入應該就是bug了
input要配合其它容器元素(i,span)等實現預期效果

戳我在線查看demo

完整的代碼我已經上傳到了https://github.com/fozero/frontcode,可以點擊查看,如果覺得還不錯的話,記得star一下哦!

相關資料

https://www.cnblogs.com/u-drive/p/7888155.html
https://fatesinger.com/74438

作者:fozero
聲明:原創文章,轉載請注明出處,謝謝!http://www.cnblogs.com/fozero/p/8902116.html
標簽:radio,checkbox美化


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM