Page always autofocus on textarea and scrolltotop is not working then

前端 未结 2 398
滥情空心
滥情空心 2020-12-22 05:45

I have a page with some checkboxes items and then a textarea element at very bottom of the page. This textarea has no autofocus as

相关标签:
2条回答
  • 2020-12-22 06:33

    Actually I was able to hack it. Basically I made the textarea readonly on page load. so it wont get focus. And then setting it to enable again after a timeout. This way textbox will be enabled by the time user starts to scroll down.

    setTimeout(function () { document.getElementById('comments').readOnly = false }, 500);

    0 讨论(0)
  • 2020-12-22 06:36

    The android scrollTop workaround would be:

    document.body.style.overflow = 'hidden';
    document.body.scrollTop = 0; 
    document.body.style.overflow = 'scroll'; // or overflow-y and/or 'auto'
    

    on some older versions of the android browser, setting scrollTop would be ignored if the overflow was not 'hidden' (which is definitely a browser bug).

    That said, I didn't test that on the body element. The bug presents itself on any DOM element with overflow which is where I discovered the workaround. I haven't encountered this bug in many years though, as the older android browsers are pretty rare and aren't supported in my current projects.

    Also, trying to fix your issue by the use of scrollTop seems wrong. You should try to fix the reason that your element is receiving focus and scrolling your window on launch in the first place. I can't help you with that without seeing a running website with the issue. If you could create a jsfiddle or equivalent that reproduces the issue I'm happy to take a look at it.

    0 讨论(0)
提交回复
热议问题