html - 水平弹出菜单 - 隐藏选项

标签 html css

我需要为按钮创建简单的水平(向左)弹出菜单。我准备了这个DEMO .

.menu {
  position: absolute;
  right: 0;
  height: 50px;
  width: 50px;
  color: white;
}
.menu li {
  pointer-events: none;
  position: relative;
  display: inline-block;
  vertical-align: middle;
  list-style: none;
  line-height: 100%;
  transform: translateZ(0);
}
.menu a {
  pointer-events: auto;
  position: relative;
  display: block;
  min-width: 5em;
  margin-bottom: .4em;
  padding: .4em;
  line-height: 100%;
  font-size: 16px;
  text-decoration: none;
  color: white;
  transition: background 0.3s;
}
.menu a:active, .menu a:focus {
  background: #B44659;
}
.menu i {
  display: block;
  margin-bottom: .2em;
  font-size: 2em;
}
.menu span {
  font-size: .625em;
  font-family: sans-serif;
  text-transform: uppercase;
}
.menu li:hover ul {
  transform: translateX(0);
  background: #B44659;
}
.menu > li {
  display: block;
}
.menu > li > a {
  background: #7D294E;
}
.menu > li:hover {
  z-index: 100;
}
.menu > li:hover a {
  background: #B44659;
}
.menu > li a:hover {
  background: #F56356;
}
.menu > li > a:after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 100%;
  width: 4px;
  opacity: 0;
  background: -moz-linear-gradient(left, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(0, 0, 0, 0.65)), color-stop(100%, rgba(0, 0, 0, 0)));
  background: -webkit-linear-gradient(left, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -o-linear-gradient(left, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -ms-linear-gradient(left, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: linear-gradient(to right, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00000000',GradientType=1 );
  transition: opacity 0.5s;
}
.menu > li:hover a:after {
  opacity: 1;
}
.menu > li ul {
  position: absolute;
  z-index: -1;
  top: 0;
  right: 100%;
  height: 100%;
  width: auto;
  white-space: nowrap;
  transform: translateX(100%);
  background: #B44659;
  transition: 0.5s transform;
}
<ul class="menu">
  <li>
    <a href="#">
      <span>ITEM 1</span>
    </a>
    <ul>
      <li>
        <a href="https://google.co.uk/" target="_blank">
          <span>SUBITEM 1.1</span>
        </a>
      </li>
      <li>
        <a href="https://google.co.uk/" target="_blank">
          <span>SUBITEM 1.2</span>
        </a>
      </li>
      <li>
        <a href="https://google.co.uk/" target="_blank">
          <span>SUBITEM 1.3</span>
        </a>
      </li>
    </ul>
  </li>
</ul>

当您将鼠标悬停在某个元素上时,选项会从右向左滑动。问题是隐藏它们 - 现在它们只是移回右边,但我想完全隐藏它们。是否可以使用纯 CSS 实现此目的?

最佳答案

您可以将overflow: hidden 设置到您的菜单。此外,您还需要设置更大的宽度,这样当您滑动菜单的其余部分时,它就不会溢出。

.visible-item{
  width: 40px;
  text-align: center;
}
.menu {
  position: absolute;
  right: 0;
  height: 30px;
  width: 50px;
  color: white;
  display: flex;
  justify-content: flex-end;
  overflow: hidden;
}

.menu:hover{
  width: 400px;
}
.menu li {
  pointer-events: none;
  position: relative;
  display: inline-block;
  vertical-align: middle;
  list-style: none;
  line-height: 100%;
  transform: translateZ(0);
}
.menu a {
  pointer-events: auto;
  position: relative;
  display: block;
  min-width: 5em;
  margin-bottom: .4em;
  padding: .4em;
  line-height: 100%;
  font-size: 16px;
  text-decoration: none;
  color: white;
  transition: background 0.3s;
}
.menu a:active, .menu a:focus {
  background: #B44659;
}
.menu i {
  display: block;
  margin-bottom: .2em;
  font-size: 2em;
}
.menu span {
  font-size: .625em;
  font-family: sans-serif;
  text-transform: uppercase;
}
.menu li:hover ul {
  transform: translateX(0);
  background: #B44659;
}
.menu > li {
  display: block;
}
.menu > li > a {
  background: #7D294E;
}
.menu > li:hover {
  z-index: 100;
}
.menu > li:hover a {
  background: #B44659;
}
.menu > li a:hover {
  background: #F56356;
}
.menu > li > a:after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 100%;
  width: 4px;
  opacity: 0;
  background: -moz-linear-gradient(left, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(0, 0, 0, 0.65)), color-stop(100%, rgba(0, 0, 0, 0)));
  background: -webkit-linear-gradient(left, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -o-linear-gradient(left, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -ms-linear-gradient(left, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: linear-gradient(to right, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00000000',GradientType=1 );
  transition: opacity 0.5s;
}
.menu > li:hover a:after {
  opacity: 1;
}
.menu > li ul {
  position: absolute;
  z-index: -1;
  top: 0;
  right: 100%;
  height: 100%;
  width: auto;
  white-space: nowrap;
  transform: translateX(100%);
  background: #B44659;
  transition: 0.5s transform;
}
<ul class="menu">
  <li>
    <a href="#" class="visible-item">
      <span>ITEM 1</span>
    </a>
    <ul>
      <li>
        <a href="https://google.co.uk/" target="_blank">
          <span>SUBITEM 1.1</span>
        </a>
      </li>
      <li>
        <a href="https://google.co.uk/" target="_blank">
          <span>SUBITEM 1.2</span>
        </a>
      </li>
      <li>
        <a href="https://google.co.uk/" target="_blank">
          <span>SUBITEM 1.3</span>
        </a>
      </li>
    </ul>
  </li>
</ul>

关于html - 水平弹出菜单 - 隐藏选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54019834/

相关文章:

javascript - 如何在 AppleScript 中使用 javascript 返回聊天室中的用户列表

javascript - JavaScript 中的简单代谢率计算

html - 放大和缩小时网页元素会改变大小

html - 如何在 MVC (asp.net) 中为进入域和根目录的应用程序编写 css?

html - 将登录页面创建为弹出窗口,使页面变得模糊

html - 为什么要这样使用数据属性?

jquery - 如何使用JQuery播放连续的歌曲?

javascript - 这个绝对定位的元素没有定位在它最近的祖先旁边,而是附加到文档主体,我想知道为什么

css - 如何将当前标签实例传递给angular2中的事件

javascript - bootstrap 4 中 "visible-xs"的等价物是什么