If you want to drag layers on the page, you can use the Draggable method of the jquery UI, how do you save the position of the dragged layer? This article will give an answer. This paper explains how to use php+mysql+jquery to drag the layer and save the drag position instantly.
The principle of this article is to update the corresponding records in the datasheet by dragging the relative position of the dragged Left,top and z-index three parameters, and the page resolves the different positions of each layer through CSS. See the specific implementation steps.
prepare MySQL datasheet
First you need to prepare a table of notes to record the contents of the layer, background color and coordinates, and other information.
CREATE TABLE IF not EXISTS ' notes ' (
' id ' int (one) not null auto_increment,
' content ' varchar ' is not NULL,
' C Olor ' enum (' Yellow ', ' blue ', ' green ') not null default ' yellow ',
' xyz ' varchar (m) default null,
PRIMARY KEY (' ID ')
Engine=myisam DEFAULT Charset=utf8;
Then insert several records into the table, noting that the XYZ field represents a combination of the XYZ coordinates of the layer, in the form of "x|y|z".
drag.php
In drag.php, you need to read the records in the Notes table, displayed in the Drag.php page, with the following code:
Include_once (' connect.php '); Link database
$notes = ';
$left = ';
$top = ';
$zindex = ';
$query = mysql_query ("SELECT * from Notes ORDER BY id desc");
while ($row =mysql_fetch_array ($query)) {
list ($left, $top, $zindex) = Explode (' | ', $row [' XYZ ']);
$notes. = '
<div class= '. $row [' Color ']. ' style= left: '. $left. ' Px;top: '. $top. ' Px;z-index: '
. $ ZIndex. ' " >
<span class= "Data" > ". $row [' id ']. </span> '. Htmlspecialchars ($row [' content ']). '
</div> ';
}
Then the read out $notes is now in the Div.
<div class= "Demo" >
<?php echo $notes;? >
</div>
Notice that I have defined the location in each div.note generated, that is, to set the Left,top and Z-index values of the div.
CSS
Demo{position:relative height:500px; margin:20px border:1px dotted #d3d3d3} note{width:150px; height:150px;
Position:absolute; margin-top:150px; padding:10px;
Overflow:hidden; Cursor:move; font-size:16px; LINE-HEIGHT:22PX}
. Note span{margin:2px}
. Yellow{background-color: #FDFB8C; border:1px solid #DEDC65;
Blue{background-color: #A6E3FC; border:1px solid #75C5E7;
Green{background-color: #A5F88B; border:1px solid #98E775;}
With the style, and then run drag.php, you can see several layers arranged in the page, but you can't drag them because you want to add jquery.
JQuery
First you need to load the jquery library and the Jquery-ui plug-in as well as the global.js.
<script type= "Text/javascript" src= "js/jquery.js" ></script>
<script type= "Text/javascript" src = "Js/jquery-ui.min.js" ></script>
And then global.js add the code:
$ (function () {
var tmp;
$ ('. Note '). each (the function () {
TMP = $ (this). CSS (' Z-index ');
if (tmp>zindex) zindex = tmp;
})
Make_draggable ($ ('. Note '));
var zindex = 0;
In Global.js, first in the $ (function () defined a variable tmp, by judging the Z-index value of each div.note, the div at the top (that is, z-index the maximum), is not covered by another layer.
and set the initial value of ZIndex to 0.
Next, write a function make_draggable (), which calls the jquery UI plug-in's Draggable method, handles the drag range, the transparency, and the update operation that is performed after the drag stops.
function make_draggable (elements) {
elements.draggable ({
opacity:0.8,
containment: ' Parent ',
Start:function (E,ui) {ui.helper.css (' Z-index ', ++zindex);},
stop:function (e,ui) {
$.get (' Update_ Position.php ', {
x : ui.position.left,
y : ui.position.top,
z : zindex,
ID : parseint (Ui.helper.find (' Span.data '). html ())
});
}
When dragged, sets the current layer's Z-index property to the maximum, ensuring that the current layer is topmost, not covered by another layer, and that the drag range and transparency are set, and when the drag is stopped, an AJAX request is sent to the background update_position.php, and the parameters passed are x,y. The value of the z and ID. Next, let's look at the processing of update_position.php.
update_position.php Save Drag location
Update_position.php needs to do is to get the foreground data from the AJAX request, and update the corresponding field contents in the datasheet.
Include_once (' connect.php ');
if (!is_numeric ($_get[' id ')) | | |!is_numeric ($_get[' x ') | |!is_numeric ($_get[' y ']) | |
! Is_numeric ($_get[' z '))
die ("0");
$id = Intval ($_get[' id '));
$x = intval ($_get[' x ']);
$y = intval ($_get[' y '));
$z = intval ($_get[' z ']);
mysql_query ("UPDATE notes SET xyz= '". $x. "|". $y. "|". $z. "' WHERE id= ". $id);
The above is about how to implement the php+mysql+jquery to drag the layer and save the drag position, hope to help you learn.