Detect if a scroll event is triggered manually in jQuery

后端 未结 7 1081
执念已碎
执念已碎 2021-01-11 13:21

This question was already asked here a long time ago:

Detect jquery event trigger by user or call by code

But it has never been answered conclusively (or m

7条回答
  •  执笔经年
    2021-01-11 13:37

    I would suggest First of all create a javascript function

    // Attaching scroll event when document/window is loaded
        function OnFirstLoad() {
            if (document.attachEvent) {
                document.attachEvent('onscroll', scrollEvent);
            } else if (document.addEventListener) {
                document.addEventListener('scroll', scrollEvent, false);
            }
    
        }
    

    then, use either

            window.onload = OnFirstLoad;
    

    Or

        $(document).ready(function () {
             OnFirstLoad();
        });
    

    In This scroll event is a function

    function scrollEvent(e) {
            var body = document.body,
                 html = document.documentElement;
    
            var docHeight = Math.max(body.scrollHeight, body.offsetHeight,
                                   html.clientHeight, html.scrollHeight, html.offsetHeight);
            var currentScroll = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
            // implement your logic according to requirement
    
        }
    

提交回复
热议问题