Resize on div element

前端 未结 11 1338
既然无缘
既然无缘 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:55

    Just try this func (it may work not only for divs):

    function resized(elem, func = function(){}, args = []){
        elem = jQuery(elem);
        func = func.bind(elem);
        var h = -1, w = -1;
        setInterval(function(){
            if (elem.height() != h || elem.width() != w){
                h = elem.height();
                w = elem.width();
                func.apply(null, args);
            }
        }, 100);
    }
    

    You can use it like this

    resized(/*element*/ '.advs-columns-main > div > div', /*callback*/ function(a){
        console.log(a);
        console.log(this); //for accessing the jQuery element you passed
    }, /*callback arguments in array*/ ['I\'m the first arg named "a"!']);
    

    UPDATE: You can also use more progressive watcher (it can work for any objects, not only DOM elements):

    function changed(elem, propsToBeChanged, func = function(){}, args = [], interval = 100){
            func = func.bind(elem);
            var currentVal = {call: {}, std: {}};
            $.each(propsToBeChanged, (property, needCall)=>{
                needCall = needCall ? 'call' : 'std';
                currentVal[needCall][property] = new Boolean(); // is a minimal and unique value, its equivalent comparsion with each other will always return false
            });
            setInterval(function(){
                $.each(propsToBeChanged, (property, needCall)=>{
                    try{
                        var currVal = needCall ? elem[property]() : elem[property];
                    } catch (e){ // elem[property] is not a function anymore
                        var currVal = elem[property];
                        needCall = false;
                        propsToBeChanged[property] = false;
                    }
                    needCall = needCall ? 'call' : 'std';
                    if (currVal !== currentVal[needCall][property]){
                        currentVal[needCall][property] = currVal;
                        func.apply(null, args);
                    }
                });
            }, interval);
        }
    

    Just try it:

    var b = '2',
        a = {foo: 'bar', ext: ()=>{return b}};
    changed(a, {
    // prop name || do eval like a function?
        foo:        false,
        ext:        true
    }, ()=>{console.log('changed')})
    

    It will log 'changed' every time when you change b, a.foo or a.ext directly

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

    I was only interested for a trigger when a width of an element was changed (I don' care about height), so I created a jquery event that does exactly that, using an invisible iframe element.

    $.event.special.widthChanged = {
      remove: function() {
          $(this).children('iframe.width-changed').remove();
      },
      add: function () {
          var elm = $(this);
          var iframe = elm.children('iframe.width-changed');
          if (!iframe.length) {
              iframe = $('<iframe/>').addClass('width-changed').prependTo(this);
          }
          var oldWidth = elm.width();
          function elmResized() {
              var width = elm.width();
              if (oldWidth != width) {
                  elm.trigger('widthChanged', [width, oldWidth]);
                  oldWidth = width;
              }
          }
    
          var timer = 0;
          var ielm = iframe[0];
          (ielm.contentWindow || ielm).onresize = function() {
              clearTimeout(timer);
              timer = setTimeout(elmResized, 20);
          };
      }
    }
    

    It requires the following css :

    iframe.width-changed {
        width: 100%;
        display: block;
        border: 0;
        height: 0;
        margin: 0;
    }
    

    You can see it in action here widthChanged fiddle

    0 讨论(0)
  • 2020-11-28 09:00
    // this is a Jquery plugin function that fires an event when the size of an element is changed
    // usage: $().sizeChanged(function(){})
    
    (function ($) {
    
    $.fn.sizeChanged = function (handleFunction) {
        var element = this;
        var lastWidth = element.width();
        var lastHeight = element.height();
    
        setInterval(function () {
            if (lastWidth === element.width()&&lastHeight === element.height())
                return;
            if (typeof (handleFunction) == 'function') {
                handleFunction({ width: lastWidth, height: lastHeight },
                               { width: element.width(), height: element.height() });
                lastWidth = element.width();
                lastHeight = element.height();
            }
        }, 100);
    
    
        return element;
    };
    
    }(jQuery));
    
    0 讨论(0)
  • 2020-11-28 09:06

    what about this:

    divH = divW = 0;
    jQuery(document).ready(function(){
        divW = jQuery("div").width();
        divH = jQuery("div").height();
    });
    function checkResize(){
        var w = jQuery("div").width();
        var h = jQuery("div").height();
        if (w != divW || h != divH) {
            /*what ever*/
            divH = h;
            divW = w;
        }
    }
    jQuery(window).resize(checkResize);
    var timer = setInterval(checkResize, 1000);
    

    BTW I suggest you to add an id to the div and change the $("div") to $("#yourid"), it's gonna be faster, and it won't break when later you add other divs

    0 讨论(0)
  • 2020-11-28 09:11

    If you just want to resize the div itself you need to specify that in css style. You need to add overflow and resize property.

    Below is my code snippet

    #div1 {
            width: 90%;
            height: 350px;
            padding: 10px;
            border: 1px solid #aaaaaa;
            overflow: auto;
            resize: both;
        }
    
    <div id="div1">
    
    </div>
    
    0 讨论(0)
提交回复
热议问题