选中时,在Pure CSS-Checkbox标签中交换文本

时间:2017-03-01 15:48:30

标签: css checkbox accordion display

我有这个我喜欢的Pure-CSS手风琴,但我想添加一个功能:我希望标签根据复选框状态而改变,以便在没有JavaScript的情况下实现这一点。

所以,

  • 如果复选框未选中,我希望问题文字显示为“更多信息”,
  • 如果复选框已选中,问题文字将显示为“少信息”。

我不关心你自己使用什么样的元素..你不必让label元素成为改变的元素。但是,重要的是解决方案继续允许用户通过单击label元素中的文本或类plus内的加号触发切换来打开手风琴。答案还需要没有JavaScript。

一个注意事项:避免将问题文本放入伪元素的解决方案将优先于那些。原因是:我想使用first-line来设置问题文本的第一行样式,并且因为您无法组合伪元素,使用PE来放置问题文本会阻止我使用{{1 }}

我很想知道是否可以做到这一点!谢谢! 这是codepen: http://codepen.io/ihatecoding/pen/oZjVPG

这是片段:

first-line
@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,800);
body {
  font-family: 'Open Sans';
  font-size: 1.5em;
  background: #eee;
}


.question-content {
  width: 80%;
  padding: 20px;
  margin: 0 auto;
  padding: 0 60px 0 0;
}


.question {
  position: relative;
  background: #8FBC8F;
  margin: 0;
  padding: 10px 10px 10px 50px;
  display: block;
  width:100%;
  cursor: pointer;
}

.answers {
  background: #999;
  padding: 0px 15px;
  margin: 5px 0;
  height: 0;
  overflow: hidden;
  z-index: -1;
  position: relative;
  opacity: 0;
  -webkit-transition: .7s ease;
  -moz-transition: .7s ease;
  -o-transition: .7s ease;
  transition: .7s ease;
}

.questions:checked ~ .answers{
  height: auto;
  opacity: 1;
  padding: 15px;
}

.plus {
  position: absolute;
  margin-left: 10px;
  z-index: 5;
  font-size: 2em;
  line-height: 100%;
  -webkit-user-select: none;    
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
  -webkit-transition: .3s ease;
  -moz-transition: .3s ease;
  -o-transition: .3s ease;
  transition: .3s ease;
  pointer-events: none;
}

.questions:checked ~ .plus {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

.questions {
  display: none;
}

1 个答案:

答案 0 :(得分:4)

您可以使用如下所示的CSS content属性并删除现有的“更多信息”文本来完成此操作:

.questions ~ .question::before{
  content: 'More Info '
}

.questions:checked ~ .question::before{
  content: 'Less Info '
}

使用::before伪元素将确保将内容放在现有的“Qx”之前,以便获得您正在寻找的外观。

示例

enter image description here

@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,800);
body {
  font-family: 'Open Sans';
  font-size: 1.5em;
  background: #eee;
}

.question-content {
  width: 80%;
  padding: 20px;
  margin: 0 auto;
  padding: 0 60px 0 0;
}

.question {
  position: relative;
  background: #8FBC8F;
  margin: 0;
  padding: 10px 10px 10px 50px;
  display: block;
  width: 100%;
  cursor: pointer;
}

.answers {
  background: #999;
  padding: 0px 15px;
  margin: 5px 0;
  height: 0;
  overflow: hidden;
  z-index: -1;
  position: relative;
  opacity: 0;
  -webkit-transition: .7s ease;
  -moz-transition: .7s ease;
  -o-transition: .7s ease;
  transition: .7s ease;
}

.questions:checked~.answers {
  height: auto;
  opacity: 1;
  padding: 15px;
}

.questions~.question::before {
  content: 'More Info '
}

.questions:checked~.question::before {
  content: 'Less Info '
}

.plus {
  position: absolute;
  margin-left: 10px;
  z-index: 5;
  font-size: 2em;
  line-height: 100%;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
  -webkit-transition: .3s ease;
  -moz-transition: .3s ease;
  -o-transition: .3s ease;
  transition: .3s ease;
  pointer-events: none;
}

.questions:checked~.plus {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

.questions {
  display: none;
}
<div class="question-content">
  <div>
    <input type="checkbox" id="question1" name="q" class="questions">
    <div class="plus">+</div>
    <label for="question1" class="question">
    Q1
  </label>
    <div class="answers">
      What if the answer is really long and wraps the whole page and you never want to finish it but you have to because its the answer!
    </div>
  </div>

  <div>
    <input type="checkbox" id="question2" name="q" class="questions">
    <div class="plus">+</div>
    <label for="question2" class="question">
    Q2
  </label>
    <div class="answers">
      short!
    </div>
  </div>

  <div>
    <input type="checkbox" id="question3" name="q" class="questions">
    <div class="plus">+</div>
    <label for="question3" class="question">
    Q3
  </label>
    <div class="answers">
      This is the answer!
    </div>
  </div>
</div>