jquery實現(xiàn)div拖拽寬度示例代碼
更新時間:2013年07月31日 17:41:18 投稿:whsnow
本例為大家演示個比較簡單的div拖動,另外可根據(jù)自己的需求,添加相應(yīng)的代碼,實現(xiàn)自己的想要的效果,具體如下,喜歡的請支持下
本例是個非常簡單的div拖動,有需要的朋友可根據(jù)自己的需求,添加相應(yīng)的代碼。歡迎拍磚
復(fù)制代碼 代碼如下:
<!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)
{
//初始化參數(shù)
var els = document.getElementById('menu').style;
//鼠標(biāo)的 X 和 Y 軸坐標(biāo)
x = 0;
$(el).mousedown(function (e)
{
//按下元素后,計算當(dāng)前鼠標(biāo)與對象計算后的坐標(biāo)
x = e.clientX - el.offsetWidth - $("#menu").width();
//在支持 setCapture 做些東東
el.setCapture ? (
//捕捉焦點
el.setCapture(),
//設(shè)置事件
el.onmousemove = function (ev)
{
mouseMove(ev || event);
},
el.onmouseup = mouseUp
) : (
//綁定事件
$(document).bind("mousemove", mouseMove).bind("mouseup", mouseUp)
);
//防止默認事件發(fā)生
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>
相關(guān)文章
jQuery簡單實現(xiàn)的HTML頁面文本框模糊匹配查詢功能完整示例
這篇文章主要介紹了jQuery簡單實現(xiàn)的HTML頁面文本框模糊匹配查詢功能,涉及jQuery事件響應(yīng)模擬列表框的下拉數(shù)據(jù)展示與隱藏,以及元素遍歷、匹配等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
jQuery學(xué)習(xí)7 操作JavaScript對象和集合的函數(shù)
jQuery學(xué)習(xí)7:操作JavaScript對象和集合的函數(shù)2010-02-02
Jquery easyui 實現(xiàn)動態(tài)樹
本文給大家介紹jquery easyui實現(xiàn)動態(tài)樹,本文通過代碼實例相結(jié)合的方式給大家展示jquery easyui實現(xiàn)動態(tài)樹的過程,感興趣的朋友一起學(xué)習(xí)吧2015-11-11
JQuery中$.ajax()方法參數(shù)詳解及應(yīng)用
JQuery中$.ajax()方法想必大家并不陌生吧,在本文將為大家介紹下其參數(shù)及應(yīng)用示例,感興趣的朋友不要錯過2013-12-12
jquery進行數(shù)組遍歷如何跳出當(dāng)前的each循環(huán)
通過 jquery 的循環(huán)方法進行數(shù)組遍歷,但是當(dāng)不符合條件時,怎么跳出當(dāng)前循環(huán),解決方法如下,需要的朋友可以參考下2014-06-06
一款基jquery超炫的動畫導(dǎo)航菜單可響應(yīng)單擊事件
。這款導(dǎo)航菜單,初始時頁面中間一個按鈕,單擊按鈕,菜單從左側(cè)飛入頁中。再次單擊按鈕,導(dǎo)航飛入左側(cè)消息2014-11-11

