How to detect DIV's dimension changed?

后端 未结 25 2360
抹茶落季
抹茶落季 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:39

    Amazingly as old as this issue is, this is still a problem in most browsers.

    As others have said, Chrome 64+ now ships with Resize Observes natively, however, the spec is still being fine tuned and Chrome is now currently (as of 2019-01-29) behind the latest edition of the specification.

    I've seen a couple of good ResizeObserver polyfills out in the wild, however, some do not follow the specification that closely and others have some calculation issues.

    I was in desperate need of this behaviour to create some responsive web components that could be used in any application. To make them work nicely they need to know their dimensions at all times, so ResizeObservers sounded ideal and I decided to create a polyfill that followed the spec as closely as possible.

    Repo: https://github.com/juggle/resize-observer

    Demo: https://codesandbox.io/s/myqzvpmmy9

    0 讨论(0)
  • 2020-11-22 06:41

    You have to bind the resize event on the window object, not on a generic html element.

    You could then use this:

    $(window).resize(function() {
        ...
    });
    

    and within the callback function you can check the new width of your div calling

    $('.a-selector').width();
    

    So, the answer to your question is no, you can't bind the resize event to a div.

    0 讨论(0)
  • 2020-11-22 06:41

    Only the window object generates a "resize" event. The only way I know of to do what you want to do is to run an interval timer that periodically checks the size.

    0 讨论(0)
  • 2020-11-22 06:43

    This is a really old question, but I figured I'd post my solution to this.

    I tried to use ResizeSensor since everyone seemed to have a pretty big crush on it. After implementing though, I realized that under the hood the Element Query requires the element in question to have position relative or absolute applied to it, which didn't work for my situation.

    I ended up handling this with an Rxjs interval instead of a straight setTimeout or requestAnimationFrame like previous implementations.

    What's nice about the observable flavor of an interval is that you get to modify the stream however any other observable can be handled. For me, a basic implementation was enough, but you could go crazy and do all sorts of merges, etc.

    In the below example, I'm tracking the inner (green) div's width changes. It has a width set to 50%, but a max-width of 200px. Dragging the slider affects the wrapper (gray) div's width. You can see that the observable only fires when the inner div's width changes, which only happens if the outer div's width is smaller than 400px.

    const { interval } = rxjs;
    const { distinctUntilChanged, map, filter } = rxjs.operators;
    
    
    const wrapper = document.getElementById('my-wrapper');
    const input = document.getElementById('width-input');
    
    
    
    
    function subscribeToResize() {
      const timer = interval(100);
      const myDiv = document.getElementById('my-div');
      const widthElement = document.getElementById('width');
      const isMax = document.getElementById('is-max');
      
      /*
        NOTE: This is the important bit here 
      */
      timer
        .pipe(
          map(() => myDiv ? Math.round(myDiv.getBoundingClientRect().width) : 0),
          distinctUntilChanged(),
          // adding a takeUntil(), here as well would allow cleanup when the component is destroyed
        )
          .subscribe((width) => {        
            widthElement.innerHTML = width;
            isMax.innerHTML = width === 200 ? 'Max width' : '50% width';
    
          });
    }
    
    function defineRange() {
      input.min = 200;
      input.max = window.innerWidth;
      input.step = 10;
      input.value = input.max - 50;
    }
    
    function bindInputToWrapper() {
      input.addEventListener('input', (event) => {
        wrapper.style.width = `${event.target.value}px`;
      });
    }
    
    defineRange();
    subscribeToResize();
    bindInputToWrapper();
    .inner {
      width: 50%;
      max-width: 200px;
    }
    
    
    
    
    /* Aesthetic styles only */
    
    .inner {
      background: #16a085;
    }
    
    .wrapper {
      background: #ecf0f1;
      color: white;
      margin-top: 24px;
    }
    
    .content {
      padding: 12px;
    }
    
    body {
      
      font-family: sans-serif;
      font-weight: bold;
    }
    <script src="https://unpkg.com/rxjs/bundles/rxjs.umd.min.js"></script>
    
    <h1>Resize Browser width</h1>
    
    <label for="width-input">Adjust the width of the wrapper element</label>
    <div>
      <input type="range" id="width-input">
    </div>
    
    <div id="my-wrapper" class="wrapper">
      <div id="my-div" class="inner">
        <div class="content">
          Width: <span id="width"></span>px
          <div id="is-max"></div>  
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-22 06:44

    You can try the code in the following snippet, it covers your needs using plain javascript. (run the code snippet and click full page link to trigger the alert that the div is resized if you want to test it.).

    Based on the fact that this is a setInterval of 100 milliseconds, i would dare to say that my PC did not find it too much CPU hungry. (0.1% of CPU was used as total for all opened tabs in Chrome at the time tested.). But then again this is for just one div, if you would like to do this for a large amount of elements then yes it could be very CPU hungry.

    You could always use a click event to stop the div-resize sniffing anyway.

    var width = 0; 
    var interval = setInterval(function(){
    if(width <= 0){
    width = document.getElementById("test_div").clientWidth;
    } 
    if(document.getElementById("test_div").clientWidth!==width) {   
      alert('resized div');
      width = document.getElementById("test_div").clientWidth;
    }    
    
    }, 100);
    <div id="test_div" style="width: 100%; min-height: 30px; border: 1px dashed pink;">
            <input type="button" value="button 1" />
            <input type="button" value="button 2" />
            <input type="button" value="button 3" />
    </div>

    You can check the fiddle also

    UPDATE

    var width = 0; 
    function myInterval() {
    var interval = setInterval(function(){
    if(width <= 0){
    width = document.getElementById("test_div").clientWidth;
    } 
    if(document.getElementById("test_div").clientWidth!==width) {   
      alert('resized');
      width = document.getElementById("test_div").clientWidth;
    }    
    
    }, 100);
    return interval;
    }
    var interval = myInterval();
    document.getElementById("clickMe").addEventListener( "click" , function() {
    if(typeof interval!=="undefined") {
    clearInterval(interval);
    alert("stopped div-resize sniffing");
    }
    });
    document.getElementById("clickMeToo").addEventListener( "click" , function() {
    myInterval();
    alert("started div-resize sniffing");
    });
    <div id="test_div" style="width: 100%; min-height: 30px; border: 1px dashed pink;">
            <input type="button" value="button 1" id="clickMe" />
            <input type="button" value="button 2" id="clickMeToo" />
            <input type="button" value="button 3" />
    </div>

    Updated Fiddle

    0 讨论(0)
  • 2020-11-22 06:44

    This is pretty much an exact copy of the top answer, but instead of a link, it's just the part of the code that matters, translated to be IMO more readable and easier to understand. A few other small changes include using cloneNode(), and not putting html into a js string. Small stuff, but you can copy and paste this as is and it will work.

    The way it works is by making two invisible divs fill the element you're watching, and then putting a trigger in each, and setting a scroll position that will lead to triggering a scroll change if the size changes.

    All real credit goes to Marc J, but if you're just looking for the relevant code, here it is:

        window.El = {}
    
        El.resizeSensorNode = undefined;
        El.initResizeNode = function() {
            var fillParent = "display: block; position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;";
            var triggerStyle = "position: absolute; left: 0; top: 0; transition: 0s;";
    
            var resizeSensor = El.resizeSensorNode = document.createElement("resizeSensor");
            resizeSensor.style = fillParent;
    
            var expandSensor = document.createElement("div");
            expandSensor.style = fillParent;
            resizeSensor.appendChild(expandSensor);
    
            var trigger = document.createElement("div");
            trigger.style = triggerStyle;
            expandSensor.appendChild(trigger);
    
            var shrinkSensor = expandSensor.cloneNode(true);
            shrinkSensor.firstChild.style = triggerStyle + " width: 200%; height: 200%";
            resizeSensor.appendChild(shrinkSensor);
        }
    
    
        El.onSizeChange = function(domNode, fn) {
            if (!domNode) return;
            if (domNode.resizeListeners) {
                domNode.resizeListeners.push(fn);
                return;
            }
    
            domNode.resizeListeners = [];
            domNode.resizeListeners.push(fn);
    
            if(El.resizeSensorNode == undefined)
                El.initResizeNode();
    
            domNode.resizeSensor = El.resizeSensorNode.cloneNode(true);
            domNode.appendChild(domNode.resizeSensor);
    
            var expand = domNode.resizeSensor.firstChild;
            var expandTrigger = expand.firstChild;
            var shrink = domNode.resizeSensor.childNodes[1];
    
            var reset = function() {
                expandTrigger.style.width = '100000px';
                expandTrigger.style.height = '100000px';
    
                expand.scrollLeft = 100000;
                expand.scrollTop = 100000;
    
                shrink.scrollLeft = 100000;
                shrink.scrollTop = 100000;
            };
    
            reset();
    
            var hasChanged, frameRequest, newWidth, newHeight;
            var lastWidth = domNode.offsetWidth;
            var lastHeight = domNode.offsetHeight;
    
    
            var onResized = function() {
                frameRequest = undefined;
    
                if (!hasChanged) return;
    
                lastWidth = newWidth;
                lastHeight = newHeight;
    
                var listeners = domNode.resizeListeners;
                for(var i = 0; listeners && i < listeners.length; i++) 
                    listeners[i]();
            };
    
            var onScroll = function() {
                newWidth = domNode.offsetWidth;
                newHeight = domNode.offsetHeight;
                hasChanged = newWidth != lastWidth || newHeight != lastHeight;
    
                if (hasChanged && !frameRequest) {
                    frameRequest = requestAnimationFrame(onResized);
                }
    
                reset();
            };
    
    
            expand.addEventListener("scroll", onScroll);
            shrink.addEventListener("scroll", onScroll);
        }
    
    0 讨论(0)
提交回复
热议问题