【问题标题】:Switch css doesn't work with input type radio切换 css 不适用于输入类型收音机
【发布时间】:2016-02-16 13:59:33
【问题描述】:

我有一个切换按钮。但它不适用于输入类型的收音机。如果我尝试使用复选框按钮,它会起作用。为什么?我该如何解决,保持无线电输入?

@charset "utf-8";
/* CSS Document */

/* ---------- GENERAL ---------- */

body {
	background: #4a4a4a;
	color: #151515;
	font: 100%/1.5em "Lato", sans-serif;
	margin: 0;
}

input {
    font-family: inherit;
    font-size: 100%;
    line-height: normal;
    margin: 0;
}

input[type="radio"] {
    box-sizing: border-box;
    padding: 0;
}

/* ---------- SWITCH ---------- */

.container {
	height: 64px;
	left: 50%;
	margin: -32px 0 0 -80px;
	position: absolute;
	top: 50%;
	width: 160px;
}

.switch {
	background: #fff;
	border-radius: 32px;
	display: block;
	height: 64px;
	position: relative;
	width: 160px;
}

.switch label {
	color: #fff;
	font-size: 48px;
	font-weight: 300;
	line-height: 64px;
	text-transform: uppercase;
	-webkit-transition: color .2s ease;
	-moz-transition: color .2s ease;
	-ms-transition: color .2s ease;
	-o-transition: color .2s ease;
	transition: color .2s ease;
	width: 100px;
}

.switch label:nth-of-type(1) {
	left: -75%;
	position: absolute;
	text-align: right;
}

.switch label:nth-of-type(2) {
	position: absolute;
  right: -75%;
	text-align: left;
}

.switch input {
	height: 64px;
	left: 0;
	opacity: 0;
	position: absolute;
	top: 0;
	width: 160px;
	z-index: 2;
}

