Page always autofocus on textarea and scrolltotop is not working then

前端 未结 2 434
滥情空心
滥情空心 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: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.

提交回复
热议问题