Resize on div element

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

提交回复
热议问题