Resize on div element

前端 未结 11 1378
既然无缘
既然无缘 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 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));
    

提交回复
热议问题