gpt4 book ai didi

html - 为什么按 TAB 键(:focus effect) causes positioned off-the-screen
element with <input> field inside it to appear on the screen?

转载 作者:可可西里 更新时间:2023-11-01 13:28:30 24 4
gpt4 key购买 nike

enter image description here

我想知道,为什么按了TAB key在屏幕上显示包含可聚焦元素(输入、超链接)的负边距右( margin-right:-170px )的 div 容器?有什么想法吗?

在我的例子中,我需要防止这种行为,因为我有侧边栏菜单(在屏幕外),它会在用户多次按下标签按钮后显示。它不应该。我只想在移动设备和平板设备上显示此侧边栏。

注:更重要的是。 Firefox 中的行为不同Google Chrome不同.在我的示例中,如果您删除 <input>元素并仅保留超链接 <a> , 按 tab 键后不会显示该框。在 Chrome 中它会。

类似帖子: Chrome bug? or how do I prevent a form field to SCROLL the container when focused?

解决方案之一(防止TAB):可能会解决问题,但不推荐。

    $("body").on("keydown",  function (e) {
if (e && e.keyCode === 9) {
console.log('Tab was pressed', e);
e.preventDefault();
}
});

>> 截图中的工作示例:

.box {
width: 150px;
height: 50px;
display: block;
right: 0;
margin-right:-170px; /* Negative margin to set container off the screen*/
padding:10px;
background: #292929;
color: #fff;
position: absolute;
-webkit-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
box-shadow:2px 2px 5px rgba(0, 0, 0, .4)
}

.box.input {
top: 0;
}

.box.hyperlink {
top: 100px;
}

body {
background: lightgray;
}

a {
color: white;
}
input {
margin:10px 15px;
}
p {
width:400px;
}
<p>After you press TAB key, box containing input will appear from the right on the screen.... but why?</p>

<div class="box input">
<label>Custom Input 1</label>
<input type="text" placeholder="some input" />
</div>

<div class="box hyperlink">
<label>Custom hyperlink</label>
<a href="http://www.apple.com" target="_blank">Some link</a>
</div>

>> 工作示例#2

http://jsfiddle.net/pndz/yy9kzp3e/

最佳答案

正在设置您的 .boxfixed而不是 absolute防止行为。

.box {
/* ... */
position: fixed; /* position the element relative to the viewport */
/* ... */
}

我假设是因为浏览器无法滚动“超出视口(viewport)”。如果您打算将元素放置在屏幕之外,这也是更直观的语义。


如果侧边栏仅适用于小型设备,我同意这样的观点,即在其他设备上根本不显示它会更干净。例如

@media all and (min-width: 769px) { /* determining the correct size is up to you */
.box {
display: none;
}
}

但请注意,视口(viewport)实际上并不是检测“小型设备”的非常可靠的方法

解释

盒子放在你的<body>里面它扩展以容纳内容(通过水平滚动条显而易见)。当按 Tab 键时,焦点会转到 <input> ,浏览器将焦点元素滚动到 View 中。所以实际上不是盒子而是整个元素内容向左移动。从技术上讲 scrollLeft包含节点的属性(在您的情况下为 <body>)设置为使输入在屏幕上可见。

这是从 Chromium 的 Angular 来看,更精细的实现细节可能因浏览器而异。

关于html - 为什么按 TAB 键(:focus effect) causes positioned off-the-screen <div> element with &lt;input&gt; field inside it to appear on the screen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33995888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com