简体   繁体   English

如何连接两个选项和切换开关按钮

[英]How to connect two options and toggle switch buttons

选项

I would like to connect two labels and toggle switches.我想连接两个标签和拨动开关。 If you click the toggle switch, it is desired that the switch is activated when you click the respective labels.如果单击切换开关,则希望在单击相应标签时激活该开关。 However, if you click on the same label, you do not want the switch to move.但是,如果您单击相同的 label,您不希望开关移动。

I made a toggle switch along this link https://www.w3schools.com/howto .我沿着这个链接https://www.w3schools.com/howto做了一个拨动开关。 This button works very well.这个按钮效果很好。 But I don't know what to do to achieve the effect I want.I tried putting two options in the label, but the layout is broken.但是不知道怎么做才能达到我想要的效果。我试过在label里面放两个选项,但是布局坏了。

I can't use other frameworks such as jQuery.我不能使用其他框架,例如 jQuery。 Pure JavaScript support is available.提供纯 JavaScript 支持。

 /* The switch - the box around the slider */.switch { position: relative; display: inline-block; width: 60px; height: 34px; } /* Hide default HTML checkbox */.switch input { opacity: 0; width: 0; height: 0; } /* 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%; }
 <label class="switch"> <input type="checkbox"> <span class="slider round"></span> </label>

This would be simple to solve with JavaScript, but it's a more interesting question with pure CSS/HTML.使用 JavaScript 可以轻松解决这个问题,但对于纯 CSS/HTML,这是一个更有趣的问题。

Explanation: pointer-events: none disables clicks from having any effect.说明: pointer-events: none使点击无效。 pointer-events: all is used selectively on child elements to re-enable clicking on them, depending on the checked state of the input . pointer-events: all选择性地用于子元素以重新启用单击它们,具体取决于inputchecked state 。

 /* the interesting bit */.label { pointer-events: none; display: flex; align-items: center; }.switch, .input:checked +.label.left, .input:not(:checked) +.label.right { pointer-events: all; cursor: pointer; } /* most of the stuff below is the same as the W3Schools stuff, but modified a bit to reflect changed HTML structure */.input { display: none; }.switch { position: relative; display: inline-block; width: 60px; height: 34px; }.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: 0.4s; transition: 0.4s; }.slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: 0.4s; transition: 0.4s; } input:checked +.label.slider { background-color: #2196f3; } input:focus +.label.slider { box-shadow: 0 0 1px #2196f3; } input:checked +.label.slider:before { -webkit-transform: translateX(26px); -ms-transform: translateX(26px); transform: translateX(26px); }.slider.round { border-radius: 34px; }.slider.round:before { border-radius: 50%; } /* styling to make it look like your screenshot */.left, .right { margin: 0.5em; font-weight: bold; text-transform: uppercase; font-family: sans-serif; }
 <input class="input" id="toggle" type="checkbox"> <label class="label" for="toggle"> <div class="left"> Option A </div> <div class="switch"> <span class="slider round"></span> </div> <div class="right"> Option B </div> </label>

This will help you perfectly using two radio button.这将帮助您完美地使用两个单选按钮。

HTML:- HTML:-

 <div class="normal-container">
  <div class="smile-rating-container">
    <div class="smile-rating-toggle-container">
      <form class="submit-rating">
        <input id="meh"  name="satisfaction" type="radio" />
        <input id="fun" name="satisfaction" type="radio" />
        <label for="meh" class="rating-label rating-label-meh">Bad</label>
        <div class="smile-rating-toggle"></div>
        <div class="toggle-rating-pill"></div>
        <label for="fun" class="rating-label rating-label-fun">Fun</label>
      </form>
    </div>
  </div>
</div>

CSS:- CSS:-

    .smile-rating-container {
    position: relative;
    height: 10%;
    min-width: 220px;
    max-width: 520px;
    margin: auto;
    font-family: 'Roboto', sans-serif;
    top: 20%;
}
.submit-rating {
    display: flex;
    align-items: center;
    justify-content: center;
}
.rating-label {
    position: relative;
    font-size: 1.6em;
    text-align: center;
    flex: 0.34;
    z-index: 3;
    font-weight: bold;
    cursor: pointer;
    color: grey;
    transition: 500ms;
}
.rating-label:hover, .rating-label:active {
    color: grey;
}
.rating-label-fun {
    left: -58px;
    text-align: right;
}
.rating-label-meh {
    left: 58px;
    text-align: left;
    color: #222;
}
.smile-rating-container input {
    display: none;
}
.toggle-rating-pill {
    position: relative;
    height: 65px;
    width: 165px;
    background: grey;
    border-radius: 500px;
    transition: all 500ms;
}
.smile-rating-toggle {
    position: absolute;
    width: 54px;
    height: 54px;
    background-color: white;
    left: 182px;
    border-radius: 500px;
    transition: all 500ms;
    z-index: 4;
}
/*
Toggle Changes
*/

#meh:checked~.rating-label-meh {
 color: #2196f3;
}
#fun:checked~.rating-label-meh {
 color: grey;
}
#fun:checked~.mouth {
 border: 4px solid #2196f3;
 border-bottom-color: rgba(1, 1, 1, 0);
 border-right-color: rgba(1, 1, 1, 0);
 border-left-color: rgba(1, 1, 1, 0);
 top: 23px;
 left: 291px;
 transform: rotateX(180deg);
 border-radius: 100%;
}
 #fun:checked~.rating-label-fun {
 color: #2196f3;
}
#fun:checked~.smile-rating-toggle {
 left: 282px;
}
 #fun:checked~.rating-eye-left {
 left: 292px;
}
#fun:checked~.rating-eye-right {
 left: 316px;
}
 #fun:checked~.toggle-rating-pill {
 background-color: #2196f3;
}
 #fun:checked~.rating-eye {
 background-color: #2196f3;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM