html 按钮下拉功能

yrdbyhpb  于 2023-09-28  发布在  其他
关注(0)|答案(2)|浏览(115)

我有以下代码:

document.querySelector('.rectangle')
  .addEventListener('click', function() {
   this.classList.toggle('expanded');
   const plus = this.querySelector('.plus');
   plus.style.transform = 
this.classList.contains('expanded') ? 'rotate(45deg)' : 'rotate(0)';
});
.tile-container {
  padding: 5px;
  font-size: 25px;
}

.rectangle {
  background-color: #0051a5;
  padding-right: 1em;
  padding-top: 1em;
  cursor: pointer;
  border-radius: 1em;
  display: grid;
  grid-template-rows: 1fr auto;
  grid-template-columns: auto 1em;
  grid-template-areas: "sample plus" "extratext extratext";
}

.plus {
  grid-area: plus;
  background: linear-gradient(#0051a5 0 0), linear-gradient(#0051a5 0 0), #fff;
  background-position: center;
  background-size: 60% 2.5px, 2.5px 60%;
  background-repeat: no-repeat;
  transition: transform 0.3s ease;
}

.radius {
  border-radius: 50%;
  width: 1em;
  height: 1em;
  margin-right: 1em;
}

.tile-label {
  grid-area: sample;
  font-family: 'PT Sans Narrow', sans-serif;
  font-size: 1em;
  text-transform: uppercase;
  font-weight: 600;
  color: #fff;
  padding-left: 1em;
  height: 2em;
}

.tile-accent {
  color: #FFC72C;
}

.hidden-text {
  grid-area: extratext;
  display: none;
  font-family: 'PT Sans Narrow', sans-serif;
  font-size: 0.75em;
  background-color: #fff;
  color: #000;
  margin: 1em;
  padding-top: 0.5em;
  padding-left: 1em;
}

.expanded>.hidden-text {
  display: block;
   animation: fade-in 1s;
}
@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}
<head>

<link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=PT+Sans+Narrow:wght@400;700&display=swap" rel="stylesheet">
</head>

<body>
  <div class="tile-container">
    <div class="rectangle">
      <div class="plus radius"></div>
      <div class="tile-label">
        Sample <span class="tile-accent">Text</span>
      </div>
      <div class="hidden-text">
        <ul>
          <li>Not Dropdown Text</li>
          <li>Dropdown Text</li>
          <li>Not Dropdown Text</li>
        </ul>
      </div>
    </div>
  </div>
</body>

在这段代码中,“非下拉文本”表示任何没有下拉功能的文本;然而,“下拉文本”指的是可以进一步展开以显示隐藏文本的文本。
我如何才能在这个盒子里实现自我?当我单击具有进一步隐藏功能的文本时,它应该在框内显示隐藏文本。

e5nszbig

e5nszbig1#

一种解决方案是在列表中创建一个包含更多数据的列表。
我还将面板本身的单击处理程序更改为标题。通过这种方式,您可以单击主体中的元素以打开/关闭。

document.querySelector('.tile-label')
  .addEventListener('click', function() {
    const _parent = this.parentNode;
    _parent.classList.toggle('expanded');
    const plus = _parent.querySelector('.plus');
    plus.style.transform = _parent.classList.contains('expanded') ? 'rotate(45deg)' : 'rotate(0)';
  });

const toggleChild = (e) => {

  if (e.currentTarget == e.target) {
    const el = e.target;
    el.classList.toggle("active")
  }
}

const level2 = document.querySelectorAll(".hidden-text ul li:has(ul)");

level2.forEach((li) => li.addEventListener("click", toggleChild))
.tile-container {
  padding: 5px;
  font-size: 25px;
}

.rectangle {
  background-color: #0051a5;
  padding-right: 1em;
  padding-top: 1em;
  border-radius: 1em;
  display: grid;
  grid-template-rows: 1fr auto;
  grid-template-columns: auto 1em;
  grid-template-areas: "sample plus" "extratext extratext";
}

.plus {
  grid-area: plus;
  background: linear-gradient(#0051a5 0 0), linear-gradient(#0051a5 0 0), #fff;
  background-position: center;
  background-size: 60% 2.5px, 2.5px 60%;
  background-repeat: no-repeat;
  transition: transform 0.3s ease;
}

.radius {
  border-radius: 50%;
  width: 1em;
  height: 1em;
  margin-right: 1em;
}

.tile-label {
  grid-area: sample;
  font-family: 'PT Sans Narrow', sans-serif;
  font-size: 1em;
  text-transform: uppercase;
  cursor: pointer;
  font-weight: 600;
  color: #fff;
  padding-left: 1em;
  height: 2em;
}

.tile-accent {
  color: #FFC72C;
}

.hidden-text {
  grid-area: extratext;
  display: none;
  font-family: 'PT Sans Narrow', sans-serif;
  font-size: 0.75em;
  background-color: #fff;
  color: #000;
  margin: 1em;
  padding-top: 0.5em;
  padding-left: 1em;
}

.expanded>.hidden-text {
  display: block;
  animation: fade-in 1s;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

/*Hides the child list be default*/

.hidden-text ul li ul {
  display: none;
}

/*active class toggles the visibility of the sub elements*/

.hidden-text ul li.active ul{
  display: block;
}

li .plus{
  transform:rotate(0);
}

li.active .plus{
  transform:rotate(45deg);
}

/*changes the cursor to a pointer for the list items that have a child ul*/

.hidden-text ul li:has(ul) .plus {
  float:right
}

.hidden-text ul li:has(ul) {
  cursor: pointer;
}
<head>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=PT+Sans+Narrow:wght@400;700&display=swap" rel="stylesheet">
</head>

<body>
  <div class="tile-container">
    <div class="rectangle">
      <div class="plus radius"></div>
      <div class="tile-label">
        Sample <span class="tile-accent">Text</span>
      </div>
      <div class="hidden-text">
        <ul>
          <li>Not Dropdown Text</li>
          <li>Dropdown Text <div class="plus radius"></div>
            <ul>
              <li>More Text!!</li>
            </ul>
          </li>
          <li>Not Dropdown Text</li>
        </ul>
      </div>
    </div>
  </div>
</body>
gwo2fgha

gwo2fgha2#

若要在“删除”框中获取删除,您可以尝试:

  • 首先,在.hidden-text div中添加一个嵌套的下拉结构。
  • 然后,在CSS中,将嵌套的下拉列表样式设置为默认情况下隐藏并在单击时显示。
  • 最后,使用JavaScript将事件侦听器添加到嵌套对象中,以切换其可见性

相关问题