[HTML5] Using the focus event to improve navigation accessibility (nextElementSibling)
html5 to The Using Event navigation focus Improve
2023-09-14 08:59:14 时间
For a menu item, when we tab onto it, we want this element get 'focus' event, so that the submenu will show up. In the post, we will see how to achieve it by using JS+css, we will also see how to use 'nextElementSibling' to only focus the elemnt has menu popup.
HTML: Highlighted part is the submenue
<nav> <ul class="menu"> <li class="menu__item"> <a href="/" class="menu__link">About</a> </li> <li class="menu__item"> <a href="/" class="menu__link">News</a> <ul class="submenu"> <li class="submenu__item"> <a href="/" class="submenu__link">Press Releases</a> </li> <li class="submenu__item"> <a href="/" class="submenu__link">Blog</a> </li> </ul> </li> <li class="menu__item"> <a href="/" class="menu__link">Contact</a> </li> </ul> </nav>
JS: We want to add 'focus' class when element get focused, in the meanwhile, we only apply focus class to the element which has 'nextElementSibling' which is <ul class="submenu">
const topMenuLinks = document.querySelectorAll(".menu__link"); topMenuLinks.forEach(link => { if (link.nextElementSibling) { link.addEventListener("focus", function() { this.parentElement.classList.add("focus"); }); } });
CSS:
.menu { display: flex; list-style: none; &__item { position: relative; &:hover .submenu, &.focus .submenu { transform: scaleY(1); } }
相关文章
- html5手机页面的那些meta
- HTML5 Web Audio API实现【董小姐】播放
- html5的audio在safari(windows)中无效
- 【转】CSS 与 HTML5 响应式图片
- HTML5实现全屏API【进入和退出全屏】(html5 requestFullScreen)
- 【HTML5】Server-Sent服务器发送事件
- 【HTML5】Web存储
- [HTML5] Using HTMLPortalElement to improve MPA preformance
- [HTML5] Add an SVG Image to a Webpage and Get a Reference to the Internal Elements in JavaScript
- [HTML5] Accessibility Implementation for complex component
- [HTML5]移动平台的HTML5开发框架
- 最大化兼容 html5 视频
- HTML5 Web Workers来加速您的移动Web应用
- 17. Html5的局: VR全景分屏视频距离我们有多远?
- HTML5 直播技术
- SAP云平台部署HTML5应用时的错误消息Failed to report quota consumption
- 成功解决MSB8020 The build tools for v141 (Platform Toolset = ‘v141‘) cannot be found. To build using the
- 【K3s】第25篇 解决The connection to the server localhost:8080 was refused
- EnvironmentNotWritableError: The current user does not have write permissions to the targe...
- 采用 HTML5 File API 达到client log
- 怎样在Ubuntu手机平台中开发Cordova HTML5应用
- 39个让你受益的HTML5教程
- PAT 1033 To Fill or Not to Fill[dp]
- Error:The supplied javaHome seems to be invalid. I cannot find the java executable. Tried location:
- Digitalofficepro:PowerPoint to HTML5 SDK