2013-05-11 34 views
0

我得到的jQuery變量來更新高度的寬度和位置,當divs被拖動,但我需要他們做同樣的時候他們的大小調整....我怎麼能做到這一點?這裏是我的代碼,我現在使用得到變量,並將它們發送到PHP文件:更新時拖動和調整大小Jquery

<? 
$query = mysql_query("SELECT * FROM users WHERE username='derekshull'"); 
$rows = mysql_fetch_array($query); 
$googlewebx = $rows['googlewebx']; 
$googleweby = $rows['googleweby']; 
$googlewebh = $rows['googlewebh']; 
$googlewebw = $rows['googlewebw']; 

$googleimagex = $rows['googleimagex']; 
$googleimagey = $rows['googleimagey']; 
$googleimageh = $rows['googleimageh']; 
$googleimagew = $rows['googleimagew']; 

$googlenewsx = $rows['googlenewsx']; 
$googlenewsy = $rows['googlenewsy']; 
$googlenewsh = $rows['googlenewsh']; 
$googlenewsw = $rows['googlenewsw']; 

$wikix = $rows['wikix']; 
$wikiy = $rows['wikiy']; 
$wikih = $rows['wikih']; 
$wikiw = $rows['wikiw']; 

$wolfx = $rows['wolfx']; 
$wolfy = $rows['wolfy']; 
$wolfh = $rows['wolfh']; 
$wolfw = $rows['wolfw']; 

$twitterx = $rows['twitterx']; 
$twittery = $rows['twittery']; 
$twitterh = $rows['twitterh']; 
$twitterw = $rows['twitterw']; 
?> 

<html> 
<head> 


<style> 
    .resizable { color: white; width: 1px; height: 1px; padding: 0.1em; bottom: 0; left: 0; } 
    .resizable h3 { text-align: center; margin: 0; } 
    </style> 


<style> 
#set div.resizable { 
    background: rgba(0, 157, 255, 0.9); 
    color: black; 
    float: left; 
    margin: 0 10px 10px 0; 

    padding: 0.5em; 
} 
    #set { clear:both; float:left; width: 368px;} 
    p { clear:both; margin:0; padding:1em 0; } 
</style> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> 

<script> 
function stop(ui, type) { 
    var pos_x; 
    var pos_y; 
    var window_width; 
    var window_height; 
    var need; 
    if (type == 'draggable') { 
     pos_x = ui.offset.left; 
     pos_y = ui.offset.top; 
     window_width = window.innerWidth; 
     window_height = window.innerHeight; 
     need = ui.helper.data("need"); 
    } else if (type == 'resizable') { 
     pos_x = $(ui.element).offset().left; 
     pos_y = $(ui.element).offset().top; 
     window_width = window.innerWidth; 
     window_height = window.innerHeight; 
     need = ui.helper.data("need"); 
    } 

    var width; 
    var height; 

    if (need == 1) { 
     width = $("#web").width(); 
     height = $("#web").height(); 
    } 

    if (need == 2) { 
     width = $("#image").width(); 
     height = $("#image").height(); 
    } 

    if (need == 3) { 
     width = $("#wiki").width(); 
     height = $("#wiki").height(); 
    } 

    if (need == 4) { 
     width = $("#twitter").width(); 
     height = $("#twitter").height(); 
    } 

    if (need == 5) { 
     width = $("#googlenews").width(); 
     height = $("#googlenews").height(); 
    } 

    if (need == 6) { 
     width = $("#wolf").width(); 
     height = $("#wolf").height(); 
    } 

    console.log(pos_x); 
    console.log(pos_y); 
    console.log(width); 
    console.log(window_width); 
    console.log(need); 

    //Do the ajax call to the server 
    alert(' x:' + pos_x + 
     ' y:' + pos_y + 
     ' ned_id:' + need + 
     ' width:' + width + 
     ' height:' + height + 
     ' window_width:' + window_width + 
     ' window_height:' + window_height); 
} 

$("#set div").draggable({ 
    stack: "#set div", 
    preventCollision: true, 
    containment: $('#main_content'), 
    stop: function (event, ui) { 
     stop(ui, 'draggable'); 
    } 
}); 

