How to resize ONLY horizontally or vertically with jQuery UI Resizable?

前端 未结 7 2258
长情又很酷
长情又很酷 2020-12-12 23:29

The only solution I\'ve found is to set the max and min height or width with the current value.

Example:

foo.resizable({ 
  maxHeight: foo.height(),          


        
相关标签:
7条回答
  • 2020-12-12 23:54

    For me solutions with both handles and resize handler worked fine, so user see handle but can resize only horizontally, similar solution should work for vertical. Without bottom right handler user might be unaware that he can resize element.

    When using ui.size.height = ui.originalSize.height; it will not work properly if element changed it's size from initial state.

    foo.resizable({
        // Handles left right and bottom right corner
        handles: 'e, w, se',
        // Remove height style
        resize: function(event, ui) {
            $(this).css("height", '');
        }
    });
    

    For better performance $(this) could be removed:

    var foo = jQuery('#foo');
    foo.resizable({
        // Handles left right and bottom right corner
        handles: 'e, w, se',
        // Remove height style
        resize: function(event, ui) {
            foo.css("height", '');
        }
    });
    
    0 讨论(0)
  • 2020-12-12 23:57

    While the poster was mainly asking for a horizontal-only resize, the question does ask for vertical, too.

    The vertical case has a special issue: You might have set a relative width (e.g. 50%) that you want to keep even when the browser window is resized. However jQuery UI sets an absolute width in px once you first resize the element and the relative width is lost.

    If found the following code to work for this use case:

    $("#resizable").resizable({
        handles: 's',
        stop: function(event, ui) {
            $(this).css("width", '');
       }
    });
    

    See also http://jsfiddle.net/cburgmer/wExtX/ and jQuery UI resizable : auto height when using east handle alone

    0 讨论(0)
  • 2020-12-13 00:02

    I wanted to allow re-size the width when browser re-sizes, but not when user changes the size of element, this worked fine:

    foo.first().resizable({
        resize: function(event, ui) { 
        ui.element.css('width','auto');
        },
    }); 
    
    0 讨论(0)
  • 2020-12-13 00:05

    The solution is to configure your resizable so it cancels changes of the dimension you don't want.

    here is an example of vertically only resizable:

    foo.resizable({
      resize: function(event, ui) { 
        ui.size.width = ui.originalSize.width;
      }      
    }); 
    
    0 讨论(0)
  • 2020-12-13 00:08

    You could set the resize handles option to only show on the left and right (or east/west), like this:

    foo.resizable({
        handles: 'e, w'
    });​
    

    You can give it a try here.

    0 讨论(0)
  • 2020-12-13 00:12

    A very simple solution:

    $( ".selector" ).resizable({ grid: [1, 10000] }); // horizontal
    $( ".selector" ).resizable({ grid: [10000, 1] }); // vertical
    

    This works for draggable() as well. Example: http://jsfiddle.net/xCepm/

    0 讨论(0)
提交回复
热议问题