css 如何使按钮保持按下状态直到再次按下

lnvxswe2  于 2023-02-06  发布在  其他
关注(0)|答案(3)|浏览(186)

我有一个按钮,它扩展一个带有背景图像的部分来显示隐藏的文本,但是我如何使它使我可以只按下按钮一次而不是having到悬停它或按住它?然后再按一次它返回到它的默认状态

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="main.css">
        <title>TITLE</title>
    </head>

<body>
    
    
      <div class="mainPic">
         <section>
            
           <header style="text-align: center;">
            <h1>TITLE & info</h1>
           </header>

           <div id="hidden-content">
        
                <label class="bottom-arrow" for="trigger"></label>
              <div id="list">
                <div>hidden text</div>
                <div>hidden text</div>
                <div>hidden text</div>
                <div>hidden text</div>
                <div>hidden text</div>
                <div>hidden text</div>
              </div>
           </div>
                  
          </section>
      </div>
    
    
</body>
</html>
@charset "utf-8";

body{
    background-color: white;
}

.mainPic{
    
    background-image: url("images/background.webp");
    background-repeat: no-repeat;
    
    background-size: cover;
    
    border: 1px solid black;
}

h1{
    
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    font-weight: 200;
    padding:0;
    margin:0;
    font-size: 75px;

    text-shadow: 1px 1px 2px red, 0 0 0.5em blue, 0 0 0.1em blue;
    
}

.bottom-arrow{
    content:'';
    position: absolute;
    left: 0;
    right: 0;
    margin: 0 auto;
    width: 0;
    height: 0;
    border-top: 25px solid #6A0136;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    cursor: pointer;
}

/*Custom CSS*/
section{position:relative;}

#hidden-content #list {
    max-height: 0;
    transition: max-height 0.15s ease-out;
    overflow: hidden;
}

#hidden-content:active #list {
    max-height: 500px;
    transition: max-height 0.25s ease-in;
}

#hidden-content:active .bottom-arrow{
  position:absolute;
  bottom:-25px;
  transition: 0.25s ease-out;
}

........................................................................

6rqinv9w

6rqinv9w1#

我从来没有找到过一个只使用CSS的解决方案,所以使用一个JavaScript的splash将有助于使用hidden-content容器上的一个类切换不同的状态,然后,使用新类的存在,您可以编辑HTML元素的外观。
换句话说,添加和删除类可以让您更新它的外观。

var trigger = document.querySelector('.bottom-arrow');
trigger.addEventListener('click', function(){
  document.getElementById('hidden-content').classList.toggle('active');
});
body{
    background-color: white;
}

.mainPic{
    
    background-image: url("images/background.webp");
    background-repeat: no-repeat;
    
    background-size: cover;
    
    border: 1px solid black;
}

h1{
    
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    font-weight: 200;
    padding:0;
    margin:0;
    font-size: 75px;

    text-shadow: 1px 1px 2px red, 0 0 0.5em blue, 0 0 0.1em blue;
    
}

.bottom-arrow{
    content:'';
    position: absolute;
    left: 0;
    right: 0;
    margin: 0 auto;
    width: 0;
    height: 0;
    border-top: 25px solid #6A0136;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    cursor: pointer;
}

/*Custom CSS*/
section{position:relative;}

#hidden-content #list {
    max-height: 0;
    transition: max-height 0.15s ease-out;
    overflow: hidden;
}

#hidden-content.active #list {
    max-height: 500px;
    transition: max-height 0.25s ease-in;
}

#hidden-content.active .bottom-arrow{
  position:absolute;
  bottom:-25px;
  transition: 0.25s ease-out;
}
<div class="mainPic">
   <section>
     <header style="text-align: center;">
        <h1>TITLE & info</h1>
     </header>

     <div id="hidden-content">
        <label class="bottom-arrow" for="trigger"></label>
        
        <div id="list">
          <div>hidden text</div>
          <div>hidden text</div>
          <div>hidden text</div>
          <div>hidden text</div>
          <div>hidden text</div>
          <div>hidden text</div>
        </div>
     </div>
    </section>
</div>
62o28rlo

62o28rlo2#

从技术上讲,你没有一个按钮。HTML标准包括一个button元素,这是人们在谈到网页/网络应用程序中的按钮时所想到的。
您希望根据元素的click事件切换hidden属性。

var label = document.querySelector("label");

label.addEventListener("click", function () {
document.getElementById("list").toggleAttribute("hidden");
});
<section>

<header style="text-align: center;">
<h1>TITLE & info</h1>
</header>

<div id="hidden-content">

<label class="bottom-arrow" for="trigger">CLICK ME</label>
<div id="list">
<div>hidden text</div>
<div>hidden text</div>
<div>hidden text</div>
<div>hidden text</div>
<div>hidden text</div>
<div>hidden text</div>
</div>
</div>

</section>
wz1wpwve

wz1wpwve3#

使用带有标签的复选框可以使用其状态。

body {
  background-color: white;
}

.mainPic {
  background-image: url("images/background.webp");
  background-repeat: no-repeat;
  background-size: cover;
  border: 1px solid black;
}

h1 {
  font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
  font-weight: 200;
  padding: 0;
  margin: 0;
  font-size: 75px;
  text-shadow: 1px 1px 2px red, 0 0 0.5em blue, 0 0 0.1em blue;
}

.bottom-arrow {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  margin: 0 auto;
  width: 0;
  height: 0;
  border-top: 25px solid #6A0136;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  cursor: pointer;
}

/*Custom CSS*/

section {
  position: relative;
}

#hidden-content #list {
  max-height: 0;
  transition: max-height 0.15s ease-out;
  overflow: hidden;
}

#trigger {
  display: none;
}

#trigger:checked + label + #list {
  max-height: 500px;
  transition: max-height 0.25s ease-in;
}

#trigger:checked + label.bottom-arrow {
  position: absolute;
  bottom: -25px;
  transition: 0.25s ease-out;
}
<div class="mainPic">
  <section>

    <header style="text-align: center;">
      <h1>TITLE & info</h1>
    </header>

    <div id="hidden-content">

      <input type="checkbox" id="trigger">
      <label class="bottom-arrow" for="trigger"></label>
      <div id="list">
        <div>hidden text</div>
        <div>hidden text</div>
        <div>hidden text</div>
        <div>hidden text</div>
        <div>hidden text</div>
        <div>hidden text</div>
      </div>
    </div>

  </section>
</div>

相关问题