How to disable scrolling temporarily?

前端 未结 30 3540
萌比男神i
萌比男神i 2020-11-21 05:16

I\'m using the scrollTo jQuery plugin and would like to know if it is somehow possible to temporarily disable scrolling on the window element through Javascript? The reason

30条回答
  •  佛祖请我去吃肉
    2020-11-21 05:19

    How about this? (If you're using jQuery)

    var $window = $(window);
    var $body = $(window.document.body);
    
    window.onscroll = function() {
        var overlay = $body.children(".ui-widget-overlay").first();
    
        // Check if the overlay is visible and restore the previous scroll state
        if (overlay.is(":visible")) {
            var scrollPos = $body.data("scroll-pos") || { x: 0, y: 0 };
            window.scrollTo(scrollPos.x, scrollPos.y);
        }
        else {
            // Just store the scroll state
            $body.data("scroll-pos", { x: $window.scrollLeft(), y: $window.scrollTop() });
        }
    };
    

提交回复
热议问题