【问题标题】:Center focused element of webpages vertically垂直居中网页元素
【发布时间】:2016-04-12 20:14:32
【问题描述】:

将焦点更改到不在您的查看区域中的网站的另一个元素(例如,通过按 TAB 键到另一个输入字段)时(例如,您在页面底部,新元素在顶部)所有浏览器(在 Windows 操作系统上测试过的 Internet Explorer、Firefox、Chrome)似乎都可以滚动,以使具有新焦点的元素可见。

例子:

Image
Text
Input text
...
Footer

当您位于FooterImage 时,Input text 不在您的查看区域内并且您现在正在按 Tab,因此焦点会跳转到 Input text,此字段现在对您可见但不可见Image 也不是 Text

虽然这种行为在大多数情况下可能是合适的,但在我的情况下却不是。为了防止这种情况,我建议将焦点元素垂直居中。如何做到这一点?

为了更清楚:

这是整个页面:

我正在向下滚动到底部,所以只有“这是页脚”可见:

我现在通过按 Tab 键来改变焦点。焦点跳转到文本输入。不幸的是,此文本字段上方的元素不可见:

【问题讨论】:

  • 理论上的答案(没有功能齐全的javascript)可以吗?
  • @LGSon 我至少需要有人指出我正确的方向。我知道如何调用这个问题,所以我无法寻找解决方案。
  • @Drako:我不想完全阻止这种变化,但新的焦点元素应该垂直居中(它在屏幕的中心,所以它上面的元素也是可见的)。跨度>
  • 好的,在我的新评论中尝试更改禁用以更改样式:$(this).css('margin-left', '50%'),工作吗?

标签: javascript html css


【解决方案1】:

抱歉,我不明白您为什么要将元素垂直居中? 当焦点元素被隐藏时,您可以获得焦点事件并阻止它。

var items = $(document).getElementsByTagName("*");
for (var i = items.length; i--;) {
    $(this).focus(function() {
        if ($(this).attr('id') == 'ID FOR THE HIDDEN ELEMENT') {
            // Disabled focus or pass focus to next element
            $(this).blur(); // Disabled
        }
    }
}

【讨论】:

  • 如果有ID,获取元素并更改样式:$('#ID').css('margin-left', '50%');
  • 我已经更新了我的问题,以更清楚地说明我想要实现的目标。
【解决方案2】:

如果将图像和文本与输入一起包装到一个标签中,它似乎可以在没有脚本的情况下工作。

<label for="input1">
  <img src="http://placehold.it/350x50"><br>
  Some text that should be visible
  <input id="input1" type="text">
</label>
<br>
<br>
<br>
<label for="input2">
  <img src="http://placehold.it/350x50"><br>
  Some text that should be visible
  <input id="input2" type="text">
</label>
<br>
<br>
<br>
<label for="input3">
  <img src="http://placehold.it/350x50"><br>
  Some text that should be visible
  <input id="input3" type="text">
</label>
<br>
<br>
<br>
<label for="input4">
  <img src="http://placehold.it/350x50"><br>
  Some text that should be visible
  <input id="input4" type="text">
</label>
<br>

如果您不能使用标签,我建议您添加一个事件处理程序,在其中捕获“tab 键”,获取具有焦点的元素并获取其父元素(可能包含您的输入以及文本和图像)并将该父级滚动到视图中

document.activeElement.parentNode.scrollIntoView();

或者你需要计算document.activeElement的位置并将其滚动到中心,这是ThinkingStiff制作的一个很好的方法,我用scrollIntoViewCenter方法更新了。

Element.prototype.documentOffsetTop = function () {
    return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop() : 0 );
};
Element.prototype.scrollIntoViewCenter = function () {
  window.scrollTo( 0, this.documentOffsetTop() - (window.innerHeight / 2 ) );
};

这是一个显示scrollIntoViewCenter 方法的sn-p。

Element.prototype.documentOffsetTop = function () {
  return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop() : 0 );
};

Element.prototype.scrollIntoViewCenter = function () {
  window.scrollTo( 0, this.documentOffsetTop() - (window.innerHeight / 2 ) );
};


window.addEventListener("keyup", myScript);


function myScript(e) {
  if ('9' == e.keyCode) {  // tab = 9
    //find and vertically center focused input
    document.activeElement.scrollIntoViewCenter();
  }
}
<img src="http://placehold.it/350x50"><br>
Some text that should be visible
<input id="input1" type="text">
<br>
<br>
<br>

<img src="http://placehold.it/350x50"><br>
Some text that should be visible
<input id="input2" type="text">
<br>
<br>
<br>

<img src="http://placehold.it/350x50"><br>
Some text that should be visible
<input id="input3" type="text">
<br>
<br>
<br>

<img src="http://placehold.it/350x50"><br>
Some text that should be visible
<input id="input4" type="text">
<br>
<br>
<br>

<img src="http://placehold.it/350x50"><br>
Some text that should be visible
<input id="input5" type="text">
<br>
<br>
<br>

<img src="http://placehold.it/350x50"><br>
Some text that should be visible
<input id="input6" type="text">
<br>
<br>
<br>

【讨论】:

  • 这是正确的方向。如果没有 JavaScript,这可能吗?
  • 您是否尝试将所有图像、文本和输入包装在一个标签中,该标签将其for 属性设置为输入的ID? ......这可能会做到。如果可以(但不明白我的意思),请发布您的 html,我可以制作一个 sn-p 给您看。
  • @mosquito87 我用似乎无需脚本即可工作的东西更新了我的答案。
  • HTML 是由框架在服务器端创建的。所以我不能直接影响 HTML。我正在测试你的建议。谢谢。
  • 似乎该框架仅提供创建带有文本的标签或使用一个标签创建 HTML 元素的功能。但标签不能充当容器。我正在尝试你的 JS。
猜你喜欢
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多