【问题标题】:Toggle switch css切换css
【发布时间】:2019-01-19 04:18:03
【问题描述】:
我有一个 div,里面有 3 个 div。
左右div是文字,中间div需要拨动开关。
这是代码
<div class="doctors-appointment">
<div class="Row">
<div class="Column">
<b>Doctor's appointment</b>
</div>
<div class="Column">
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
<div class="Column">
<b>For internal purposes</b>
</div>
</div>
</div>
但切换开关不在 div 中
这是屏幕:
我从那里得到样式w3school
这里是 div 的 css
.doctors-appointment {
clear: both;
width: 100%;
margin-top: 100px;
color: #1d69b4;
font-size: 20px;
}
.Row {
display: table;
width: 100%; /*Optional*/
table-layout: fixed; /*Optional*/
border-spacing: 10px; /*Optional*/
}
.Column {
display: table-cell;
background-color: red; /*Optional*/
}
我的问题在哪里?
【问题讨论】:
标签:
javascript
jquery
html
css
【解决方案1】:
添加这个 CSS
.doctors-appointment {
clear: both;
width: 100%;
margin-top: 100px;
color: #1d69b4;
font-size: 20px;
}
.Row {
display: table;
width: 100%; /*Optional*/
table-layout: fixed; /*Optional*/
border-spacing: 10px; /*Optional*/
}
.Column {
display: table-cell;
background-color: red; /*Optional*/
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* Hide default HTML checkbox */
.switch input {display:none;}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<div class="doctors-appointment">
<div class="Row">
<div class="Column">
<b>Doctor's appointment</b>
</div>
<div class="Column">
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
<div class="Column">
<b>For internal purposes</b>
</div>
</div>
</div>
【解决方案2】:
你可以只用你的输入和一些样式,你实际上不需要一个新的跨度。
这里我使用 sass 上传了一个 codepen。
你的输入应该是这样的:
<input class="switch" type="checkbox" />
还有样式:
.switch{
-webkit-appearance: none;
height: 1rem;
width: 3rem;
background-color: gray;
border-radius: 43px;
position: relative;
cursor: pointer;
&::after {
top: 1px;
left: 2px;
content: '';
width: 0.8rem;
height:0.8rem;
background-color:lightgray;
position: absolute;
border-radius: 100%;
transition: 1s;
}
&:checked {
background-color: blue;
&::after {
transform: translateX(2rem);
}
}
&:focus {
outline-color: transparent;
}
}
https://codepen.io/jdtorregrosas/pen/qLzvye