Skip to content

Focus on a input in a table will cause auto scrolling to top under jQuery 3 #362

@tommyxst

Description

@tommyxst

If we put a text input on the bottom of a long table and when focusing on it, the scroll bar will be automatically scrolled to the top of the page. This problem only happens under jQuery 3. To solve this problem, you need to add position attribute to the input.

After debugging I found that it may be caused by the changing of jQuery position() function. In the older version of jQuery (at least in jQuery 2.2.4), position() function used jQuery.offsetParent() to find element's parent who is positioned, however, in jQuery 3, they changed to use native offsetParent to locate the parent, which will cause some problem in tables. According to MDN doc, if the element is non-positioned, the nearest table, table cell or root element will be returned.

I wrote a snippet to reproduce this problem. You can check here. Although jQuery 3 uses native offsetParent in position() function but jQuery.offsetParent() can still return to .jsPane because it uses a loop to find the element whose position attribute is set so in the snippet console you can see if you comment "position: relative" in css file, the offsetParent will be different and auto scrolling will happen if focusing on the last input but if position attribute is added, it will work well.

I know it may not be a big deal to add a "position: relative" but I thought this worth to telling you and maybe some help will be made. Thank!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions