2010-07-30 7 views
31

Cómo hacer que la interfaz de usuario de jQuery también se pueda cambiar de tamaño en el sentido inverso.jQuery UI Resizable tambiénResize inversa

suponer en el html hay dos etiqueta div está allí, si puedo cambiar el tamaño de arriba significa que el otro tiene que cambiar el tamaño de la baja

<script> 
     $(function() { 
     $("#resizable").resizable({alsoResize: ".myiframe"}); 

    }); 
</script> 
<div id = "resizable"> 
     This is the resizable content... 
</div> 

<div class="myframe"> 
    This must resize in reverse direction... 
</div> 

lo intenté, pero no sirve de nada favor guía para resolver este

Respuesta

55

al modificar el código jQuery utiliza para implementar la opción alsoResize, podemos hacer nuestra propia opción alsoResizeReverse. A continuación, simplemente podemos utilizar esto como sigue:

$("#resizable").resizable({ 
    alsoResizeReverse: ".myframe" 
}); 

La estructura de la opción original de alsoResize se ha cambiado a través de las diferentes versiones de jQuery UI y mi código original no funciona en las versiones más recientes. Daré el código para agregar esta funcionalidad en la versión 1.8.1 y 1.11.4.

Sólo unas pocas cosas tenían que ser cambiado, como el cambio de nombre obvio alsoResize a alsoResizeReverse y restando el delta en lugar de añadir que (lo que hace que el cambio de tamaño invertida). El código original alsoResize se inicia en la línea 2200 de version 1.8.1 de jQuery UI y la línea 7922 de version 1.11.4. Puede ver los pocos cambios necesarios here.

Para añadir la funcionalidad alsoResizeReverse, agregar esto a su Javascript (Esto debe ser puesto fuera de document.ready()):

hay nuevas versiones de jQuery UI (ejemplo se basa en v1.11.4):

$.ui.plugin.add("resizable", "alsoResizeReverse", { 

    start: function() { 
     var that = $(this).resizable("instance"), 
      o = that.options; 

     $(o.alsoResizeReverse).each(function() { 
      var el = $(this); 
      el.data("ui-resizable-alsoresizeReverse", { 
       width: parseInt(el.width(), 10), height: parseInt(el.height(), 10), 
       left: parseInt(el.css("left"), 10), top: parseInt(el.css("top"), 10) 
      }); 
     }); 
    }, 

    resize: function(event, ui) { 
     var that = $(this).resizable("instance"), 
      o = that.options, 
      os = that.originalSize, 
      op = that.originalPosition, 
      delta = { 
       height: (that.size.height - os.height) || 0, 
       width: (that.size.width - os.width) || 0, 
       top: (that.position.top - op.top) || 0, 
       left: (that.position.left - op.left) || 0 
      }; 

     $(o.alsoResizeReverse).each(function() { 
      var el = $(this), start = $(this).data("ui-resizable-alsoresize-reverse"), style = {}, 
       css = el.parents(ui.originalElement[0]).length ? 
        [ "width", "height" ] : 
        [ "width", "height", "top", "left" ]; 

      $.each(css, function(i, prop) { 
       var sum = (start[prop] || 0) - (delta[prop] || 0); 
       if (sum && sum >= 0) { 
        style[prop] = sum || null; 
       } 
      }); 

      el.css(style); 
     }); 
    }, 

    stop: function() { 
     $(this).removeData("resizable-alsoresize-reverse"); 
    } 
}); 

Para versión anterior (basado en v1.8.1 - mi primera respuesta):

$.ui.plugin.add("resizable", "alsoResizeReverse", { 

    start: function(event, ui) { 

     var self = $(this).data("resizable"), o = self.options; 

     var _store = function(exp) { 
      $(exp).each(function() { 
       $(this).data("resizable-alsoresize-reverse", { 
        width: parseInt($(this).width(), 10), height: parseInt($(this).height(), 10), 
        left: parseInt($(this).css('left'), 10), top: parseInt($(this).css('top'), 10) 
       }); 
      }); 
     }; 

     if (typeof(o.alsoResizeReverse) == 'object' && !o.alsoResizeReverse.parentNode) { 
      if (o.alsoResizeReverse.length) { o.alsoResize = o.alsoResizeReverse[0]; _store(o.alsoResizeReverse); } 
      else { $.each(o.alsoResizeReverse, function(exp, c) { _store(exp); }); } 
     }else{ 
      _store(o.alsoResizeReverse); 
     } 
    }, 

    resize: function(event, ui){ 
     var self = $(this).data("resizable"), o = self.options, os = self.originalSize, op = self.originalPosition; 

     var delta = { 
      height: (self.size.height - os.height) || 0, width: (self.size.width - os.width) || 0, 
      top: (self.position.top - op.top) || 0, left: (self.position.left - op.left) || 0 
     }, 

     _alsoResizeReverse = function(exp, c) { 
      $(exp).each(function() { 
       var el = $(this), start = $(this).data("resizable-alsoresize-reverse"), style = {}, css = c && c.length ? c : ['width', 'height', 'top', 'left']; 

       $.each(css || ['width', 'height', 'top', 'left'], function(i, prop) { 
        var sum = (start[prop]||0) - (delta[prop]||0); // subtracting instead of adding 
        if (sum && sum >= 0) 
         style[prop] = sum || null; 
       }); 

       //Opera fixing relative position 
       if (/relative/.test(el.css('position')) && $.browser.opera) { 
        self._revertToRelativePosition = true; 
        el.css({ position: 'absolute', top: 'auto', left: 'auto' }); 
       } 

       el.css(style); 
      }); 
     }; 

     if (typeof(o.alsoResizeReverse) == 'object' && !o.alsoResizeReverse.nodeType) { 
      $.each(o.alsoResizeReverse, function(exp, c) { _alsoResizeReverse(exp, c); }); 
     }else{ 
      _alsoResizeReverse(o.alsoResizeReverse); 
     } 
    }, 

    stop: function(event, ui){ 
     var self = $(this).data("resizable"); 

     //Opera fixing relative position 
     if (self._revertToRelativePosition && $.browser.opera) { 
      self._revertToRelativePosition = false; 
      el.css({ position: 'relative' }); 
     } 

     $(this).removeData("resizable-alsoresize-reverse"); 
    } 
}); 

