问题描述
在我的iPhone上,我希望在按下文本字段并显示键盘时隐藏页脚.现在,它只是将自己定位在键盘上方,而显示的网站太少了.
On my iPhone, I want the footer to be hidden when a text field is pushed and the keyboard appears. Right now it's just positioning itself above the keyboard and too little of the website is shown.
<div data-role="footer" data-id="foo1" data-position="fixed">
<div data-role="navbar">
<div data-role="controlgroup" data-type="vertical">
<ul><li><input data-iconpos="top" data-icon='plus' type="button" value="Tur" id='nyTur' /></li>
<li><input data-iconpos="top" data-icon='plus' type="button" value="48%" id='ny48' /></li>
<li><input data-iconpos="top" data-icon='plus' type="button" value="100%" id='ny100' /></li>
</ul>
</div>
</div><!-- /navbar -->
</div><!-- /footer -->
推荐答案
这是一个很难获得正确"的问题.您可以尝试在输入元素焦点上隐藏页脚,并在模糊下显示,但这在iOS上并不总是可靠的.每隔一段时间(例如,在我的iPhone 4S中,十分之一),焦点事件似乎无法触发(或者JQuery Mobile出现竞争情况),并且页脚没有被隐藏.
This is a difficult problem to get 'right'. You can try and hide the footer on input element focus, and show on blur, but that isn't always reliable on iOS. Every so often (one time in ten, say, on my iPhone 4S) the focus event seems to fail to fire (or maybe there is a race condition with JQuery Mobile), and the footer does not get hidden.
经过反复试验,我想出了一个有趣的解决方案:
After much trial and error, I came up with this interesting solution:
<head>
...various JS and CSS imports...
<script type="text/javascript">
document.write( '<style>#footer{visibility:hidden}@media(min-height:' + ($( window ).height() - 10) + 'px){#footer{visibility:visible}}</style>' );
</script>
</head>
本质上:使用JavaScript确定设备的窗口高度,然后在窗口高度缩小10个像素时动态创建CSS媒体查询以隐藏页脚.因为打开键盘会调整浏览器显示的大小,所以在iOS上这绝不会失败.因为它使用CSS引擎而不是JavaScript,所以它也更快,更流畅!
Essentially: use JavaScript to determine the window height of the device, then dynamically create a CSS media query to hide the footer when the height of the window shrinks by 10 pixels. Because opening the keyboard resizes the browser display, this never fails on iOS. Because it's using the CSS engine rather than JavaScript, it's much faster and smoother too!
注意:我发现使用"visibility:hidden"比"display:none"或"position:static"的毛刺要小,但是您的行驶里程可能会有所不同.
Note: I found using 'visibility:hidden' less glitchy than 'display:none' or 'position:static', but your mileage may vary.
这篇关于键盘时jQuery Mobile隐藏固定页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!