简体   繁体   English

如何使滚动事件一次仅接收一个请求?

[英]How to make scroll event to receive only one request at a time?

I am trying to trigger an action on scroll event when the user reaches a certain point in the web page. 当用户到达网页中的某个点时,我试图触发滚动事件操作。

So What I am facing right now is that the Scroll event fires too many times which messes the structure of the web page. 因此,我现在面对的是Scroll事件触发了太多次,这弄乱了网页的结构。 I mean too much stuffs are loaded into the site suddenly which is exactly How I do not want it. 我的意思是突然有太多的东西加载到该网站,这正是我不想要的方式。 Instead I have tried Debounce method in jQuery & also Throttle but none of them are working as because let me tell you. 相反,我在jQuery和Throttle中尝试了Debounce方法,但是它们都不起作用,因为让我告诉你。

DEBOUNCE can't keep to execute until when the user stops scrolling so if the user keeps scrolling continuously with no delays so the debounce function won't execute. 直到用户停止滚动时, DEBOUNCE才能继续执行,因此,如果用户不加延迟地连续滚动,则不会执行反跳功能。

THROTTLE as it keeps executing after every given amount of time so It can miss the scroll place to fire there. THROTTLE,因为它在每个给定的时间后都会继续执行,因此它可能会错过滚动位置以在其中射击。 So it's no help too. 因此也无济于事。

What I Want Exactly ? 我到底要什么?

I want that whenever a user scrolls to the certain area of the web page so even if the page fires too much scroll events so it should only process one event and ignore the rest and then when the user scrolls down and then he reaches the area so then again it should only process one request. 我希望每当用户滚动到网页的特定区域时,即使页面触发了太多滚动事件,因此它也应该只处理一个事件并忽略其余事件,然后当用户向下滚动然后到达该区域时,然后再次它应该只处理一个请求。

IMP : Also I want to capture fast scrolling too as right now it's not firing any events on fast scrolling only when the user scrolls too much slowly. IMP:我也想捕捉快速滚动,因为现在它仅在用户滚动太慢时才触发快速滚动。 Something like the when even during fast scrolling the user passes the certain area of the web page so the event should fire. 甚至在用户快速滚动时,用户也会通过网页的特定区域,因此事件应该触发。

Here is my Code : 这是我的代码:

    $i = 0;
    $(window).scroll(_.throttle(function() {
     // console.log($i++);
        if ($('.searchResult').length) {
            scrollTop = $(this).scrollTop();
            lastLiPos = parseInt($('.searchResult li:last-child').offset().top) - parseInt($(window).height());
            last12thRowLiPos = parseInt($('.searchResult li:nth-last-child(26)').offset().top) - parseInt($(window).height());
            if ((scrollTop > last12thRowLiPos && scrollTop < (last12thRowLiPos) + 150) &&
                $('#search_result_cont').attr('data-disable-load-result') === 'false'
            ) {
                var text = $.trim($("div#load_more").text());
                if (text !== "No More Results") {
                        $('div#load_more').trigger('click');
                    }
                if (text !== "No More Results") {
                    $('div.loading_animate').fadeIn(1000);
                } else {
                    console.log($.trim($("div#load_more").text()));
                }
            }
        }
    },100));

我认为您需要一些RXJS 过滤器魔术。我没有使用过它,但是有一个rxjs-jquery库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM