1

我正在尝试使用 CSS 设置多个复选框的样式,但只有一个有效。我的代码有什么问题?我想为复选框设置唯一的 id,但这里的 CSS 似乎不允许这样做。

演示:http: //jsfiddle.net/JW3qf/65/

html:

<div class="slideparam">
        <input type="checkbox" id="slideparam" value="1">
        <label for="slideparam"></label>
        </div>

    <div class="slideparam">
        <input type="checkbox" id="slideparam" value="1">
        <label for="slideparam"></label>
        </div>

CSS:

input[type=checkbox] {
    visibility: hidden;
}



/* SLIDE THREE */
.slideparam {
    width: 80px;
    height: 26px;
    background: #333;
    margin: 20px auto;

    -webkit-border-radius: 50px;
    -moz-border-radius: 50px;
    border-radius: 50px;
    position: relative;

    -webkit-box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
    -moz-box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
    box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
}

.slideparam:after {
    content: 'OFF';
    font: 12px/26px Arial, sans-serif;
    color: #000;
    position: absolute;
    right: 10px;
    z-index: 0;
    font-weight: bold;
    text-shadow: 1px 1px 0px rgba(255,255,255,.15);
}

.slideparam:before {
    content: 'ON';
    font: 12px/26px Arial, sans-serif;
    color: #00bf00;
    position: absolute;
    left: 10px;
    z-index: 0;
    font-weight: bold;
}

.slideparam label {
    display: block;
    width: 34px;
    height: 20px;

    -webkit-border-radius: 50px;
    -moz-border-radius: 50px;
    border-radius: 50px;

    -webkit-transition: all .4s ease;
    -moz-transition: all .4s ease;
    -o-transition: all .4s ease;
    -ms-transition: all .4s ease;
    transition: all .4s ease;
    cursor: pointer;
    position: absolute;
    top: 3px;
    left: 3px;
    z-index: 1;

    -webkit-box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.3);
    -moz-box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.3);
    box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.3);
    background: #fcfff4;

    background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
    background: -moz-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
    background: -o-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
    background: -ms-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
    background: linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfff4', endColorstr='#b3bead',GradientType=0 );
}

.slideparam input[type=checkbox]:checked + label {
    left: 43px;
}
4

3 回答 3

2

ID必须是唯一的,这意味着您只能在页面上使用特定 ID 一次。另一方面,类可以在一个页面上多次使用。

您需要使用 ID 以便labels正确工作,但要使每个 ID 不同以保持其唯一性。我建议这样的结构:

input[type=checkbox] {
  visibility: hidden;
}

.slideparam {
  position: relative;
  width: 80px;
  height: 26px;
  background: #333;
  margin: 20px auto;
  border-radius: 50px;
  box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
}

.slideparam:after {
  content: 'OFF';
  position: absolute;
  right: 10px;
  z-index: 0;
  font: 12px/26px Arial, sans-serif;
  color: #000;
  font-weight: bold;
  text-shadow: 1px 1px 0px rgba(255, 255, 255, .15);
}

.slideparam:before {
  content: 'ON';
  position: absolute;
  left: 10px;
  z-index: 0;
  font: 12px/26px Arial, sans-serif;
  color: #00bf00;
  font-weight: bold;
}

.slideparam label {
  display: block;
  position: absolute;
  top: 3px;
  left: 3px;
  width: 34px;
  height: 20px;
  border-radius: 50px;
  transition: all .4s ease;
  cursor: pointer;
  z-index: 1;
  box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  background: #fcfff4;
  background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -moz-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -o-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -ms-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fcfff4', endColorstr='#b3bead', GradientType=0);
}

.slideparam input[type=checkbox]:checked+label {
  left: 43px;
}
<div class="slideparam">
  <input type="checkbox" id="slideparam_1" value="1" />
  <label for="slideparam_1"></label>
</div>
<div class="slideparam">
  <input type="checkbox" id="slideparam_2" value="2" />
  <label for="slideparam_2"></label>
</div>
<div class="slideparam">
  <input type="checkbox" id="slideparam_3" value="3" />
  <label for="slideparam_3"></label>
</div>

于 2013-07-24T20:28:45.957 回答
1
<input type="checkbox" id="slideparam" value="1">
<input type="checkbox" id="slideparam" value="1">

id 必须是唯一的

我建议您使用w3c validator验证您的 html 。你会注意到这个错误。

只需将您的 ID 替换为唯一值并以另一种方式应用您的样式。

于 2013-07-24T20:28:34.307 回答
0

问题是您的 css 是样式类,但您的 css 中没有任何类,而是有类的 ids。

将您的 html 中的 id 更改为 classes,它将起作用。

.slideparam

代表类

而在你的 html 中只有id="slideparam"

于 2013-07-24T20:30:58.967 回答