easyui視窗(window)越界問題--限定easyui視窗(window)活動範圍,視窗easyui
在使用easyui視窗(window)時遇到一個問題,在表單拖拽或改變大小時,如果超出瀏覽器範圍,就回不來了.
因為瀏覽器不會顯示捲軸.還要重新重新整理頁面.
於是我寫了一個方法來實現限定表單的活動範圍(只能顯示在瀏覽器可視範圍中):
<div id="createSchoolWindow" class="easyui-window" title="建立學校"
data-options="modal:true,closed:true,minimizable:false,iconCls:'icon-add'"
style="width: 530px; height: 380px; padding: 10px;">
<form id="createSchoolForm" method="post">
<table>
<tr>
<td>學校名稱:</td>
<td><input class="easyui-validatebox" type="text" name="name" data-options="required:true"></input></td>
<td>學校網址:</td>
<td><input class="easyui-validatebox" type="text" name="webSite" data-options="validType:'url'"/></input></td>
</tr>
<tr>
<td>連絡人姓名:</td>
<td><input class="easyui-validatebox" type="text" name="linkman"></input></td>
<td>連絡人電話:</td>
<td><input class="easyui-validatebox" type="text" name="linkmanPhone"></input></td>
</tr>
<tr>
<td>學校地址:</td>
<td colspan="3"><textarea rows="4" cols="25" name="addr"></textarea></td>
</tr>
<tr>
<td>備忘:</td>
<td colspan="3">
<textarea rows="4" cols="25" name="remark"></textarea>
</td>
</tr>
<tr>
<td colspan="4" align="center">
<a href="#" class="easyui-linkbutton" onclick="createSchool();" icon="icon-save">儲存</a>
<a href="#" class="easyui-linkbutton" onclick="closeCreateSchoolWindow();" icon="icon-cancel">關閉</a>
</td>
</tr>
</table>
</form>
</div>
/*
原理很簡單:註冊onMove和onResize事件,在越界是自動回複:
*/
$(document).ready(function() {
$("#createSchoolWindow").window({
onMove:function(left,top){
//console.log(this.left+" - "+top);
if(left<0){
$("#createSchoolWindow").window("resize",{left:0});
}
if(top<0){
$("#createSchoolWindow").window("resize",{top:0});
}
},
onResize:function(width, height){
var w=$(window).width();//可視寬度
var h=$(window).height();//可視高度
var left=$("#createSchoolWindow").window("options").left;//表單左邊距離
var top=$("#createSchoolWindow").window("options").top;//表單右邊距離
if((width+left)>w){
$("#createSchoolWindow").window("resize",{width:w-left});
}
if((height+top)>h){
$("#createSchoolWindow").window("resize",{height:h-top});
}
}
});
});