选中选项卡时隐藏内容

时间:2016-09-09 00:51:25

标签: html css html5 css3 sass

我使用单选按钮创建了一些标签,我在点击第二个标签(SPECIFICATION)时尝试隐藏div为id" hide-me"。为了解决这个问题,我尝试了这个:

ul.tabs [id^="#tab2"]:checked ~ #hide-me{
  display:none;
}

但没有工作。

如何仅使用HTML5,CSS3和SASS执行此操作。

的jsfiddle:

https://jsfiddle.net/2ucnpwbb/2/



body {
  padding: 20px;
  text-align: left;
  font-family: Lato;
  color: #fff;
  background: #ddd;
}
h1 {
  font-weight: normal;
  font-size: 40px;
  font-weight: normal;
  text-transform: uppercase;
  float: left;
  margin: 20px 0 100px 10px;
}
h1 span {
  font-size: 13px;
  display: block;
  padding-left: 4px;
}
.tabs {
  width: 650px;
  float: none;
  list-style: none;
  position: relative;
  margin: 80px 0 0 10px;
  text-align: left;
}
.tabs li {
  float: left;
  display: block;
}
.tabs input[type="radio"] {
  position: absolute;
  top: -9999px;
  left: -9999px;
}
.tabs label {
  display: block;
  padding: 14px 21px;
  border-radius: 2px 2px 0 0;
  font-size: 20px;
  font-weight: normal;
  text-transform: uppercase;
  background: #8e44ad;
  cursor: pointer;
  position: relative;
  top: 4px;
  -moz-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.tabs label:hover {
  background: #703688;
}
.tabs .tab-content {
  z-index: 2;
  display: none;
  overflow: hidden;
  width: 100%;
  font-size: 17px;
  line-height: 25px;
  padding: 25px;
  position: absolute;
  top: 53px;
  left: 0;
  background: #612e76;
}
.tabs [id^="tab"]:checked + label {
  top: 0;
  padding-top: 17px;
  background: #612e76;
}
.tabs [id^="tab"]:checked ~ [id^="tab-content"] {
  display: block;
}
p.link {
  clear: both;
  margin: 380px 0 0 15px;
}
p.link a {
  text-transform: uppercase;
  text-decoration: none;
  display: inline-block;
  color: #fff;
  padding: 5px 10px;
  margin: 0 5px;
  background-color: #612e76;
  -moz-transition: all 0.2s ease-in;
  -o-transition: all 0.2s ease-in;
  -webkit-transition: all 0.2s ease-in;
  transition: all 0.2s ease-in;
}
p.link a:hover {
  background-color: #522764;
}
#hide-me {
  margin-top: 250px;
  color: #000;
}
ul.tabs [id^="#tab2"]:checked ~ #hide-me {
  display: none;
}

<ul class="tabs">
  <li>
    <input type="radio" name="tabs" id="tab1" checked />
    <label for="tab1">Description</label>
    <div id="tab-content1" class="tab-content">
      <p>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
        dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
    </div>
  </li>

  <li>
    <input type="radio" name="tabs" id="tab2" />
    <label for="tab2">Specification</label>
    <div id="tab-content2" class="tab-content">
      <p>"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
        voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius
        modi tempora incidunt</p>
    </div>
  </li>
</ul>

<div style="clear:both;">
</div>

<div id="hide-me">
  HIDE ME HIDE ME HIDE ME
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

你可以这样做,但它的目标是^^

代码已更改:

#tab1:checked + ul li:first-of-type label,
#tab2:checked + #tab1 + ul li:nth-of-type(2) label {
  top: 0;
  padding-top: 17px;
  background: #612e76;
}
#tab1:checked + ul li:first-of-type .tab-content,
#tab2:checked + #tab1 + ul li:nth-of-type(2) .tab-content {
  display: block;

#tab2:checked + #tab1 + ul + div + #hide-me {
  display: none;
}

body {
  padding: 20px;
  text-align: left;
  font-family: Lato;
  color: #fff;
  background: #ddd;
}
h1 {
  font-weight: normal;
  font-size: 40px;
  font-weight: normal;
  text-transform: uppercase;
  float: left;
  margin: 20px 0 100px 10px;
}
h1 span {
  font-size: 13px;
  display: block;
  padding-left: 4px;
}
.tabs {
  width: 650px;
  float: none;
  list-style: none;
  position: relative;
  margin: 80px 0 0 10px;
  text-align: left;
}
.tabs li {
  float: left;
  display: block;
}
input[type="radio"] {
  display:none;
}
.tabs label {
  display: block;
  padding: 14px 21px;
  border-radius: 2px 2px 0 0;
  font-size: 20px;
  font-weight: normal;
  text-transform: uppercase;
  background: #8e44ad;
  cursor: pointer;
  position: relative;
  top: 4px;
  -moz-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.tabs label:hover {
  background: #703688;
}
.tabs .tab-content {
  z-index: 2;
  display: none;
  overflow: hidden;
  width: 100%;
  font-size: 17px;
  line-height: 25px;
  padding: 25px;
  position: absolute;
  top: 53px;
  left: 0;
  background: #612e76;
}
#tab1:checked + ul li:first-of-type label,
#tab2:checked + #tab1 + ul li:nth-of-type(2) label {
  top: 0;
  padding-top: 17px;
  background: #612e76;
}
#tab1:checked + ul li:first-of-type .tab-content,
#tab2:checked + #tab1 + ul li:nth-of-type(2) .tab-content {
  display: block;
}
p.link {
  clear: both;
  margin: 380px 0 0 15px;
}
p.link a {
  text-transform: uppercase;
  text-decoration: none;
  display: inline-block;
  color: #fff;
  padding: 5px 10px;
  margin: 0 5px;
  background-color: #612e76;
  -moz-transition: all 0.2s ease-in;
  -o-transition: all 0.2s ease-in;
  -webkit-transition: all 0.2s ease-in;
  transition: all 0.2s ease-in;
}
p.link a:hover {
  background-color: #522764;
}
#hide-me {
  margin-top: 250px;
  color: #000;
}
#tab2:checked + #tab1 + ul + div + #hide-me {
  display: none;
}
<input type="radio" name="tabs" id="tab2" />
<input type="radio" name="tabs" id="tab1" checked />
<ul class="tabs">
  <li>
    <label for="tab1">Description</label>
    <div id="tab-content1" class="tab-content">
      <p>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
        dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
    </div>
  </li>

  <li>
    <label for="tab2">Specification</label>
    <div id="tab-content2" class="tab-content">
      <p>"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
        voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius
        modi tempora incidunt</p>
    </div>
  </li>
</ul>

<div style="clear:both;">
</div>

<div id="hide-me">
  HIDE ME HIDE ME HIDE ME
</div>