点击加号或标签打开CSS手风琴

时间:2017-02-28 17:58:29

标签: css checkbox

我找到了一个很棒的CSS / html codepen,它允许我在点击label类的question元素内的文本时以手风琴的方式查看文本,但是我只有一个问题:

问题:

我不能使用类plus的加号来切换手风琴。相反,在当前的实现中,只有当我点击课程label的{​​{1}}元素内的文字时,手风琴才会打开并允许我查看答案。

现在的情况是,如果我点击加号(类question),没有任何反应。有人可以帮我修改这段代码,以便点击 .plus 的加号或 {{1}内的问题文本本身}元素切换答案的可见性?

这是codepen: https://codepen.io/ihatecoding/pen/ryOpQd

这是片段:

plus
label

3 个答案:

答案 0 :(得分:4)

一个简单的解决方法是给你的.pluspointer-events: none;。这允许鼠标点击加号图标,点击它后面的标签。

@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,800);

body {
  font-family: 'Open Sans';
  font-size: 1.5em;
  background: #eee;
}

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

.centerplease {
  margin: 0 auto;
  max-width: 270px;
  font-size: 40px;
}

.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;
}
<div class='centerplease'>
  FAQ accordion
</div>
<br>

<div class="content">
<div>
  <input type="checkbox" id="question1" name="q"  class="questions">
  <div class="plus">+</div>
  <label for="question1" class="question">
    This is the question that will be asked?
  </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">
    Short?
  </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">
    But what if the question is really long and wraps the whole page and you feel like you will never finish reading the question?
  </label>
  <div class="answers">
    This is the answer!
  </div>
</div>
</div>

答案 1 :(得分:1)

我会将加号移动到标签的伪元素中,因为它不需要在标记中并且仅用于显示。

@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,800);

body {
  font-family: 'Open Sans';
  font-size: 1.5em;
  background: #eee;
}

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

.centerplease {
  margin: 0 auto;
  max-width: 270px;
  font-size: 40px;
}

.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;
}

.question:before {
  position: absolute;
  margin-left: 10px;
  z-index: 5;
  font-size: 2em;
  line-height: 100%;
  content: '+';
  left: 0; top: 0;
  -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;
}

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

.questions {
  display: none;
}
<div class='centerplease'>
  FAQ accordion
</div>
<br>

<div class="content">
<div>
  <input type="checkbox" id="question1" name="q"  class="questions">
  <label for="question1" class="question">
    This is the question that will be asked?
  </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">
  <label for="question2" class="question">
    Short?
  </label>
  <div class="answers">
    short!
  </div>
</div>
  
<div>
  <input type="checkbox" id="question3" name="q" class="questions">
  <label for="question3" class="question">
    But what if the question is really long and wraps the whole page and you feel like you will never finish reading the question?
  </label>
  <div class="answers">
    This is the answer!
  </div>
</div>
</div>

答案 2 :(得分:0)

只需在标签中添加'plus',当然也可以修复定位。

  <label for="question1" class="question">
    <div class="plus">+</div>This is the question that will be asked?
  </label>