首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CSS来切换一个基于另一个开关的开关?

如何使用CSS来切换一个基于另一个开关的开关?
EN

Stack Overflow用户
提问于 2020-11-25 13:42:11
回答 1查看 116关注 0票数 0

这是我一直在尝试的

当我点击‘Switk-1’和反之亦然的时候,javascript函数myFunc()中应该使用什么来获得“Switch-2”以使灰色背景变成蓝色。我对CSS或jQuery相当陌生。我认为解决办法应该是使用其中之一或两者兼而有之。如果有人能够共享代码来实现这一点,将会有很大的帮助。提前谢谢。

代码语言:javascript
复制
function myFunc() {
  var checkBox1 = document.getElementById("SW1");
  var checkBox2 = document.getElementById("SW2");
  
  if (checkBox1.checked == true){
    alert("SW2:"+checkBox2.checked);
    //How to Enable SW2 with blue background
  } else {
     alert("SW2:"+checkBox2.checked);
     //How to Disable SW2 with gray background
  }
}
代码语言:javascript
复制
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.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%;
}
代码语言:javascript
复制
<html>
<body>

<h2>Toggle Switch</h2>

<label>Switch 1: </label>
<label class="switch">
  <input type="checkbox" id="SW1" onclick="myFunc()">
  <span class="slider"></span>
</label><br><br>

<label>Switch 2: </label>
<label class="switch">
  <input type="checkbox" id="SW2">
  <span class="slider round"></span>
</label>

</body>
</html>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-25 14:10:07

这就是你想做的吗?试着运行片段。

代码语言:javascript
复制
function switchClicked(currentSwitchState, switchToToggle) {
  var switchElement = document.getElementById(switchToToggle);
  switchElement.checked = currentSwitchState.checked;  
}
代码语言:javascript
复制
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.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%;
}
代码语言:javascript
复制
<html>
<body>

<h2>Toggle Switch</h2>

<label>Switch 1: </label>
<label class="switch">
  <input id="s1" type="checkbox" onclick="switchClicked(this, 's2');">
  <span class="slider"></span>
</label><br><br>

<label>Switch 2: </label>
<label class="switch">
  <input id="s2" type="checkbox" onclick="switchClicked(this, 's1');">
  <span class="slider round"></span>
</label>

</body>
</html>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65005896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档