简体   繁体   English

防止iPhone Web应用程序滚动输入字段的焦点

[英]Prevent iPhone web-app from scrolling on focus of an input field

When you click on an input field on a website in the iPhone Safari it automatically scrolls the content the way the input will be centered. 当您单击iPhone Safari中网站上的输入字段时,它会自动按输入中心的方式滚动内容。 I would like to prevent Safari of doing that and found several solutions, one of them: Preventing an <input> element from scrolling the screen on iPhone? 我想阻止Safari这样做,并找到了几个解决方案,其中之一: 防止<input>元素在iPhone上滚动屏幕? where after focusing we use jQuery.animate() to move back where we were. 聚焦后我们使用jQuery.animate()移回原来的位置。

Unfortunately this only works as long as I call the website through Safari. 不幸的是,只有通过Safari呼叫网站,这才有效。 As soon as I add the website to the homescreen, the automatic scrolling on a focus appears again. 只要我将网站添加到主屏幕,就会再次显示焦点上的自动滚动。 Now when I write the position of the body/html-element before and after correcting it (with the methode mentioned above) into the console everything seems as if it works correctly: The web-app think it has scrolled back to the place it should even if I haven't noticed anything visually. 现在当我在修改它(使用上面提到的方法)之前和之后写入body / html-element的位置进入控制台时,一切看起来好像它正常工作:web-app认为它已经滚动回到它应该的位置即使我没有在视觉上注意到任何东西。

Any suggestion? 有什么建议吗?

Fixed the auto-centering on input click for me in React (JSX) with: 修复了React(JSX)中输入点击的自动居中:

<input onMouseDown={ev => ev.preventDefault()} />

It should work with any way of attaching an event handler, eg addEventListener 它应该适用于任何附加事件处理程序的方法,例如addEventListener

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

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