Find first scrollable parent

后端 未结 8 1256
南方客
南方客 2020-12-02 11:53

I have this situation in which I need to scroll an element into the viewport. The problem is that I don\'t know which element is scrollable. For example, in Portrait the bod

相关标签:
8条回答
  • 2020-12-02 12:56

    If you are using jQuery UI you can use the scrollParent method. Have a look at the API or the source.

    From the API:

    .scrollParent(): Get the closest ancestor element that is scrollable

    This method does not accept any arguments. This method finds the nearest ancestor that allows scrolling. In other words, the .scrollParent() method finds the element that the currently selected element will scroll within.

    Note: This method only works on jQuery objects containing one element.

    If you are not using jQuery UI but are using jQuery, then there are alternative independent libraries providing similar functionality, such as:

    jquery-scrollparent

    0 讨论(0)
  • 2020-12-02 12:57

    This is the way to find what you want.

    document.addEventListener('scroll', (e) => {
        console.log(e.target.scrollingElement.scrollTop);
    })
    
    0 讨论(0)
提交回复
热议问题