简体   繁体   English

iOS7用Javascript检测键盘高度?

[英]iOS7 Detect keyboard height with Javascript?

Now this issue has made it on here before ( What is the height of iPad's onscreen keyboard? ), but I think it needs a refresher due to iOS7 recently being released. 现在这个问题已经在这里发布了( iPad屏幕键盘的高度是多少? ),但我认为由于iOS7最近发布需要进一步复习。

The issue: I have a fixed position modal that appears in the bottom right corner of the page. 问题:我有一个固定位置模式出现在页面的右下角。 It has a single form field that gains focus when the modal opens. 它有一个单独的表单字段,可在模态打开时获得焦点。 The focus triggers the softkeyboard to open. 焦点触发软键盘打开。 The problem is that I want to programatically detect the keyboard height to position the modal at the top of the keyboard, otherwise part of the modal gets cutoff from view. 问题是我想以编程方式检测键盘高度以将模态定位在键盘的顶部,否则模态的一部分会从视图中截止。

What I've tried: 我尝试过的:

    var scrollHere = currentWidget.offset().top;
    //this scrolls the page to the top of the widget, but the keyboard is below.
    setTimeout(function() {
        $('html, body').scrollTop(scrollHere);
    }, 0);

The page scrolls to the top of the modal. 页面滚动到模态的顶部。 Not ideal because sometimes the form field is hidden below the keyboard. 不理想,因为有时表单字段隐藏在键盘下方。

I have also tried alerting the window.innerHeight 我也试过提醒窗口。内心高度

    alert(window.innerHeight);

But that shows up to be the same whether or not the keyboard is visible. 但无论键盘是否可见,这都表明相同。

So my question is, has anyone found a way to detect the iOS7 keyboard height in JavaScript? 所以我的问题是,有没有人找到一种方法来检测JavaScript中的iOS7键盘高度? Might there be a workaround? 可能有解决方法吗? Unlikely, but could this be a bug in iOS7 safari? 不太可能,但这可能是iOS7 Safari中的一个错误吗?

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

So the answer is, I was wrong about not being able to detect the window.innerHeight change. 所以答案是,我错误的是无法检测到window.innerHeight更改。

The reason I couldn't detect the change was because on the iPad the keyboard animates up from the bottom and does not fire a window resize event. 我无法检测到更改的原因是因为在iPad上,键盘从底部开始动画,并且不会触发窗口调整大小事件。 My solution was not to detect the window size, I made the body have a max-height of 100% when the modal is open. 我的解决方案是不检测窗口大小,当模态打开时,我使主体的最大高度为100%。 Then I wait for the user to focus on the text field and manipulate the scroll position at that point: 然后我等待用户专注于文本字段并操纵该点的滚动位置:

$(document.body).on('focus', '.input-field', function(){
    setTimeout(function(){
         window.scrollTo(0, $('#modalInput').offset().top - 100);
    }, 0);
});

This is for when you focus on your input field. 这适用于您专注于输入字段的情况。 iOS likes to try to center the window on the field when the keyboard opens, and that can be annoying if say you have information above the input the user needs to see. 当键盘打开时,iOS喜欢尝试将窗口置于字段中心,如果您在用户需要查看的输入上方有信息,则可能会很烦人。 The example above scrolls the window so that my text field is right above the keyboard. 上面的例子滚动窗口,使我的文本字段位于键盘正上方。 You'd think that's all you need to do, but iOS sometimes tries to be too smart. 您认为这就是您需要做的所有事情,但iOS有时会尝试过于聪明。 When the user starts typing in the input, it re-centers on the input again! 当用户开始输入输入时,它会再次以输入为中心! So we take the above code and make it into this: 所以我们采用上面的代码并将其转化为:

$(document.body).on('focus keyup', '.input-field', function(){
    setTimeout(function(){
         window.scrollTo(0, $('#modalInput').offset().top - 100);
    }, 0);
});

This way the scroll position never changes from where you want it while the user is typing. 这样,在用户键入时,滚动位置永远不会从您想要的位置发生变化。

Note : The only time I was able to detect the change in window.innerHeight was in the on keyup event because at that point the keyboard was done animating and fully shown on the page. 注意 :我唯一能够检测到window.innerHeight中的更改是在on keyup事件中,因为此时键盘已完成动画并完全显示在页面上。 On focus it still said the innerHeight was same as without the keyboard. 焦点仍然表示内心高度与没有键盘的情况相同。

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

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