2014-12-01 97 views
8

我正在嘗試使用jQuery可調整大小來調整我的div大小,除了在stop事件之後,jQuery UI會將我的%值還原爲pixel值之後,我的工作狀態還不錯。如何強制jQuery調整大小以使用百分比?

我有一個工作小提琴這裏:http://jsfiddle.net/totszwai/j60h38fy/5/

當你拖動current容器的第一次,它會正確地計算所有的價值,但stop事件發生後,jQuery的用戶界面將更新%和變化它回到像素...所以下一次再拖動它時,%會丟失。

你如何強制jQuery將寬度值設置爲%?我可以在技術上使用setTimeout之類的東西,但這太難看了。

我不想要一個解決方案來處理像素中的div,因爲我可以在技術上在我的設置中添加n-divs,並且帶有%的代碼應該可以與n-div一起工作。

如果你看看我的neighbor div,%保留在那裏,只有current被覆蓋。我也嘗試過直接使用ui.element以及將ui.size.width設置爲我的%,但沒有任何一項工作。

更新:一種解決方法是將數據存儲每次在stop事件,從來沒有在start檢查一遍,但是,這仍然沒有解決的問題,即stop返回不正確的像素值。查看更新的jsFiddle:http://jsfiddle.net/totszwai/j60h38fy/6/

如果只有jQuery resizable需要我的%值,則所有內容都按預期工作。

已解決:好的,我接受了apaul34208的回答,因爲我曾問過如何使用%。但是,爲了實際解決我最初想要解決的問題,我最終使用了像素。請參閱我發佈below的答案,如果該答案對您有幫助,請改爲取而代之。

回答

7

我遇到了同樣的問題,所選解決方案對我無效,因爲我需要將其推廣到任意數量的列。 此外codenamezero的答案沒有處理這樣一個事實,即原始問題需要使用寬度的百分比。

對於我的情況使用百分比的寬度是很重要的,因爲我正在實現一個表編輯器,並且保存的表必須根據呈現的位置動態更改大小。

所以我想出了這個解決方案,它以codenamezero的解決方案,但在改變後的寬度停止的工作原理類似於被設定爲百分比:
http://jsfiddle.net/genpsire/4mywcm1x/14/

$(document).ready(function() { 

    var container = $(".wrapper"); 
    var numberOfCol = 3; 
    $(".test").css('width', 100/numberOfCol +'%'); 

    var sibTotalWidth; 
    $(".test").resizable({ 
     handles: 'e', 
     start: function(event, ui){ 
      sibTotalWidth = ui.originalSize.width + ui.originalElement.next().outerWidth(); 
     }, 
     stop: function(event, ui){  
      var cellPercentWidth=100 * ui.originalElement.outerWidth()/ container.innerWidth(); 
      ui.originalElement.css('width', cellPercentWidth + '%'); 
      var nextCell = ui.originalElement.next(); 
      var nextPercentWidth=100 * nextCell.outerWidth()/container.innerWidth(); 
      nextCell.css('width', nextPercentWidth + '%'); 
     }, 
     resize: function(event, ui){ 
      ui.originalElement.next().width(sibTotalWidth - ui.size.width); 
     } 
    }); 
}); 

請選擇該解決方案,以便其他人不花他們的早晨拉着頭髮試圖將apaul34208的解決方案概括爲n列的情況。 (像我這樣做)

+0

這一個比apaul的好,但是,你不應該使用

... – codenamezero2015-01-26 15:21:59

+0

容器不一定是一個表,所以這將與divs一起工作。如果您使用

來顯示錶格數據,那麼您確定。我同意
不應該用於格式化頁面。在我的情況下,我們正在顯示錶格數據。 – genspire2015-01-27 20:48:01

+0

謝謝,很好的解決方案!它失敗了shift-drag測試(用於縱橫比縮放),但使用'.resizable()'的所有其他答案也是如此(因爲在按住shift的同時調整大小時高度發生了變化)。但是,通過這樣做很容易修復在'resizable'和'stop'事件中:'$(this).css('height','')'。我分叉你的小提琴,並在這裏添加了幾個修復和改進:http:// jsfiddle。net/8auwgazu/ – nothingisnecessary 2016-03-09 17:40:25