He aquí una demostración: http://jsfiddle.net/WpgzZ/

+0

Realmente amigo de Grt !! –

+0

+1, guau, ¿tienes tiempo para eso? –

+0

Solo pensé en agregar que AGREGAS esto a tu jquery-us.js, para que también funcione el tamaño, el tamaño y el tamaño. (Por si acaso alguien estaba confundido) ¡Además, buen trabajo! – Richard

4
$('#div1').resizable({ 
     handles: 'n', 
     resize: function(){ 
      $('#div2').css("height","-"+ui.size.height+"px"); 
     } 
    }).bind("resize", function() { 
     $(this).css("top", "auto"); //To handle the issue with top 
    }); 

Esto también debería funcionar para cambiar el tamaño de otro div en la dirección opuesta.

+1

Bueno en mi caso 'ui no está definido' – Kiwy

2

Incluso si el código publicado por Simen funciona muy bien, aquí es mi código para cambiar el tamaño de dos div verticalmente (y funciona bien)

<script> 
    var myheight = ($(window).height()-50); 
    var mywidth = $(window).width(); 
    $(window).load(function() { 
     $("#resizable").height(100); 
     $("#content").height(myheight-$("#resizable").height()); 
    }); 
</script> 

<div id="resizable" style="border:1px solid #333; overflow:auto">resizable</div> 
<div id="content" style="border:1px solid red; overflow:auto">content</div> 

<script> 
    $("#resizable").resizable({ 
     handles: 's', 
     maxHeight: myheight, 
     resize: function() { 
     $("#content").height(myheight-$("#resizable").height()); 
     } 
    }); 
</script> 
+1

Hola compañero, debes hacer tu pregunta en otra pregunta – Littm

+0

El código funciona bien, pero el div inferior tiene algún problema para cambiar el tamaño. Necesita una pequeña modificación, supongo, no pude entenderlo. He intentado desbordamiento: ¡diferentes estilos ...! – raja777m

8

tuve problemas para conseguir la -code "Simen Echholt" para trabajar con jQuery 1.9.1/jquery-ui (1.10.2), pero funcionó cuando intercambié "$ (this) .data (" tamaño variable ")" con "$ (this) .data ("ui-resizable") ".

+0

Pensé que podría mencionar que también tuve que cambiar '$ .browser.opera' por' $ .support.opera' cuando uso jQuery 2.0.3/jQuery UI 1.10.3. –

1

actualizada para jquery-ui 1.10.4

$.ui.plugin.add('resizable', 'alsoResizeReverse', { 

    start: function() { 
    var that = $(this).data('ui-resizable'), 
    o = that.options, 
    _store = function (exp) { 
     $(exp).each(function() { 
     var el = $(this); 
     el.data('ui-resizable-alsoresize-reverse', { 
      width: parseInt(el.width(), 10), height: parseInt(el.height(), 10), 
      left: parseInt(el.css('left'), 10), top: parseInt(el.css('top'), 10) 
     }); 
     }); 
    }; 

    if (typeof(o.alsoResizeReverse) === 'object' && !o.alsoResizeReverse.parentNode) { 
     if (o.alsoResizeReverse.length) { o.alsoResize = o.alsoResizeReverse[0]; _store(o.alsoResizeReverse); } 
     else { $.each(o.alsoResizeReverse, function(exp) { _store(exp); }); } 
    }else{ 
     _store(o.alsoResizeReverse); 
    } 
    }, 

    resize: function (event, ui) { 
    var that = $(this).data('ui-resizable'), 
    o = that.options, 
    os = that.originalSize, 
    op = that.originalPosition, 
    delta = { 
     height: (that.size.height - os.height) || 0, width: (that.size.width - os.width) || 0, 
     top: (that.position.top - op.top) || 0, left: (that.position.left - op.left) || 0 
    }, 

    _alsoResizeReverse = function(exp, c) { 
     $(exp).each(function() { 
     var el = $(this), start = $(this).data('ui-resizable-alsoresize-reverse'), style = {}, 
     css = c && c.length ? c : el.parents(ui.originalElement[0]).length ? ['width', 'height'] : ['width', 'height', 'top', 'left']; 

     $.each(css, function(i, prop) { 
      var sum = (start[prop]||0) - (delta[prop]||0); // subtracting instead of adding 
      if (sum && sum >= 0) { 
      style[prop] = sum || null; 
      } 
     }); 

     el.css(style); 
     }); 
    }; 

    if (typeof(o.alsoResizeReverse) === 'object' && !o.alsoResizeReverse.nodeType) { 
     $.each(o.alsoResizeReverse, function(exp, c) { _alsoResizeReverse(exp, c); }); 
    }else{ 
     _alsoResizeReverse(o.alsoResizeReverse); 
    } 
    }, 

    stop: function(event, ui){ 
    $(this).removeData("resizable-alsoresize-reverse"); 
    } 
}); 
5