.switch input:checked~label:nth-of-type(1) { color: #fff; }
.switch input:checked~label:nth-of-type(2) { color: #808080; }

.switch input~:checked~label:nth-of-type(1) { color: #808080; }
.switch input~:checked~label:nth-of-type(2) { color: #fff; }

.switch input:checked~.toggle {
	left: 4px;
}

.switch input~:checked~.toggle {
	left: 100px;
}

.switch input:checked {
	z-index: 0;
}

.toggle {
	background: #4a4a4a;
	border-radius: 50%;
	height: 56px;
	left: 0;
	position: absolute;
	top: 4px;
	-webkit-transition: left .2s ease;
	-moz-transition: left .2s ease;
	-ms-transition: left .2s ease;
	-o-transition: left .2s ease;
	transition: left .2s ease;
	width: 56px;
	z-index: 1;
}

.c-window{
  display: block;
  position: absolute;
  width: 235px;
  height: 235px;
  margin: 0 auto;
  border-radius: 100%;
  border: 8px solid #FFB399;
  background: #5ddfe8; 
  box-shadow: 0px 0px 5px rgba(0,0,0,0.25) inset;
  overflow: hidden;
	transition: background 1s ease-in-out;
}


input[type="radio"]:checked ~ .c-window {
  background: #111;
}
 
	<div class="container">

       <div class="c-window"></div>
      
		<div class="switch white">

			<input type="radio" name="switch" id="switch-off">
			<input type="radio" name="switch" id="switch-on" checked>

			<label for="switch-off">On</label>
			<label for="switch-on">Off</label>

			<span class="toggle"></span>


  

		</div> <!-- end switch -->

	</div> <!-- end container -->

我希望 .c 窗口在切换时改变背景颜色

【问题讨论】:

  • 您有一些拼写错误,例如.switch input~:checked.switch input~:checked~.toggle。此外,input[type="radio"]:checked ~ .c-window 不起作用,因为它们不是兄弟姐妹
  • 我编辑了 .switch input:checked ~ .c-window { background: #111; }但它还没有工作
  • 但您的 input:checked 和您的 .c-window 仍然不是兄弟姐妹。欲了解更多信息,请参阅developer.mozilla.org/en-US/docs/Web/CSS/…
  • 我把下面的 c-window 放到输入中,所以理论上它们现在是兄弟姐妹。但是,如果我这样做并刷新页面,则圆圈会立即变为黑色背景并且不会更改
  • 如果您对上面的代码进行了一些更改,请将修改后的代码放在 fiddle 中,以便我们可以看到相同的内容

标签: html css input checked


【解决方案1】:
  • @fcalderan 在 cmets 中提到,您有一些拼写错误。
  • 您需要将.c-window 作为同级,更改HTML 标记并将z-index:-1 添加到其中。
  • 您需要指定哪个input 将更改background 颜色。

/* CSS Document */

/* ---------- GENERAL ---------- */

body {
  background: #4a4a4a;
  color: #151515;
  font: 100%/1.5em"Lato", sans-serif;
  margin: 0;
}
input {
  font-family: inherit;
  font-size: 100%;
  line-height: normal;
  margin: 0;
}
input[type="radio"] {
  box-sizing: border-box;
  padding: 0;
}
/* ---------- SWITCH ---------- */

.container {
  height: 64px;
  left: 50%;
  margin: -32px 0 0 -80px;
  position: absolute;
  top: 50%;
  width: 160px;
}
.switch {
  background: #fff;
  border-radius: 32px;
  display: block;
  height: 64px;
  position: relative;
  width: 160px;
}
.switch label {
  color: #fff;
  font-size: 48px;
  font-weight: 300;
  line-height: 64px;
  text-transform: uppercase;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
  width: 100px;
}
.switch label:first-of-type {
  left: -75%;
  position: absolute;
  text-align: right;
}
.switch label:last-of-type {
  position: absolute;
  right: -75%;
  text-align: left;
}
.switch input {
  height: 64px;
  left: 0;
  opacity: 0;
  position: absolute;
  top: 0;
  width: 160px;
  z-index: 2;
}
.switch input:checked~label:first-of-type {
  color: #fff;
}
.switch input:checked~label:last-of-type {
  color: #808080;
}
.switch input:checked~label:first-of-type {
  color: #808080;
}
.switch input:checked~label:last-of-type {
  color: #fff;
}
.switch input:checked {
	z-index: 0;
}
.switch input:checked~.toggle {
  left: 4px;
}
.switch input~:checked~.toggle {
  left: 100px;
}
.toggle {
  background: #4a4a4a;
  border-radius: 50%;
  height: 56px;
  left: 0;
  position: absolute;
  top: 4px;
  -webkit-transition: left .2s ease;
  -moz-transition: left .2s ease;
  -ms-transition: left .2s ease;
  -o-transition: left .2s ease;
  transition: left .2s ease;
  width: 56px;
  z-index: 1;
}
.c-window {
  display: block;
  position: absolute;
  width: 235px;
  height: 235px;
  margin: 0 auto;
  border-radius: 100%;
  border: 8px solid #FFB399;
  background: #5ddfe8;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.25) inset;
  overflow: hidden;
  transition: background 1s ease-in-out;
  z-index:-1
}
input:last-of-type[type="radio"]:checked ~ .c-window {
  background: #111;
}
<div class="container">
 
  <div class="switch white">
    <input type="radio" name="switch" id="switch-off">
    <input type="radio" name="switch" id="switch-on" checked>
    <label for="switch-off">On</label>
    <label for="switch-on">Off</label>
    <span class="toggle"></span>
    <div class="c-window"></div>
  </div>
  <!-- end switch -->
</div>
<!-- end container -->

【讨论】:

    【解决方案2】:

    你不能用 javascript/jQuery 来做吗?

    HTML:

    <div class="container">
    
       <div class="c-window"></div>
    
        <div class="switch white">
    
            <input type="radio" name="switch" value="0" id="switch-off" class="switch">
            <input type="radio" name="switch" value="1" id="switch-on" class="switch" checked>
    
            <label for="switch-off">On</label>
            <label for="switch-on">Off</label>
    
            <span class="toggle"></span>
    
        </div> <!-- end switch -->
    
    </div> <!-- end container -->
    

    jQuery(捕捉更改事件):

        $(document).ready(function(){
    $('.switch').on('change', function(ev){
    if ($(this).val() == 0){
    //do some stuff you want when off
    }else{
    //do some stuff you want when on
    }
    });
    });
    

    【讨论】:

    • OP 既没有提到 javascript 也没有提到 jQuery。
    • @NagyIstvan 看到我的答案
    • 正确使用 :checked 伪类作为 OP 尝试执行的操作(另请参阅之前的答案)
    【解决方案3】:

    如果你想使用~ 选择器,你的代码应该是这样的

    body, html {
      margin: 0;
      padding: 0;
    }
    
    .content {
      display: flex;
      height: 100vh;
      align-items: center;
      justify-content: center;
      position: relative;
    }
    
    .c-window {
      position: absolute;
      transition: all 0.3s ease-in;
      width: 235px;
      height: 235px;
      border-radius: 50%;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      background: #aaa;
      border: 5px solid black;
      z-index: 1;
    }
    
    input {
      position: relative;
      z-index: 2;
    }
    
    #switch-off:checked ~ .c-window  {
      background: blue;
    }
    
    #switch-on:checked ~ .c-window  {
      background: #aaa;
    }
    <div class="content">
      <input type="radio" name="switch" id="switch-off">
      <input type="radio" name="switch" id="switch-on" checked>
      
      <div class="c-window"></div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 2017-07-02
      • 2022-07-01
      • 2017-02-23
      • 2013-10-12
      • 1970-01-01
      相关资源
      最近更新 更多