How to debounce or throttle scroll events in iOS devices ( Safari )

房东的猫 提交于 2020-01-01 15:06:07

问题


I`m trying to implement infinity scroll dropdown for mobile and desktop devices. Under "infinity scroll", I mean the following - if you have 100 records, when reach the end of the scrollable container, 20 new records will be loaded and the first 20 records will hide ( same for the backward direction )

I have encountered the following problems:

Everything works perfectly in mobile Android Chrome browsers + desktop browsers except the Safari Mobile browser ( iPhone, iPad, etc.)

I have tried the following solutions:

  • Added lodash debounce/throttle function to the whole scroll handler function which didn't help, even broke the scroll everywhere

  • I tried to use iScroll but encountered the same issue as here + additional bugs from iscroll scrollTo() method.

  • requestAnimationFrame() - without any success.

Here is my example project: https://jsfiddle.net/q4nLverg/2/

The scroll Handler function code :

function scrollHandler(e) {

    var top = $(this.$refs.dropdownContainer).scrollTop();
    var difference = $(this.$refs.dropdownMenu).height() - $(this.$refs.dropdownContainer).height()

    if (top >= difference - this.scrollLimit && difference > 0) {

        this.maxRowsLimitIndex = this.maxRowsLimitIndex + this.numberOfItemsToLoad;
        this.minRowsLimitIndex = this.minRowsLimitIndex + this.numberOfItemsToLoad

        if ( this.maxRowsLimitIndex >= this.options.length ) {

            this.maxRowsLimitIndex = this.options.length
            this.minRowsLimitIndex = this.options.length - this.numberOfVisibleItems
        }
        else {
            this.scrollTo( (difference - (this.numberOfItemsToLoad * 28)) )
        }
    }
    else if ( top <= this.scrollLimit )
    {
        this.maxRowsLimitIndex = this.maxRowsLimitIndex - this.numberOfItemsToLoad;
        this.minRowsLimitIndex = this.minRowsLimitIndex - this.numberOfItemsToLoad

        if ( this.maxRowsLimitIndex <= this.numberOfVisibleItems  ) {
            this.maxRowsLimitIndex = this.numberOfVisibleItems
            this.minRowsLimitIndex = 0
        }
        else {
            this.scrollTo( (this.numberOfItemsToLoad * 28)/2)
        }

    }
}

this.scrollTo - just changes the scrollTop of the container to simulate auto scroll up or down when the new data is loaded and the old one is removed from the select

The expected results in iOS devices ( and any other ):

When the user scrolls fast to the end of the scrollable container it shouldn`t go to the end of the scrollable container, but it must continue the scroll event and load the data consequently ( just like how it works in android device ) to simulate infinity scroll functionality.


回答1:


If all of the rows has equal heights, you can try calculate the initial height of the scrolling area and use Intersection Observer to determine which rows are visible.



来源:https://stackoverflow.com/questions/54328355/how-to-debounce-or-throttle-scroll-events-in-ios-devices-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!