$(".resizable").resizable({ 
    stop: function (event, ui) { 
     stop(ui, 'resizable'); 
    } 
}); 
</script> 




<meta http-equiv='Content-type' content='text/html;charset=UTF-8'> 
<title>15:11 Project | You • Your Community • Your World</title> 
</head> 
<body> 


<form action='' method='post'> 
<fieldset><center> 
<input type='search' name='q' /><input type='submit' value='Search' name='submit' /> 
</fieldset></center> 
</form> 


<div id="main_content" style="position: fixed; bottom: 0; left: 0; width:100.8%; margin:0 auto; height:95.1%; background-color: #ffffff; color: #000000;"> 

<div id="set"> 





<? 
if(isset($_POST['q'])){ 
?> 
<div id='web' style='overflow:hidden; left: 5%; top: 5%; width: 20%; height: 15%; position:fixed !important;' class='resizable ui-widget-content' data-need='1'> 
<?php 

enter code here for div 1. 

echo "</div>"; 
} 
?> 









<? 
if(isset($_POST['q'])){ 
?> 
<div id='image' style='overflow:hidden; height: 19%; width: 32%; left: 60%; top: 12%; position:fixed !important;' class='resizable ui-widget-content' data-need='2'><center> 
<?php 

Enter code here for div 2 
echo "</center></div>"; 
} 
?> 








<? 
if(isset($_POST['q'])){ 
?> 
<div id='wiki' style='overflow:hidden; left: 5%; top: 36%; height: 17%; width: 25%; position:fixed !important;' class='resizable ui-widget-content' data-need='3'> 
<?php 

Enter div 3. 
} 
?> 
</div> 



</div> 

</div> 

回答

0

您可以將功能停止事件......

$(function() { 
    $(".resizable").resizable({ 
     stop: function(){ 
      // Do your updates here 
     } 
    }); 
}); 

工作小提琴: http://jsfiddle.net/zkDHJ/

+0

我更新了我的代碼,但沒有奏效。它甚至沒有給出調整大小的選項。仍然可以拖動它。 – user2362601 2013-05-11 21:01:42

+0

我已經添加了一個工作代碼的小提琴。你必須在別的地方遇到問題。 – eidsonator 2013-05-11 21:08:56

0

可調整大小的函數具有相同的事件停止,你可以創建一個函數來你在拖拽停止正在做什麼,從調整大小停止

叫它

http://api.jqueryui.com/resizable/#event-stop

$(function() { 
$(".resizable").resizable({ 
    stop: function() { 
    // call the same function as in draggable event stop 
    } 
}); 
}); 

我建議創建一個函數,從兩個事件調用如

function stop(ui, type) { 
// your code 
} 

然後從兩個事件稱之爲:

$(".resizable").resizable({ 
stop: function(event, ui) { 
    stop(ui, 'resizable'); 
)}; 

$(".draggable").resizable({ 
stop: function(event, ui) { 
    stop(ui, 'draggable'); 
} 
)}; 

編輯:你看可以看到這個jsfiddle顯示你的代碼工作: http://jsfiddle.net/v8efG/1/

與可拖動和可調整大小返回的對象有所不同。

Draggable包含一個可以使用的偏移屬性。看看維基:

​​

和resizable不包含偏移,但它包含元素,你可以從它的偏移。在維基看看:

http://api.jqueryui.com/resizable/#event-stop

當拖動

pos_x = ui.offset.left; 

當可調整大小

pos_x = $(ui.element).offset().left; 
+0

我更新了我的代碼,但沒有奏效。它甚至沒有給出調整大小的選項。仍然可以拖動它。 – user2362601 2013-05-11 21:02:00

+0

它現在將調整大小,但不會像拖動時那樣保存位置和大小。 – user2362601 2013-05-11 21:11:26

+0

請更新您的代碼,看看您是否有任何其他問題,因爲代碼是相同的,它應該以任何方式工作。 – sergioadh 2013-05-11 21:13:52

相關問題