首頁  >  文章  >  web前端  >  css3模擬jq點擊事件

css3模擬jq點擊事件

高洛峰
高洛峰原創
2018-05-15 17:00:541552瀏覽

今天是css3模擬jq點擊事件,因為我發現,css3中沒有類似,js的點擊事件,那麼,可不可以仿照

jq的效果,類似的做一個呢?主要用到,input裡面的radio 單選按鈕,然後後面跟一個a標籤,讓radio覆蓋在a上,那為什麼不直接

把 a放在radio上面呢?因為選擇器 + 好選擇嘛,用radio的功能,a來修飾按鈕樣式,再把radio 隱藏,這裡要用opacity(透明度)

這就是,主要原理!

上視圖吧

css3模拟jq点击事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
	    <title></title>
	    <style>
	    	*{
	    		margin: 0;
	    		padding: 0;
	    		list-style: none;	
	    		text-decoration: none;    	
	    	}
	    	.fd{
	    		width: 100%;
	    		height: 100px;
	    		margin-top: 200px;
	    		position: fixed;
	    	}
	    	input,a{
	    		width: 33.33%;
	    		height: 100px;
	    		position: absolute;
	    		font-size: 30px;
	    		font-weight: 700;
	    		cursor:pointer;
	    	}
	    	a{
	    		display: block;
	    		color: #000;
	    		text-align: center;
	    		line-height: 100px;
	    		z-index: 10;/*要覆盖嘛,当然需要层级关系*/
	    		border-radius: 20px;
	    		
	    	}
	    	input{
	    		z-index: 100;/*要覆盖嘛,当然需要层级关系*/
	    		opacity:0;
	    	}
	    	input:checked + a{
	    		background: rgba(0,0,0,0.5);
	    	}
	    	#a1,#a1+a{
	    		left: 0%;
	    	}
	    	#a2,#a2+a{
	    		left: 33.33%;
	    	}
	    	#a3,#a3+a{
	    		left: 66.66%;
	    	}
	    </style>
	</head>
	<body>
		<!--单选按钮的时候,name要统一
			原理就是,把input覆盖在a上,然后再把input透明度为0隐藏;
			然后,按钮的样式由a标签来控制。input上,a下,是因为;
			选择器 + 容易选到。
		-->
		<p class="fd">
			<input type="radio" name="单选" id="a1" />
			<a href="#">css</a>
			<input type="radio" name="单选" id="a2"  />
			<a href="#">html</a>
			<input type="radio" name="单选" id="a3" />
			<a href="#">javascript</a>
		</p>
	</body>
</html>

更多css3模擬jq點擊事件 相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn