2013-03-09 22 views
9

Sử dụng giao diện người dùng jQuery, làm cách nào tôi có thể sử dụng loại tính năng Splitter như một tại http://methvin.com/splitter/3csplitter.html?Giao diện người dùng và bộ chia jQuery

Tôi yêu cầu điều này vì tôi cần 2 điều cần được triển khai trên trang của mình; Portlet (Draggable) :: http://jqueryui.com/sortable/#portlets và dọc Splitter :: http://methvin.com/splitter/3csplitter.html

Tôi không chắc chắn như thế nào tốt mã hóa thực tế nó là nếu tôi bao gồm 2 thư viện riêng (mặc dù cả hai đều jQuery dựa); như http://host.sonspring.com/portlets/ cho Portlets và http://methvin.com/splitter/3csplitter.html cho Splitter

+0

nó cung cấp thay đổi kích thước. bạn có thể thực hiện bộ chia bằng cách sử dụng. Ngoài ra, hãy kiểm tra [this out] (http://stackoverflow.com/questions/5199368/resizable-split-screen-divs-using-jquery-ui) – Dogoku

+0

Tôi cần một bộ chia tương tự như http://methvin.com/splitter/ 3csplitter.html – testndtv

Trả lời

17

Dưới đây là một ví dụ về cách tạo splitter sử dụng jQuery UI:

HTML:

<div class="wrap"> 
    <div class="resizable resizable1"></div> 
    <div class="resizable resizable2"></div> 
</div> 

Script:

$(function() 
{ 
    $(".resizable1").resizable(
    { 
     autoHide: true, 
     handles: 'e', 
     resize: function(e, ui) 
     { 
      var parent = ui.element.parent(); 
      var remainingSpace = parent.width() - ui.element.outerWidth(), 
       divTwo = ui.element.next(), 
       divTwoWidth = (remainingSpace - (divTwo.outerWidth() - divTwo.width()))/parent.width()*100+"%"; 
       divTwo.width(divTwoWidth); 
     }, 
     stop: function(e, ui) 
     { 
      var parent = ui.element.parent(); 
      ui.element.css(
      { 
       width: ui.element.width()/parent.width()*100+"%", 
      }); 
     } 
    }); 
}); 

Đây là một kịch bản phổ biến. Tôi đã thêm ít sửa đổi cho bố cục chất lỏng.

jsFiddle example

+0

Hahah. Tôi đã tìm kiếm kịch bản này TẤT CẢ NGÀY! Cảm ơn. – CIA

+0

Sử dụng Firefox 32.0.1, hộp màu xanh sẽ kết thúc tốt đẹp với dòng tiếp theo khi thay đổi kích thước.Để ngăn chặn điều đó, hãy thay thế dấu phẩy bằng các khai báo 'var' riêng lẻ bắt đầu từ' var remainingSpace = ... ' – Loathing

+0

Ngoài ra, tôi thấy rằng hai dòng css sau đây được yêu cầu để làm cho nó hoạt động:' .ui-resizable {position: tương đối;} 'và' .ui-resizable-handle {position: absolute; font-size: 0.1px; display: block; } ' – Loathing

2

Tôi đã sử dụng câu trả lời của Dmitriy làm cơ sở triển khai. Lưu ý rằng không có gì ngăn cản việc triển khai cụ thể đó tăng gấp đôi hộp giới hạn khi thanh trượt được kéo sang phải.

Tôi cần một bộ chia không thể di chuyển đơn giản trong tương lai, và ứng dụng của tôi đã sử dụng jQuery, vì vậy tôi đã đạt được điều này bằng cách thay đổi một phần mã của mình như sau:

$(function() 
{ 
    $(".resizable1").resizable(
    { 
     autoHide: false, 
     containment: "#wrap", 
     ... 

tôi cũng đã thay đổi css kiểu con trỏ (trong số những thứ khác) để ngăn chặn con trỏ thay đổi kích thước từ được hiển thị như sau:

.ui-resizable-e { 
    cursor: default; 
    ... 

Cảm ơn Dmitriy!

0

Suy nghĩ đầu tiên của tôi là: trước tiên bạn chọn tất cả các hộp trừ hộp cuối cùng. Những người có được một splitter bên phải của họ. Sau đó, khi bộ chia được di chuyển, chỉ có hai ô chạm vào bộ tách được thay đổi kích cỡ.

Đây là ví dụ bạn có thể sao chép dán; nó hoạt động như vậy. Điều này có thể được sử dụng cho bất kỳ số lượng cột nào; chỉ cần chắc chắn rằng bạn cũng thích ứng với css.

Tôi đã thêm một số nút để mở rộng 1 hộp; cũng là một nút reset.

<!DOCTYPE HTML> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>jQuery UI 4-Column Splitter</title> 
    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" /> 
    <style> 
     body { 
     padding: 0; 
     margin: 0; 
     } 
     .wrap { 
     width: 100%; 
     white-space: nowrap; 
     background-color: #c0c0c0; 
     } 
     .resizable { 
      width: 25%; 
      height: 120px; 
      display: inline-block; 
      overflow: hidden; 
     } 
     .ui-resizable-e { 
      cursor: e-resize; 
      width: 10px; 
      top: 0; 
      bottom: 0; 
      background-color: gray; 
      z-index: 10; 
     } 
    </style> 
    </head> 
    <body> 
    <div class="wrap"> 
     <div class="resizable"> HELLO </div> 
     <div class="resizable"> WORLD </div> 
     <div class="resizable"> FOO </div> 
     <div class="resizable"> BAR </div> 
    </div> 
    <div id="controls"> 
     <input type="button" value="expand 0" data-index="0" class="expand"> 
     <input type="button" value="expand 1" data-index="1" class="expand"> 
     <input type="button" value="expand 2" data-index="2" class="expand"> 
     <input type="button" value="expand 3" data-index="3" class="expand"> 
     <input type="button" value="reset" class="reset"> 
    </div> 

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script> 
    <style type="text/css" media="all"> 
    </style> 
    <script type="text/javascript"> 
     $(function() { 
     // settings 
     var minWidth = 15; 
     var splitterWidth = 10; // this sh ould match the css value 

     var splitter = $('.ui-resizable-e'); 
     var container = $('.wrap'); 
     var boxes =  $('.resizable'); 

     var subBoxWidth = 0; 


     $(".resizable:not(:last)").resizable({ 
      autoHide: false, 
      handles: 'e', 
      minWidth: minWidth, 

      start: function(event, ui) { 
      // We will take/give width from/to the next element; leaving all other divs alone. 
      subBoxWidth = ui.element.width() + ui.element.next().width(); 
      // set maximum width 
      ui.element.resizable({ 
       maxWidth: subBoxWidth - splitterWidth - minWidth 
      }); 
      }, 

      resize: function(e, ui) { 
      var index = $('.wrap').index(ui.element); 
      ui.element.next().width(
       subBoxWidth - ui.element.width() 
      ); 
      }, 

     }); 
     // collapses all the other boxes to the minimum width; expands the chosen box to what ever width is left 
     $('.expand').click(function() { 
      var index = $(this).data('index'); 
      var boxesToCollapse = boxes.length - 1; 
      var widthLeft = container.width() - boxesToCollapse * (minWidth + splitterWidth); 
      for (var i=0; i<boxes.length; i++) { 
      boxes.eq(i).width(i==index ? widthLeft : minWidth); 
      } 
     }); 
     $('.reset').click(function() { 
      boxes.removeAttr('style'); 
     }); 
     }); 
    </script> 

    </body> 
</html> 
0

Splitter component một phần của khung giao diện người dùng Shield cho phép bạn sử dụng hỗn hợp bộ chia ngang và dọc.