【问题标题】:Increase bootstrap custom-control custom-switch size only with css仅使用 css 增加引导自定义控件自定义开关大小
【发布时间】:2021-06-10 05:11:32
【问题描述】:
您好,我正在尝试仅使用 css 增加自定义控件自定义开关元素的大小,但它不起作用。
我的自定义开关现在是这个例子中的默认大小,但我需要它的大小更大:
https://getbootstrap.com/docs/4.2/components/forms/
我们没有在我们的项目中使用 scss,我为此找到的每个解决方案都是一个 scss 解决方案,这就是为什么我正在寻找一个纯原版 css 解决方案。
我的自定义开关代码与此类似:
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1">Toggle this switch element</label>
</div>
【问题讨论】:
标签:
css
twitter-bootstrap
【解决方案1】:
要简单地改变大小,我认为可以在 custom-control-input 类上进行缩放。
.custom-control-input {
transform: scale(1.4);
}
【解决方案2】:
改变大小的简单想法
.custom-control-label::before ,.custom-control-label::after{width:20px; height:20px}
一个细节
.custom-control-label { // added for alignment with the switch
padding-top: 0.5rem;
padding-left: 2rem;
padding-bottom: 0.1rem;
}
.custom-switch .custom-control-label::before {
left: -2.25rem;
height: 2rem;
width: 3.5rem; // it was 1.75rem before. Sliding way is longer than before.
pointer-events: all;
border-radius: 1rem;
}
.custom-switch .custom-control-label::after {
top: calc(0.25rem + 2px);
left: calc(-2.25rem + 2px);
width: calc(2rem - 4px); // it was calc(1rem - 4px) before. Oval is bigger than before.
height: calc(2rem - 4px); // it was calc(1rem - 4px) before. Oval is bigger than before.
background-color: #adb5bd;
border-radius: 2rem; // it was 0.5rem before. Oval is bigger than before.
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-transform 0.15s ease-in-out;
transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-transform 0.15s ease-in-out;
}
@media (prefers-reduced-motion: reduce) {
.custom-switch .custom-control-label::after {
transition: none;
}
}
.custom-switch .custom-control-input:checked ~ .custom-control-label::after {
background-color: #fff;
-webkit-transform: translateX(1.5rem); //translateX(0.75rem);
transform: translateX(1.5rem); //translateX(0.75rem);
}
【解决方案3】:
接受的解决方案是过度杀伤,而第二个解决方案为我停止了 bootstrap v4.6 的动画。
这是我的 2 倍比例解决方案:
/* Positioning the enlarged switch */
.custom-switch > .custom-control-label {
padding-left: 1.75rem;
padding-bottom: 0.5rem;
}
/* Sync the animation for switch knob */
.custom-switch > .custom-control-input:checked ~ .custom-control-label::after {
-webkit-transform: scale(2) translateX(0.75rem);
transform: scale(2) translateX(0.75rem);
}
/* Enlarge and position the switch knob */
.custom-switch > .custom-control-label::after {
left: -1.75rem;
transform: scale(2);
}
/* Enlarge and position the switch socket */
.custom-switch > .custom-control-label::before {
left: -1.50rem;
transform: scale(2);
}