2011-09-21 278 views

回答

1
var w = prompt('width?')*1; 
var h = prompt('height?')*1; 
$('#resizable').css('width', w); 
$('#resizable').css('height', h); 

您不需要使用jqueryui的可調整大小以便在腳本中調整它的大小。 jQuery .css()方法適用於任何DOM元素。您也可以animate({width: 500},1000)

+0

我試過了。 '$('#resizable')'小部件確實被調整了大小,但是在'$('#resizable')'和'jQuery UI注入的$('。ui-wrapper')'之間留下了空白區域。當然我知道我也可以調整'$('。ui-wrapper')'的大小。我只是想知道是否有更優雅的方式來做到這一點。這不是常見的情況嗎? – user949952

+0

當我嘗試在此頁面的控制檯上運行此代碼時,我不明白:http://jqueryui.com/demos/resizable/ ...沒有空白。但是,是的,調整任何你需要調整大小。雖然jQuery可調整大小的setSize()方法的缺失確實讓我覺得有點奇怪。 –

0

我用這個下面的代碼:

function resize(target, new_width, new_height){ 
    var $wrapper = $(target).resizable('widget'), 
     $element = $wrapper.find('.ui-resizable'), 
     dx = $element.width() - new_width, 
     dy = $element.height() - new_height; 

    $element.width(new_width); 
    $wrapper.width($wrapper.width() - dx); 
    $element.height(new_height); 
    $wrapper.height($wrapper.height() - dy); 
} 
0

可以擴展原有的可調整大小的小部件一樣如下:

(function($) { 
    $.widget("custom.mysizable", $.ui.resizable, { 

     options: { 
      x: 0, 
      y: 0 
     }, 

     _create: function() { 
      $.ui.resizable.prototype._create.call(this); 
      this.options['x'] = $(this.element).width(); 
      this.options['y'] = $(this.element).height(); 
     }, 

     _resize: function(x, y) { 
      if(x != null){ 
       $(this.element).width(x); 
      } 
      if(y != null){ 
       $(this.element).height(y); 
      } 
      this._proportionallyResize(); 
     }, 

     _setOption: function(key, value) { 
      this.options[ key ] = value; 
      this._update(); 
     }, 

     _update: function() { 
      this._resize(
       this.options['x'], 
       this.options['y'] 
      ); 
     }, 

     _destroy: function() { 
      $.ui.resizable.prototype._destroy.call(this); 
     } 
    }); 
})(jQuery); 

要使用它

$('#myElement').mysizable('option', 'y', 100); 
$('#myElement').mysizable('option', 'x', 100); 
相關問題