html 如何使一个元素顺利地扩展自己?

hsgswve4  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(103)

我需要最深的li>span文本来平滑地扩展自己,因为我在divli类上徘徊。

ul {
  list-style-type: none;
}

.kinds {
  width: 600px;
  margin-left: auto;
  margin-right: auto;
}

.drop_vert_menu {
  text-align: center;
  width: 600px;
  margin: 0;
  padding: 0;
}

.drop_vert_menu li ul {
  list-style-type: none;
  width: 600px;
  padding: 0;
  background-color: #666;
  position: relative;
  display: none;
}

.drop_vert_menu>li {
  margin-left: auto;
  margin-right: auto;
  position: relative;
  padding: 10px 0px;
}

.drop_vert_menu li:hover ul {
  display: block;
}

.drop_vert_menu li ul li {
  text-align: center;
  padding: 8px 0px;
}
<div class="kinds">
  <ul class="drop_vert_menu">
    <li>
      <div class="li"><span>text that expands</span>
        <ul>
          <li><span>text that should be expanded</span></li>
        </ul>
      </div>
    </li>
  </ul>
</div>
hmmo2u0o

hmmo2u0o1#

使用transitiontransform代替display: none

我对你现有的CSS样式做了一个很小的调整。
我没有在display: none;display: block之间切换,而是使用opacitytransform来显示和隐藏。
几点注意事项:

  • transition:这决定了哪些更改将被动画化。在本例中,我transition编辑了opacitytransform。如果你愿意,你可以使用max-height来模拟垂直扩展/折叠。
  • pointer-events:确保不可见项不会中断光标悬停/单击事件
ul {
  list-style-type: none;
}

.kinds {
  width: 600px;
  margin-left: auto;
  margin-right: auto;
}

.drop_vert_menu {
  text-align: center;
  width: 600px;
  margin: 0;
  padding: 0;
}

.drop_vert_menu li ul {
  list-style-type: none;
  width: 600px;
  padding: 0;
  background-color: #666;
  position: relative;
  /* changes are made here */
  transition: transform .2s, opacity .2s;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-15px);
  /* --------------------- */
}

.drop_vert_menu>li {
  margin-left: auto;
  margin-right: auto;
  position: relative;
  padding: 10px 0px;
}

.drop_vert_menu li:hover ul {
  /* changes are made here */
  opacity: 1;
  transform: translateY(0);
  /* --------------------- */
}

.drop_vert_menu li ul li {
  text-align: center;
  padding: 8px 0px;
}
<div class="kinds">
  <ul class="drop_vert_menu">
    <li>
      <div class="li"><span>text that expands</span>
        <ul>
          <li><span>text that should be expanded</span></li>
        </ul>
      </div>
    </li>
  </ul>
</div>

相关问题