jQuery 和jQuery ui搭配,可以實現象igoogle那樣的拖拉拽的效果,下面以實例
講解下。
首先要用到的類庫是jquery和jquery ui了。
1
Java代碼
- <div id="column1" class="column">
- <!-- Widgets code here -->
- </div>
-
- <div id="column2" class="column">
- <!-- Widgets code here -->
- </div>
<div id="column1" class="column">
<!-- Widgets code here -->
</div>
<div id="column2" class="column">
<!-- Widgets code here -->
</div>
假設分為兩列,要從列1拖拉到列2
2 再細化代碼如下
Java代碼
- <div id="column1" class="column">
- <div class="dragbox" id="item1" >
- <h2>Handle 1
- [url=#] [/url]
- [url=#] [/url]
-
- </h2>
- <div class="dragbox-content" >
- <!-- Panel Content Here -->
- </div>
- </div>
- </div>
-
- <div id="column2" class="column">
- <div class="dragbox" id="item2" >
- <h2>Handle 2
- [url=#] [/url]
- [url=#] [/url]
-
- </h2>
- <div class="dragbox-content" >
- <!-- Panel Content Here -->
- </div>
- </div>
- </div>
<div id="column1" class="column">
<div class="dragbox" id="item1" >
<h2>Handle 1
[url=#] [/url]
[url=#] [/url]
</h2>
<div class="dragbox-content" >
<!-- Panel Content Here -->
</div>
</div>
</div>
<div id="column2" class="column">
<div class="dragbox" id="item2" >
<h2>Handle 2
[url=#] [/url]
[url=#] [/url]
</h2>
<div class="dragbox-content" >
<!-- Panel Content Here -->
</div>
</div>
</div>
可以看到,每個DIV層中,都有兩個按鈕,分別是最大化和刪除該層菜單的按鈕.
3
現在輪到jquery 出場,代碼如下:
Java代碼
- $(
- 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();
- });
$(
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();
});
下面解析下上面的代碼:
$('a.maxmin').click 和$('a.delete').click(
分別是用來處理最小化,最大化的情景,而a.delete則是處理刪除的情況(這裡沒進行處理)
之後拖拉是通過jquery的sortable的可拖拉層來進行處理,具體的請參考jquery
手冊,這裡增加了一個處理,是在拖拉移動時,用alert的方法,顯示出在第一列,第2列中,
當前的各列中div層的名稱.
4 配套的CSS
Java代碼
- .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;
- }