Detect Document Height Change

后端 未结 8 1089
独厮守ぢ
独厮守ぢ 2020-12-04 10:07

I\'m trying to detect when my document height changes. Once it does, I need to run a few functions to help organize my page layout.

I\'m not looking fo

8条回答
  •  天命终不由人
    2020-12-04 10:55

    vsync's answer is completely fine. Just in case you don't like to use setTimeout and you can use requestAnimationFrame (see support) and of course you are still interested.

    In the example below the body gets an extra event sizechange. And every time the height or width of the body changes it is triggered.

    (function checkForBodySizeChange() {
        var last_body_size = {
            width: document.body.clientWidth,
            height: document.body.clientHeight
        };
    
        function checkBodySizeChange()
        {
            var width_changed = last_body_size.width !== document.body.clientWidth,
                height_changed = last_body_size.height !== document.body.clientHeight;
    
    
            if(width_changed || height_changed) {
                trigger(document.body, 'sizechange');
                last_body_size = {
                    width: document.body.clientWidth,
                    height: document.body.clientHeight
                };
            }
    
            window.requestAnimationFrame(checkBodySizeChange);
        }
    
        function trigger(element, event_name, event_detail)
        {
            var evt;
    
            if(document.dispatchEvent) {
                if(typeof CustomEvent === 'undefined') {
                    var CustomEvent;
    
                    CustomEvent = function(event, params) {
                        var evt;
                        params = params || {
                            bubbles: false,
                            cancelable: false,
                            detail: undefined
                        };
                        evt = document.createEvent("CustomEvent");
                        evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
                        return evt;
                    };
    
                    CustomEvent.prototype = window.Event.prototype;
    
                    window.CustomEvent = CustomEvent;
                }
    
                evt = new CustomEvent(event_name, {"detail": event_detail});
    
                element.dispatchEvent(evt);
            }
            else {
                evt = document.createEventObject();
                evt.eventType = event_name;
                evt.eventName = event_name;
                element.fireEvent('on' + event_name, evt);
            }
        }
    
        window.requestAnimationFrame(checkBodySizeChange);
    })();
    

    A live demo

    The code can be reduced much if you have an own triggerEvent function in your project. Therefore just remove the complete function trigger and replace the line trigger(document.body, 'sizechange'); with for example in jQuery $(document.body).trigger('sizechange');.

提交回复
热议问题