Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

jQuery UI 實作 iGoogle 式可拖拉面板

jQuery 搭配 jQuery UI 的 sortable,可以做出 iGoogle 那樣的門檻效果:面板能跨欄拖曳、能折疊、能刪除,放開滑鼠時還能把新的排列順序回傳給伺服器保存。下面用一個雙欄範例走完整流程。

一、HTML 骨架

先準備兩欄容器,每欄裡放若干面板:

<div id="column1" class="column">
  <!-- Widgets code here -->
</div>
<div id="column2" class="column">
  <!-- Widgets code here -->
</div>

二、細化每個面板

<div id="column1" class="column">
  <div class="dragbox" id="item1">
    <h2>Handle 1 <a class="opIcons maxmin" href="#"></a>
         <a class="opIcons delete" href="#"></a></h2>
    <div class="dragbox-content">
      <!-- Panel Content Here -->
    </div>
  </div>
</div>

<div id="column2" class="column">
  <div class="dragbox" id="item2">
    <h2>Handle 2 <a class="opIcons maxmin" href="#"></a>
         <a class="opIcons delete" href="#"></a></h2>
    <div class="dragbox-content">
      <!-- Panel Content Here -->
    </div>
  </div>
</div>

結構上刻意分了三層:外層 .column 是放物件的容器,.dragbox 是可拖曳的單位,h2 充當拖曳把手(handle),.dragbox-content 則是折疊的內容區。標題欄上的兩個小圖示分別負責最大化/最小化與刪除。

三、jQuery 上場

$(function(){
  // 最大化 / 最小化:切換內容區顯示
  $('a.maxmin').click(function(){
    $(this).parent().siblings('.dragbox-content').toggle();
  });

  // 刪除面板
  $('a.delete').click(function(){
    var sel = confirm('do you want to delete the widget?');
    if(sel){
      // del code here
    }
  });

  $('.column').sortable({
    connectWith: '.column',      // 允許跨欄拖曳
    handle: 'h2',                // 只有標題欄能拖
    cursor: 'move',
    placeholder: 'placeholder',  // 佔位樣式
    forcePlaceholderSize: true,
    opacity: 0.4,
    stop: function(event, ui){
      $(ui.item).find('h2').click();
      var sortorder = '';
      $('.column').each(function(){
        var itemorder = $(this).sortable('toArray');
        var columnId  = $(this).attr('id');
        sortorder += columnId + '=' + itemorder.toString() + '&';
      });
      sortorder = sortorder.substring(0, sortorder.length - 1);
      alert('SortOrder: ' + sortorder);
    }
  }).disableSelection();
});

幾個關鍵參數:connectWith 讓多個 .column 互相打通,這才是跨欄拖曳的開關;handle: 'h2' 限定只能抓標題;placeholder 是拖曳過程中顯示的落位提示;disableSelection() 則避免拖曳時把文字選取起來。

stop 回呼是最實用的一段:sortable('toArray') 會依目前順序回傳該欄所有子元素的 id 陣列,我們把它拼成 column1=item2,item1&column2=item3 這樣的查詢串,丟給後端保存即可(範例用 alert 代替,實際上換成 $.post)。

四、配套 CSS

.column{
  width: 49%;
  margin-right: .5%;
  min-height: 300px;
  background: #fff;
  float: left;
}
.column .dragbox{
  margin: 5px 2px 20px;
  background: #fff;
  position: relative;
  border: 1px solid #946553;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
}
.column .dragbox h2{
  margin: 0;
  font-size: 12px;
  background: #946553;
  color: #fff;
  border-bottom: 1px solid #946553;
  font-family: Verdana;
  cursor: move;
  padding: 5px;
}
.dragbox-content{
  background: #fff;
  min-height: 100px;
  margin: 5px;
  font-family: 'Lucida Grande', Verdana;
  font-size: 0.8em;
  line-height: 1.5em;
}
.column .placeholder{
  background: #EED5B7;
  border: 1px dashed #946553;
}
.opIcons{
  background-image: url('iconSpirite.gif') !important;
  background-repeat: no-repeat;
  float: right;
  height: 13px;
  width: 13px;
  margin: 0px 2px;
}
.maxmin{ background-position: -26px 1px; }
.delete{ background-position: -269px center; padding-top: 1px; }

樣式上有兩處是為拖曳服務的:標題欄的 cursor: move 給出可拖曳的視覺暗示;.placeholder 的虛線框讓使用者預覽落點。圖示則用一張精靈圖(sprite)透過 background-position 定位,省下多次請求。

五、可延伸的方向

這套結構很容易擴充:把 sortorder 換成一次非同步提交就能持久化佈局;在 receive 回呼裡依 ui.item 的 id 觸發懶加載,可以延後載入面板內容;若要支援觸控裝置,再疊上 jQuery UI Touch Punch 這類兼容層即可。

Copyright © Linux教程網 All Rights Reserved