css - 纯 CSS 弹出菜单 - 在点击/触摸/单击时显示/隐藏

标签 css

我正在尝试修改实现水平弹出窗口的纯 CSS 菜单。原始代码在悬停时执行。我试图通过触摸/点击实现相同的功能 - 即:点击一次显示它,再次点击关闭它。

这可能使用纯 CSS 吗?

我插入了下面的代码,但请引用 codepen - 它呈现得更好。我现在面临的挑战是我必须按住它才能显示菜单 - 如何在不使用 javascript 的情况下进行切换?

代码笔:http://codepen.io/pliablepixels/pen/WwPgwg

/*
Forked from http://codepen.io/IanLunn/pen/NPapxy */
/*
 sass flyout.scss >flyout.css (sudo gem install sass)
 or sass flyout.scss --style compressed >flyout.min.css
 "IL" logo Copyright (c) Ian Lunn Design Limited 2015

 Modified by pliable pixels
*/
.drawer {
  position: absolute;
  z-index: 10;
  top: 0;
  left: 0;
  /*height: 100%;*/
  padding: .4em 0;
  background: #7D294E;
  color: white;
  text-align: center;
  /* Remove 4px gap between <li> */
  font-size: 0;
}
.drawer li {
  pointer-events: none;
  position: relative;
  display: inline-block;
  vertical-align: middle;
  list-style: none;
  transform: translateZ(0);
}
.drawer a {
  pointer-events: auto;
  position: relative;
  display: block;
  min-width: 5em;
  margin-bottom: .4em;
  padding: .4em;
  line-height: 100%;
  /* Reset font-size */
  font-size: 16px;
  text-decoration: none;
  color: white;
  transition: background 0.2s;
}
.drawer a:active, .drawer a:focus {
  background: #B44659;
}
.drawer i {
  display: block;
  margin-bottom: .2em;
  font-size: 2em;
}
.drawer span {
  font-size: .625em;
  font-family: sans-serif;
  text-transform: uppercase;
}
.drawer li:active ul {
  /* Open the fly-out menu */
  transform: translateX(0);
  background: #B44659;
  /* Enable this if you wish to replicate hoverIntent */
}
.drawer > li {
  display: block;
  /* Fly out menus */
}
.drawer > li > a {
  background: #7D294E;
}
.drawer > li:active, .drawer > li:focus {
  z-index: 100;
}
.drawer > li:active, .drawer > li:focus a {
  background: #B44659;
}
.drawer > li a:active {
  background: #F56356;
}
.drawer > li ul {
  position: absolute;
  /* Stack below the top level */
  z-index: -1;
  top: 0;
  left: 100%;
  /* height: 100%;*/
  width: auto;
  white-space: nowrap;
  /* Close the menus */
  transform: translateX(-100%);
  background: #B44659;
  transition: 0.2s transform;
}
<ul class="drawer">
        <li>
            <a href="">
               
                <span>Info</span>
            </a>
            <ul>
                <li>
                    <a href="http://www.google.com">
                        
                        <span>Item 1</span>
                    </a>
                </li>
                <li>
                    <a href="http://www.google.com" >
                        
                        <span>Item 2</span>
                    </a>
                </li>
                <li>
                    <a href="http://www.google.com">
                        
                        <span>Item 3</span>
                    </a>
                </li>
            </ul>
        </li>
    </ul>

最佳答案

检查以下代码,它应该适合您。

<a href="#something">Show</a>
<div id="something">Bingo!</div>

CSS:

#something {
  display: none;
}

#something:target {
  display: block;
}

这是一个代码笔:http://codepen.io/anon/pen/EKreRy

关于css - 纯 CSS 弹出菜单 - 在点击/触摸/单击时显示/隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37117304/

相关文章:

jquery - 将鼠标悬停在他的位置时,子菜单仍然出现

jquery - 在 jquery 中单击链接时打开模式框

javascript - 如何使用 javascript、jquery 或 css 为模态调整大小设置动画?

php - 我无法将我的 styles.css 文件恢复到其原始形式

css - Bootstrap 4.2 和我的布局和行不是水平居中的

javascript - 存储文本输入并显示在页面上

jQuery:获取输入的值并将其作为类添加到封闭标签中?

javascript - jQuery - 查找最后选择的元素

html - 如何调整彼此下的div和链接

php - 动态 CSS - 移动图像