Resize on div element

前端 未结 11 1337
既然无缘
既然无缘 2020-11-28 08:31

jQuery has the resize() - event, but it just work with window.

jQuery(window).resize(function() { /* What ever */ });

This wor

相关标签:
11条回答
  • 2020-11-28 08:46

    Only window is supported yes but you could use a plugin for it: http://benalman.com/projects/jquery-resize-plugin/

    0 讨论(0)
  • 2020-11-28 08:47

    I've created jquery plugin jquery.resize it use resizeObserver if supported or solution based on marcj/css-element-queries scroll event, no setTimeout/setInterval.

    You use just

     jQuery('div').on('resize', function() { /* What ever */ });
    

    or as resizer plugin

    jQuery('div').resizer(function() { /* What ever */ });
    

    I've created this for jQuery Terminal and extracted into separated repo and npm package, but in a mean time I switched to hidden iframe because I had problems with resize if element was inside iframe. I may update the plugin accordingly. You can look at iframe based resizer plugin in jQuery Terminal source code.

    EDIT: new version use iframe and resize on it's window object because the previous solutions was not working when page was inside iframe.

    EDIT2: Because the fallback use iframe you can't use it with form controls or images, you need to add it to the wrapper element.

    EDIT3:: there is better solution using resizeObserver polyfill that use mutation observer (if resizeObserver is not supported) and work even in IE. It also have TypeScript typings.

    0 讨论(0)
  • 2020-11-28 08:47

    You can change your text or Content or Attribute depend on Screen size: HTML:

    <p class="change">Frequently Asked Questions (FAQ)</p>
    <p class="change">Frequently Asked Questions </p>
    

    Javascript:

    <script>
    const changeText = document.querySelector('.change');
    function resize() {
      if((window.innerWidth<500)&&(changeText.textContent="Frequently Asked Questions (FAQ)")){
        changeText.textContent="FAQ";
      } else {
        changeText.textContent="Frequently Asked Questions (FAQ)";
      }
    }
    window.onresize = resize;
    </script>
    
    0 讨论(0)
  • 2020-11-28 08:53

    There is a really nice, easy to use, lightweight (uses native browser events for detection) plugin for both basic JavaScript and for jQuery that was released this year. It performs perfectly:

    https://github.com/sdecima/javascript-detect-element-resize

    0 讨论(0)
  • 2020-11-28 08:55

    DIV does not fire a resize event, so you won't be able to do exactly what you've coded, but you could look into monitoring DOM properties.

    If you are actually working with something like resizables, and that is the only way for a div to change in size, then your resize plugin will probably be implementing a callback of its own.

    0 讨论(0)
  • 2020-11-28 08:55

    For a google maps integration I was looking for a way to detect when a div has changed in size. Since google maps always require proper dimensions e.g. width and height in order to render properly.

    The solution I came up with is a delegation of an event, in my case a tab click. This could be a window resize of course, the idea remains the same:

    if (parent.is(':visible')) {
        w = parent.outerWidth(false);
        h = w * mapRatio /*9/16*/;
        this.map.css({ width: w, height: h });
    } else {
        this.map.closest('.tab').one('click', function() {
            this.activate();
        }.bind(this));
    }
    

    this.map in this case is my map div. Since my parent is invisible on load, the computed width and height are 0 or don't match. By using .bind(this) I can delegate the script execution (this.activate) to an event (click).

    Now I'm confident the same applies for resize events.

    $(window).one('resize', function() {
        this.div.css({ /*whatever*/ });
    }.bind(this));
    

    Hope it helps anyone!

    0 讨论(0)
提交回复
热议问题