仅使用 css 增加引导自定义控制自定义开关大小



您好,我正在尝试仅使用 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>

要简单地更改大小,我认为可以在custom-control-input类上缩放。

.custom-control-input {
transform: scale(1.4);
}

更改大小的简单想法

.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);
}

接受的解决方案是矫枉过正,而第二个为我停止了引导 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);
}

最新更新