使用 CSS 模拟鼠标点击交互
通过 CSS 方式模拟鼠标点击交互动效的两个核心要素:
- HTML 元素需要使用
<input>
和<label>
元素 - CSS 使用
:checked
伪类选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS实现点击交互</title>
<style>
input {
display: none;
}
input:checked + label {
background-color: yellow ;
}
</style>
</head>
<body>
<input type="radio" id="1">
<label for="1">点我</label>
<!-- 当用户点击的时候,添加黄色的背景色 -->
</body>
</html>