0

,我不得不刪除鬼和動畫和調整你的CSS了一下,display: flex;似乎導致意想不到的結果,但我想我可能有一個簡單的解決方案無意中發現:

Working Example

$(document).ready(function() { 

    $(".test").resizable({ 
    handles: 'e', 
    resize: function() { 
     $('.test:first-of-type').css('width', $('.test:first-of-type').outerWidth() * 100/$(window).innerWidth() + '%'); 
     $('.test:nth-of-type(2)').css('width', 100 - ($('.test:first-of-type').outerWidth() * 100/$(window).innerWidth()) + '%'); 

    } 

    }); 
}); 
* { 
    -moz-box-sizing: border-box; 
    box-sizing: border-box; 
} 
html, 
body { 
    height: 100%; 
    width: 100%; 
    padding: 0; 
    margin: 0; 
} 
.wrapper { 
    height: 100%; 
    width: 100%; 
} 
.test { 
    float: left; 
    border: 3px solid red; 
} 
<div class="wrapper"> 
    <div class="test" style="width: 50%; height: 200px;">current</div> 
    <div class="test" style="width: 50%; height: 200px;">neighbor</div> 
</div> 
1

在敲擊了一個早晨之後,我終於找到了完美的解決方案。它實際上比我想象的簡單得多,只是因爲我過於複雜的事情。

我試圖獲得它的動態大小ui.helper,並試圖在stop上操縱鄰居元素。但我真正關心的是this元素的寬度和近鄰元素的寬度,所以,我最終會做以下幾點:

start: function (event, ui) { 
     // just remember the total width of self + neighbor 
     this.widthWithNeighbor = 
      ui.originalSize.width + ui.element.next().outerWidth(); 
    }, 
    resize: function (event, ui) { 
     // then simply subtract it! 
     ui.element.next().width(this.widthWithNeighbor - ui.size.width); 
    }, 
    stop: function(event, ui) { 
     // clean up, is this needed? 
     delete this.widthWithNeighbor; 
    } 

該解決方案適用於N + 1倍的DIV並排坐着。如果您用高度翻轉寬度,它也將與垂直堆疊的DIV一起使用。:)

乾杯!

Working jsFiddle

注:鬼和動畫從jQuery的UI選項仍然給奇怪的問題如前。這是從jQuery 2.0.2和jQuery-UI 1.10.3開始的,希望他們能儘快修補它。

+0

我還以爲你要去跟%的工作可調整大小的元素,我誤解了問題?在Firefox和Chrome中,你的答案似乎都會返回px的寬度。 – apaul 2014-12-02 21:03:09

+0

嗯...是的,你是對的。本來我想讓它與%一起工作,因爲我找不到適當調整DIV的方法。我想我可以接受你的答案,如果你可以修改它與n + 1 DIVs一起工作? – codenamezero 2014-12-03 13:58:07

+0

今天晚些時候我會再試一次吧 – apaul 2014-12-03 14:02:13

0

我知道我的答案不是一個「良好的做法」,因爲它需要你定製一個經常更新的庫,但它解決了我的問題: 更改jquery-ui中的「resize」函數.js文件根據需要操作寬度。 在我來說,我需要調整大小完全忽略寬度,所以我註釋掉「寬度」的變化:

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.alsoResize).each(function() { 
       var el = $(this), start = $(this).data("ui-resizable-alsoresize"), 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); 
      }); 
    }, 
+0

是的,它會工作,但我會從我的經驗告訴你,黑客入侵第三方庫是未來保持和升級的屁股。您也可能會引入一些不明確的錯誤,這些錯誤會影響其他鬆散相關的代碼,使其成爲追蹤和修補/修復/破解的噩夢。 – codenamezero 2016-08-10 12:38:17

相關問題