Search code examples
jqueryjquery-pluginswindow-resize

updating jquery plugin settings on window resize


I have a jquery plugin I'm writing that has default values, and of course the ability for folks to define their own values for those variables.

var settings = $.extend({
  width:      $(window).width(),
  height:     $(window).height(),
  rotate_btn: $(".rotate_js")
}, options );

Here are what I have for the current defaults. What I'm having trouble with is I'd like for these values to be updated on the window resize, either the default value or the value defined by the person using the plugin. How would you go about doing that so it happens within the plugin?

Thanks in advance for any help with this.


Solution

  • var settings = $.extend({
        width: $(window).width(),
        height: $(window).height(),
        rotate_btn: $(".rotate_js")
    }, options );
    
    $(window).on('resize.myCustomEventName', function(event) {
            $.extend(settings, {
                width: $(window).width(),
                ...
            });
    });