I have a web app. I'm trying to disable/prevent the scrolling that occurs when you focus on different form inputs (i.e. the scrolling that occurs as you advance through input elements in a form).
I've already disabled scrolling with this:
<script type="text/javascript">
$(document).ready(function() {
document.ontouchmove = function(e){
e.preventDefault();
}
});
</script>
To add a little more info - I have a few "slides" that my page consists of. Each are 768x1024 and they are "stacked" on the page (i.e. the page is 768x3072 [1024*3=3072]) and when you click a link I'm using scrollTo jquery plugin to scroll to the next "slide" and .focus()
on an input element.
Does anyone know how to do this?
If you do not want the input to be editable the answer from kpozin works fine. But as soon as you remove the
readonly
attributeonfocus
oronclick
the input field scrolls in focus again.What really helps and does not impact focus or ability to enter text is adding
onFocus="window.scrollTo(0, 0);"
to the input elements.Of course you have to guarantee that the relevant input is not covered by the onscreen keyboard!
See also this post: Preventing an <input> element from scrolling the screen on iPhone?
To "focus" without any scroll, you may use
select
.select
by default will select all of the text inside an input. To mimicfocus
behavior and have the caret at the end of the input you can use:Try this answer by BLSully. Give all your input elements a
readonly="readonly"
attribute initially. This will prevent Mobile Safari from scrolling to it. Remove the attribute on focus and add it again on blur for each element.To prevent the browser from scrolling to the element you move focus to I use jQuerys
one
event listener. Right before I move the focus to the element we add the listener to thescroll
event and counter scroll. The event listener is than removed by jQuery.This way seems simple, effective to me and works great in all tests I have done.
Hope it helps someone.
None of the provided solutions worked for me. Since the form that is grabbing the focus (and scrolling page initial display to said form) is below the fold, I simply added a fade-in to the form.
So I add a class name called
fade-in
, which corresponding css todisplay:none
using jQuery I then simply do
jQuery(".fade-in").fadeIn();
after document ready, and the issue is gone.This is obviously not limited to using jQuery. Vanilla js can be used for the fade.
Ooops, the
window.scrollTo
suggested is kinda lousy and make things unpredictable.Try this better solution:
Or, just hook your input:
Why? Browsers may scroll the focused form element into view by default on 'focusin' event fired (Firefox has a bug, so hook on focus instead). So, just tell them don't do that explicitly.
BTW, if the
focus
event is triggered byelement.focus()
in your code, then even the upon solution would not be functional.So, a solution to that would be replace you
focus
trigger toselect
, i.e.other than
element.focus()
If you don't like the
element.select()
approach since it willselect
the text inside the input element, then try to create aRange object
of the input element text and collapse it if you will, sorry for no time to try that at this moment.