现在有这样一个需求,我需要改变两个div的大小,一个变大,另一个就需要变小。

24 篇文章 0 订阅

有一排div 吧。

然后我让第一个变大,第二个就变小。用ui的resizable方法可以实现,但是有bug。当滑动很快时有很多问题。。研究了一下午,终于在stackoverflow上找到解决方法。网址如下:http://stackoverflow.com/questions/3369045/jquery-ui-resizable-alsoresize-reverse/10247741#10247741

另外这个方法有点小问题,需要自己改一下,可能是他用的jquery版本太低。

如下是我改完的代码:

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. $.ui.plugin.add("resizable""alsoResizeReverse", {  
  2.     start: function(event, ui) {  
  3.         var self = $(this).data("uiResizable"),  
  4.         o = self.options;  
  5.         var _store = function(exp) {  
  6.             $(exp).each(function() {  
  7.                 $(this).data("resizable-alsoresize-reverse", {  
  8.                     width: parseInt($(this).width(), 10), height: parseInt($(this).height(), 10),  
  9.                     left: parseInt($(this).css('left'), 10), top: parseInt($(this).css('top'), 10)  
  10.                 });  
  11.             });  
  12.         };  
  13.         if (typeof(o.alsoResizeReverse) == 'object' && !o.alsoResizeReverse.parentNode) {  
  14.             if (o.alsoResizeReverse.length) { o.alsoResize = o.alsoResizeReverse[0];    _store(o.alsoResizeReverse); }  
  15.             else { $.each(o.alsoResizeReverse, function(exp, c) { _store(exp); }); }  
  16.         }else{  
  17.             _store(o.alsoResizeReverse);  
  18.         }  
  19.     },  
  20.     resize: function(event, ui){  
  21.         var self = $(this).data("uiResizable"), o = self.options, os = self.originalSize, op = self.originalPosition;  
  22.   
  23.         var delta = {  
  24.                 height: (self.size.height - os.height) || 0, width: (self.size.width - os.width) || 0,  
  25.                 top: (self.position.top - op.top) || 0, left: (self.position.left - op.left) || 0  
  26.             },  
  27.   
  28.             _alsoResizeReverse = function(exp, c) {  
  29.                 $(exp).each(function() {  
  30.                     var el = $(this), start = $(this).data("resizable-alsoresize-reverse"), style = {}, css = c && c.length ? c : ['width''height''top''left'];  
  31.   
  32.                     $.each(css || ['width''height''top''left'], function(i, prop) {  
  33.                         var sum = (start[prop]||0) - (delta[prop]||0);  
  34.                         if (sum && sum >= 0)  
  35.                             style[prop] = sum || null;  
  36.                     });  
  37.   
  38.                     //Opera fixing relative position  
  39.                     if (/relative/.test(el.css('position')) && false) {  
  40.                         self._revertToRelativePosition = true;  
  41.                         el.css({ position: 'absolute', top: 'auto', left: 'auto' });  
  42.                     }  
  43.   
  44.                     el.css(style);  
  45.                 });  
  46.             };  
  47.   
  48.         if (typeof(o.alsoResizeReverse) == 'object' && !o.alsoResizeReverse.nodeType) {  
  49.             $.each(o.alsoResizeReverse, function(exp, c) { _alsoResizeReverse(exp, c); });  
  50.         }else{  
  51.             _alsoResizeReverse(o.alsoResizeReverse);  
  52.         }  
  53.     },  
  54.   
  55.     stop: function(event, ui){  
  56.         var self = $(this).data("uiResizable");  
  57.   
  58.         //Opera fixing relative position  
  59.         if (self._revertToRelativePosition && $.browser.opera) {  
  60.             self._revertToRelativePosition = false;  
  61.             el.css({ position: 'relative' });  
  62.         }  
  63.         $(this).removeData("resizable-alsoresize-reverse");  
  64.     }  
  65. });  
调用如下:

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. $("#div1").resizable({  
  2.     grid: [ 20, 20 ],  
  3.     handles:'e',  
  4.     minWidth:20,  
  5.     alsoResizeReverse: "#div2",  
  6.     resize:function(event,ui){  
  7.         $(this).resizable( "option""maxWidth", div1.width()+div2.width()-20 );  
  8.     }  
  9. });  


另外发现一个bug,如果是css有box-sizing:border-box;属性,div会一次一次的缩小...找个时间改一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值