Update para jQuery 2.1.1 y 1.11.2 jQuery UI.

$.ui.plugin.add("resizable", "alsoResizeReverse", { 
 

 
    start: function() { 
 
    var that = $(this).resizable("instance"), 
 
     o = that.options, 
 
     _store = function(exp) { 
 
     $(exp).each(function() { 
 
      var el = $(this); 
 
      el.data("ui-resizable-alsoResizeReverse", { 
 
      width: parseInt(el.width(), 10), 
 
      height: parseInt(el.height(), 10), 
 
      left: parseInt(el.css("left"), 10), 
 
      top: parseInt(el.css("top"), 10) 
 
      }); 
 
     }); 
 
     }; 
 

 
    if (typeof(o.alsoResizeReverse) === "object" && !o.alsoResizeReverse.parentNode) { 
 
     if (o.alsoResizeReverse.length) { 
 
     o.alsoResizeReverse = o.alsoResizeReverse[0]; 
 
     _store(o.alsoResizeReverse); 
 
     } else { 
 
     $.each(o.alsoResizeReverse, function(exp) { 
 
      _store(exp); 
 
     }); 
 
     } 
 
    } else { 
 
     _store(o.alsoResizeReverse); 
 
    } 
 
    }, 
 

 
    resize: function(event, ui) { 
 
    var that = $(this).resizable("instance"), 
 
     o = that.options, 
 
     os = that.originalSize, 
 
     op = that.originalPosition, 
 
     delta = { 
 
     height: (that.size.height - os.height) || 0, 
 
     width: (that.size.width - os.width) || 0, 
 
     top: (that.position.top - op.top) || 0, 
 
     left: (that.position.left - op.left) || 0 
 
     }, 
 

 
     _alsoResizeReverse = function(exp, c) { 
 
     $(exp).each(function() { 
 
      var el = $(this), 
 
      start = $(this).data("ui-resizable-alsoResizeReverse"), 
 
      style = {}, 
 
      css = c && c.length ? 
 
      c : 
 
      el.parents(ui.originalElement[0]).length ? ["width", "height"] : ["width", "height", "top", "left"]; 
 

 
      $.each(css, function(i, prop) { 
 
      var sum = (start[prop] || 0) - (delta[prop] || 0); 
 
      if (sum && sum >= 0) { 
 
       style[prop] = sum || null; 
 
      } 
 
      }); 
 

 
      el.css(style); 
 
     }); 
 
     }; 
 

 
    if (typeof(o.alsoResizeReverse) === "object" && !o.alsoResizeReverse.nodeType) { 
 
     $.each(o.alsoResizeReverse, function(exp, c) { 
 
     _alsoResizeReverse(exp, c); 
 
     }); 
 
    } else { 
 
     _alsoResizeReverse(o.alsoResizeReverse); 
 
    } 
 
    }, 
 

 
    stop: function() { 
 
    $(this).removeData("resizable-alsoResizeReverse"); 
 
    } 
 
}); 
 
$(function() { 
 

 
    $("#resizable").resizable({ 
 
    alsoResizeReverse: ".myframe" 
 
    }); 
 

 
});
#resizable, 
 
.myframe { 
 
    border: 1px solid black; 
 
    padding: 10px; 
 
    margin-bottom: 20px; 
 
    width: 50%; 
 
    height: 150px 
 
}
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/base/jquery-ui.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script> 
 
<div id="resizable"> 
 
    This is the resizable content... 
 
</div> 
 

 
<div class="myframe"> 
 
    This must resize in reverse direction... 
 
</div>

[http://jsfiddle.net/WpgzZ/1136/][1]

1

Adaptado las ideas de la marg t y Joseph Baker, - que creo que es un enfoque mucho mejor. Este método no requiere ningún hack o plugin de biblioteca Jquery para dividir un div en dos paneles. Sólo tiene que añadir una función para compensar el ancho en el evento 'redimensionar' de tamaño variable:

$("#left_pane").resizable({ 
    handles: 'e', //'East' draggable edge 
    resize: function() { 
    $("#right_pane").outerWidth($("#container").innerWidth() - $("#left_pane").outerWidth()); 
    } 
}); 

Aquí está la completa JS fiddle.