我从youtube视频中获得了此代码,但是它不起作用。因此,我将其放入jsfiddle并修复了所有它说错的地方,但是EEERRRRRR...。它仍然无法正常工作。应该有四个滑动开/关开关。但是,我只有一个巨大的Blob,除了坐在那里嘲笑我之外,什么也不做。有帮助吗?
<html>
<head>
<title>Toggle Switch</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<h1>Toggle Switch</h1>
<lable class="switch">
<input type="checkbox">
<span class="slider"></span>
</lable>
<lable class="switch">
<input type="checkbox" checked>
<span class="slider"></span>
</lable>
<lable class="switch">
<input type="checkbox">
<span class="slider"></span>
</lable>
<lable class="switch">
<input type="checkbox" checked>
<span class="slider"></span>
</lable>
</div>
</body>
</html>
css
* {
margin: 0;
padding: 0;
}
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
h1 {
margin-bottom: 40px;
font-family: sans-serif;
}
.switch {
position: relitive;
display: inline-block;
width: 110px;
height: 60px;
margin: 0 10px;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: 0.4s;
border-radius: 34px;
}
.switch input { display: none; }
.slider::before {
position: absolute;
content: "";
height: 50px;
width: 50px;
left: 5px;
bottom: 5px;
background-color: white;
transition: 0.4s;
border-radius: 50px;
}
input:checked + .slider {
background-color: #ff278c;
}
input:checked + .slider::before {
transform: translateX(50px);
}
答案 0 :(得分:-1)
您有错字,position: relitive;
是错的:
.switch {
position: relative;
display: inline-block;
width: 110px;
height: 60px;
margin: 0 10px;
}
和lable
应该是label
答案 1 :(得分:-1)
这有效。将lable
固定为label
,将relitive
固定为relative
* {
margin: 0;
padding: 0;
}
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
h1 {
margin-bottom: 40px;
font-family: sans-serif;
}
.switch {
position: relative;
display: inline-block;
width: 110px;
height: 60px;
margin: 0 10px;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: 0.4s;
border-radius: 34px;
}
.switch input { display: none; }
.slider::before {
position: absolute;
content: "";
height: 50px;
width: 50px;
left: 5px;
bottom: 5px;
background-color: white;
transition: 0.4s;
border-radius: 50px;
}
input:checked + .slider {
background-color: #ff278c;
}
input:checked + .slider::before {
transform: translateX(50px);
}
<html>
<head>
<title>Toggle Switch</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<h1>Toggle Switch</h1>
<label class="switch">
<input type="checkbox">
<span class="slider"></span>
</label>
<label class="switch">
<input type="checkbox" checked>
<span class="slider"></span>
</label>
<label class="switch">
<input type="checkbox">
<span class="slider"></span>
</label>
<label class="switch">
<input type="checkbox" checked>
<span class="slider"></span>
</label>
</div>
</body>
</html>