English 中文(简体)
移动设备文本输入焦点的滚动页面?
原标题:Scroll page on text input focus for mobile devices?

正在制作一个移动优化的网站, 其文本输入过滤您输入的列表。 它类似 : < a href="http://jquerymobile. com/ test/docs/lists/ lists/ lists- search. html" rel = “ noreferrer” > http://jquerymobile. com/ test/docs/lists/listss- search. html

对于电话来说,如果您选择了滚动页面的输入, 输入就位于页面的顶端, 那么对手机来说, 使用性将大有裨益 。 这样的话, 下面的列表中的大部分将会像您输入的一样可见 。 这有可能和/ 或任何人有这样做的经验吗? 谢谢 。

最佳回答

- 同意 - 方便易用

如果您在使用 jQuery, 这应该可以做这个把戏 :

$( #id-of-text-input ).on( focus , function() {
    document.body.scrollTop = $(this).offset().top;
});
问题回答

更好的解决办法是:

$( #id-of-text-input ).on( focus , function() {
   document.body.scrollTop += this.getBoundingClientRect().top - 10
});

因为 offsecetTop (或 .offsets ().top 如果使用 Jquery) 返回第一个位置的距离 parent , 而您需要从 document 上方的距离 。

getBoundingClientRect (). top 返回当前 viewport 位置与元素之间的距离, 也就是说, 如果您在元素下重新滚动300px, 它返回 - 300 。 因此, 使用 添加距离将会滚动到元素上。 - 10 是允许在顶部有空间的 。

$( input, textarea ).focus(function () {
    $( html, body ).animate({ scrollTop: ($( input, textarea ).offset().top - 10) }, 1);
    return false;
});




相关问题
How can I make mobile version of an existing web site?

We have a website coded with jsp-Java. Now, we want to show our website more user friendly on blackberry etc. Is there any tool or method to do this issue more easier? thanks for your answers.

Should I make my mobile application for Mobile 6 or Mobile 5?

I am going to be making a small application very soon. I am wondering would it be better to go and make it for 6.0 or should I make it for 5.0? I don t know much about both versions but I guessing ...

How do I combine @font-face and @media declarations?

I d like to build a common typography stylesheet with a very small number of selectors. As such, I d far prefer to use @media sections for the various versions rather than create different files, each ...