简体   繁体   English

如何在纯CSS中创建“粘性”下拉菜单?

[英]How do I create a “sticky” dropdown menu in pure CSS?

Effect I'm trying to achieve: 我想要实现的效果:

In Twitter's new redesign, they implemented a "sticky" dropdown menu for the account settings area. 在Twitter的新重新设计中,他们为帐户设置区域实施了“粘性”下拉菜单。

Basically, when you click on your username in the black global nav bar at the top, a menu is toggled open and stays open until you click on one of the links or on your username name again to toggle it closed. 基本上,当您在顶部的黑色全局导航栏中单击您的用户名时,菜单将打开并保持打开状态,直到您单击其中一个链接或再次使用您的用户名称切换为关闭。

I'd like to achieve the exact same effect of a sticky menu with just CSS and HTML. 我想用CSS和HTML实现粘性菜单的完全相同的效果。 I'm okay with using CSS3 features, but if this can be achieved without relying on CSS3, that's even better. 我可以使用CSS3功能,但如果可以在不依赖CSS3的情况下实现,那就更好了。

What I've tried so far 到目前为止我尝试过的

I can create the basic navigation menu with dropdown working with pure CSS and HTML, but only using the :hover pseudoclass. 我可以创建基本的导航菜单,下拉使用纯CSS和HTML,但只使用:hover pseudoclass。 I tried out the :active pseudoclass, but unfortunately it doesn't "stick" and stay open. 我尝试了:有效的伪类,但不幸的是它没有“坚持”并保持开放状态。

Is this "sticky" dropdown effect even possible without relying on javascript? 这种“粘性”下拉效果是否可以在不依赖javascript的情况下实现? If it is not possible without relying on javascript, how should I handle this so it degrades gracefully? 如果不依赖于javascript就不可能,我应该如何处理它以使它降级优雅?

I'm going off memory here as I cannot access Twitter from work. 我要离开这里,因为我无法从工作中访问Twitter。 If I recall correctly, the same script is used on the main page to login. 如果我没记错的话,在主页面上使用相同的脚本来登录。 It creates a little popup type window that stays there even after moving the mouse. 它创建了一个小弹出窗口,即使在移动鼠标后也会保持不变。

If that's what you're talking about you can't achieve that with just CSS; 如果这就是你所说的,你只能通过CSS实现这一目标; it's a styling language, not a scripting language. 它是一种样式语言,而不是脚本语言。 The :hover/:active pseudo-class styles will all un-apply themselves as soon as that event stops. :hover/:active伪类样式将在该事件停止后全部取消应用。

The alternative with Javascript involved would be to make the button a link that leads you to an actual page. 使用Javascript的替代方案是使按钮成为引导您进入实际页面的链接。 Then bind it's onclick to popup an absolutely positioned div that's hidden by default ( return false within the onclick to prevent following the link). 然后绑定它的onclick以弹出一个默认隐藏的绝对定位的div (在onclick中return false以防止跟随链接)。 This div isn't hidden until whatever condition you want to hide it with, and it starts off hidden, so if they don't have Javascript they won't know what they're missing. 这个div在你想隐藏它的任何条件下都不会被隐藏,并且它开始隐藏,所以如果他们没有Javascript,他们就不会知道他们缺少什么。

Use the pseudo class :focus instead of :active . 使用:focus而不是:active You might also need to use tabindex="" in your HTML to make an element accept focus. 您可能还需要在HTML中使用tabindex=""来使元素接受焦点。

However, iOS touchscreens don't seem to recognize tabindex="" . 但是,iOS触摸屏似乎无法识别tabindex=""

It's possible to achieve with help of CSS use label in combination with input[type="checkbox"] and :checked pseudoselector to store state 可以通过CSS使用标签结合输入[type =“checkbox”]来实现,并且:勾选伪选择器来存储状态

See example at https://developer.mozilla.org/en/CSS/%3achecked 请参阅https://developer.mozilla.org/en/CSS/%3achecked上的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM