How to detect DIV's dimension changed?

后端 未结 25 2406
抹茶落季
抹茶落季 2020-11-22 06:14

I\'ve the following sample html, there is a DIV which has 100% width. It contains some elements. While performing windows re-sizing, the inner elements may be re-positioned,

25条回答
  •  失恋的感觉
    2020-11-22 06:46

    ResizeSensor.js is part of a huge library, but I reduced its functionality to THIS:

    function ResizeSensor(element, callback)
    {
        let zIndex = parseInt(getComputedStyle(element));
        if(isNaN(zIndex)) { zIndex = 0; };
        zIndex--;
    
        let expand = document.createElement('div');
        expand.style.position = "absolute";
        expand.style.left = "0px";
        expand.style.top = "0px";
        expand.style.right = "0px";
        expand.style.bottom = "0px";
        expand.style.overflow = "hidden";
        expand.style.zIndex = zIndex;
        expand.style.visibility = "hidden";
    
        let expandChild = document.createElement('div');
        expandChild.style.position = "absolute";
        expandChild.style.left = "0px";
        expandChild.style.top = "0px";
        expandChild.style.width = "10000000px";
        expandChild.style.height = "10000000px";
        expand.appendChild(expandChild);
    
        let shrink = document.createElement('div');
        shrink.style.position = "absolute";
        shrink.style.left = "0px";
        shrink.style.top = "0px";
        shrink.style.right = "0px";
        shrink.style.bottom = "0px";
        shrink.style.overflow = "hidden";
        shrink.style.zIndex = zIndex;
        shrink.style.visibility = "hidden";
    
        let shrinkChild = document.createElement('div');
        shrinkChild.style.position = "absolute";
        shrinkChild.style.left = "0px";
        shrinkChild.style.top = "0px";
        shrinkChild.style.width = "200%";
        shrinkChild.style.height = "200%";
        shrink.appendChild(shrinkChild);
    
        element.appendChild(expand);
        element.appendChild(shrink);
    
        function setScroll()
        {
            expand.scrollLeft = 10000000;
            expand.scrollTop = 10000000;
    
            shrink.scrollLeft = 10000000;
            shrink.scrollTop = 10000000;
        };
        setScroll();
    
        let size = element.getBoundingClientRect();
    
        let currentWidth = size.width;
        let currentHeight = size.height;
    
        let onScroll = function()
        {
            let size = element.getBoundingClientRect();
    
            let newWidth = size.width;
            let newHeight = size.height;
    
            if(newWidth != currentWidth || newHeight != currentHeight)
            {
                currentWidth = newWidth;
                currentHeight = newHeight;
    
                callback();
            }
    
            setScroll();
        };
    
        expand.addEventListener('scroll', onScroll);
        shrink.addEventListener('scroll', onScroll);
    };
    

    How to use it:

    let container = document.querySelector(".container");
    new ResizeSensor(container, function()
    {
        console.log("dimension changed:", container.clientWidth, container.clientHeight);
    });
    

提交回复
热议问题