DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery實現div拖拽寬度示例代碼
jquery實現div拖拽寬度示例代碼
編輯:JQuery特效代碼     
本例是個非常簡單的div拖動,有需要的朋友可根據自己的需求,添加相應的代碼。歡迎拍磚
. 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html style="height:100%;">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>div width resize</title>
<!--引用jquery-->
<script src="http://code.jquery.com/jquery-1.8.0.min.js"
type="text/javascript"></script>
<script type="text/javascript">
function bindResize(el)
{
//初始化參數
var els = document.getElementById('menu').style;
//鼠標的 X 和 Y 軸坐標
x = 0;
//邪惡的食指
$(el).mousedown(function (e)
{
//按下元素後,計算當前鼠標與對象計算後的坐標
x = e.clientX - el.offsetWidth - $("#menu").width();
//在支持 setCapture 做些東東
el.setCapture ? (
//捕捉焦點
el.setCapture(),
//設置事件
el.onmousemove = function (ev)
{
mouseMove(ev || event);
},
el.onmouseup = mouseUp
) : (
//綁定事件
$(document).bind("mousemove", mouseMove).bind("mouseup", mouseUp)
);
//防止默認事件發生
e.preventDefault();
});
//移動事件
function mouseMove(e)
{
//宇宙超級無敵運算中...
els.width = e.clientX - x + 'px';
}
//停止事件
function mouseUp()
{
//在支持 releaseCapture 做些東東
el.releaseCapture ? (
//釋放焦點
el.releaseCapture(),
//移除事件
el.onmousemove = el.onmouseup = null
) : (
//卸載事件
$(document).unbind("mousemove", mouseMove).unbind("mouseup", mouseUp)
);
}
}
var divResize=function(){
var totalHeight=$("html").height();
console.log(totalHeight);
var topHeight=$("#top").height()
$("#menu").height(totalHeight-topHeight);
$("#rightbar").height(totalHeight-topHeight);
}
$(function() {
divResize();
$(window).resize(divResize);

bindResize(document.getElementById('rightbar'));
});
</script>
<style type="text/css">
.content {
width: 200px;
background: #f1f1f1;
text-align: center;
border-color: #CCCCCC;
border-style: solid;
border-width: 0 1px;
}
</style>
</head>
<body style="padding: 0; margin: 0;">
<%--
<table style="height: 100%">
<tr>
<td id="menu" class="content"></td>
<td id="rightbar"
style="width: 2px; background: #cccccc; cursor: e-resize;"></td>
</tr>
</table>
--%>
<div>
<div id="top" style="width: 100%; height: 80px;"></div>
<div style="float: left;" id="menu" class="content">
<span>待拖拽的div</span>
</div>
<div id="rightbar"
style="width: 2px; background: #cccccc; cursor: e-resize; float: left;"></div>
</div>
</body>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved