admin管理员组文章数量:1278910
I am not sure that "focus" is the right term here, but I don't know of anything better.
What I mean here by "focus" on a scrolling item is that the user can scroll that particular item with page-up and page-down keys. A container div on a page has a scrollbar, and I'd like it to have "focus" on load. Is there a way of doing that?
I am not sure that "focus" is the right term here, but I don't know of anything better.
What I mean here by "focus" on a scrolling item is that the user can scroll that particular item with page-up and page-down keys. A container div on a page has a scrollbar, and I'd like it to have "focus" on load. Is there a way of doing that?
Share Improve this question asked Feb 25 at 1:30 ChristopheChristophe 2,0781 gold badge19 silver badges24 bronze badges1 Answer
Reset to default 3Focus is the exact term. It's used mostly for tab navigation and it is an accessibility feature. To implement it we should use tabindex.
Note: tabindex="-1" may be useful for elements that should not be navigated to directly using the Tab key...
Since the div should not be focused with the tab key but programmatically we are going to use tabindex="-1"
.
Putting it together, you should be able to scroll the div using the arrow keys immediately:
const scrollableDiv = document.getElementById("b");
scrollableDiv.focus();
.box {
display: flex;
}
.scrollable {
width: 200px;
height: 200px;
overflow: scroll;
border: 1px solid #ccc;
}
.content {
height: 500px;
}
<div class="box">
<div class="scrollable"><div class="content">a</div></div>
<div class="scrollable" id="b" tabindex="-1"><div class="content">b</div></div>
</div>
本文标签: cssSetting quotfocusquot for scrolling items in HTMLStack Overflow
版权声明:本文标题:css - Setting "focus" for scrolling items in HTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741233106a2362472.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论