PHP+MySQL+jQuery實現隨意拖動層

來源:互聯網
上載者:User

之前我有文章:jQuery實現拖動布局並將排序結果儲存到資料庫,文中以項目為樣本,講解了實現拖動布局的方法。本文與之不同之處在於可以任意拖動頁面位置,原理就是通過拖動將拖動後層的相對位置left,top和z-index三個參數更新到資料表中對應的記錄,頁面通過CSS解析每個層不同的位置。請看具體實現步驟。

 

 

準備MySQL資料表

首先需要準備一張表notes,用來記錄層的內容,背景色和座標等資訊。

 
  1. CREATE TABLE IF NOT EXISTS `notes` (   
  2.   `id` int(11) NOT NULL auto_increment,   
  3.   `content` varchar(200) NOT NULL,   
  4.   `color` enum("yellow","blue","green") NOT NULL default "yellow",   
  5.   `xyz` varchar(100) default NULL,   
  6.   PRIMARY KEY  (`id`)   
  7. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;  

然後向表中插入幾條記錄,注意xyz欄位表示的是層的xyz座標的組合,格式為"xyz"。

drag.php

在drag.php中,需要讀取notes表中的記錄,顯示在drag.php頁面中,代碼如下:

 
  1. include_once("connect.php"); //連結資料庫   
  2. $notes = "";     
  3. $left="";     
  4. $top="";     
  5. $zindex="";     
  6. $query = mysql_query("select * from notes order by id desc");   
  7. while($row=mysql_fetch_array($query)){   
  8.     list($left,$top,$zindex) = explode("",$row["xyz"]);    
  9.     $notes.= "   
  10.     <div class="note ".$row["color"]."" style="left:".$left."px;top:".$top."px;z-index:"   
  11. .$zindex."">   
  12.         <span class="data">".$row["id"].".</span>".htmlspecialchars($row["content"])."   
  13.     </div>";   
  14. }  

然後將讀取出來的$notes現在在div中。

 
  1. <div class="demo">   
  2.     <?php echo $notes;?>   
  3. </div>  

注意,我在產生的每個DIV.note中定義了位置,即設定該div的left,top和z-index值。

CSS

 
  1. .demo{position:relative; height:500px; margin:20px; border:1px dotted #d3d3d3}   
  2. .note{width:150px; height:150px; position:absolute; margin-top:150px; padding:10px;   
  3.  overflow:hidden; cursor:move; font-size:16px; line-height:22px;}   
  4. .note span{margin:2px}   
  5.    
  6. .yellow{background-color:#FDFB8C;border:1px solid #DEDC65;}   
  7. .blue{background-color:#A6E3FC;border:1px solid #75C5E7;}   
  8. .green{background-color:#A5F88B;border:1px solid #98E775;}  

有了樣式之後,然後運行drag.php,這時就可以看到頁面中排列的的幾個層,但是還不能拖動,因為還要加入jQuery。

jQuery

首先需要載入jquery庫和jquery-ui外掛程式以及global.js。

 
  1. <script type="text/javascript" src="js/jquery.js"></script>   
  2. <script type="text/javascript" src="js/jquery-ui.min.js"></script>  

然後再global.js加入代碼:

 
  1. $(function(){   
  2.     var tmp;   
  3.        
  4.     $(".note").each(function(){   
  5.         tmp = $(this).css("z-index");   
  6.         if(tmp>zIndex) zIndex = tmp;   
  7.     })   
  8.     make_draggable($(".note"));   
  9. });   
  10. var zIndex = 0;  

global.js中,首先在$(function()裡定義了一個變數tmp,通過判斷每個div.note的z-index值,保證拖動時,該DIV在最上層(即z-index為最大值),就是不會被別的層覆蓋。

並且設定zIndex的初始值為0。

接下來,寫了一個函數make_draggable();該函數調用jquery ui外掛程式的Draggable方法,處理拖動範圍,透明度及拖動停止後執行的更新操作。

 
  1. function make_draggable(elements){   
  2.     elements.draggable({   
  3.         opacity: 0.8,   
  4.         containment:"parent",   
  5.         start:function(e,ui){ ui.helper.css("z-index",++zIndex); },   
  6.         stop:function(e,ui){   
  7.             $.get("update_position.php",{   
  8.                   x        : ui.position.left,   
  9.                   y        : ui.position.top,   
  10.                   z        : zIndex,   
  11.                   id    : parseInt(ui.helper.find("span.data").html())   
  12.             });   
  13.         }   
  14.     });   
  15. }  

當拖動時,將當前層的z-index屬性設定為最大值,即保證當前層在最上面,不被其他層覆蓋,並且設定了拖動範圍和透明度,當停止拖動時,向後台update_position.php發送一個ajax請求,傳遞的參數有x,y,z和id的值。接下來我們來看update_position.php的處理。

update_position.php儲存拖動位置

update_position.php需要做的是,擷取前台通過ajax請求發來的資料,更新資料表中相應的欄位內容。

 
  1. include_once("connect.php");   
  2. if(!is_numeric($_GET["id"])  !is_numeric($_GET["x"])  !is_numeric($_GET["y"])     
  3. !is_numeric($_GET["z"]))   
  4. die("0");   
  5.    
  6. $id = intval($_GET["id"]);   
  7. $x = intval($_GET["x"]);   
  8. $y = intval($_GET["y"]);   
  9. $z = intval($_GET["z"]);   
  10.    
  11. mysql_query("UPDATE notes SET xyz="".$x."".$y."".$z."" WHERE id=".$id);   
  12.    
  13. echo "1";  

如此完成了拖動並即時儲存的效果。

原文連結:http://www.helloweba.com/view-blog-120.html



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.