Disable scrolling when changing focus form element

2019-01-04 01:35发布

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?

10条回答
叛逆
2楼-- · 2019-01-04 02:10

If you do not want the input to be editable the answer from kpozin works fine. But as soon as you remove the readonly attribute onfocus or onclick 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?

查看更多
可以哭但决不认输i
3楼-- · 2019-01-04 02:13

To "focus" without any scroll, you may use select. select by default will select all of the text inside an input. To mimic focus behavior and have the caret at the end of the input you can use:

var myInput = document.getElementById('my-input');
var length = myInput.value.length;  // Or determine the value of length any other way
myInput.select();
myInput.setSelectionRange(length, length);
查看更多
Anthone
4楼-- · 2019-01-04 02:14

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.

查看更多
爷的心禁止访问
5楼-- · 2019-01-04 02:15

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 the scroll event and counter scroll. The event listener is than removed by jQuery.

var oldScroll = $(window).scrollTop();

$( window ).one('scroll', function() {
    $(window).scrollTop( oldScroll ); //disable scroll just once
});

$('.js-your-element').focus();

This way seems simple, effective to me and works great in all tests I have done.

Hope it helps someone.

查看更多
The star\"
6楼-- · 2019-01-04 02:17

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 to display: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.

查看更多
来,给爷笑一个
7楼-- · 2019-01-04 02:18

Ooops, the window.scrollTo suggested is kinda lousy and make things unpredictable.

Try this better solution:

jQuery('body').bind('focusin focus', function(e){
  e.preventDefault();
})

Or, just hook your input:

jQuery('input.your-input-class').bind('focusin focus', function(e){
  e.preventDefault();
})

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 by element.focus() in your code, then even the upon solution would not be functional.

So, a solution to that would be replace you focus trigger to select, i.e.

element.select()

other than element.focus()

If you don't like the element.select() approach since it will select the text inside the input element, then try to create a Range object of the input element text and collapse it if you will, sorry for no time to try that at this moment.

查看更多
登录 后发表回答