jquery實(shí)現(xiàn)div拖拽寬度示例代碼
更新時(shí)間:2013年07月31日 17:41:18 投稿:whsnow
本例為大家演示個(gè)比較簡(jiǎn)單的div拖動(dòng),另外可根據(jù)自己的需求,添加相應(yīng)的代碼,實(shí)現(xiàn)自己的想要的效果,具體如下,喜歡的請(qǐng)支持下
本例是個(gè)非常簡(jiǎn)單的div拖動(dòng),有需要的朋友可根據(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)
{
//按下元素后,計(jì)算當(dāng)前鼠標(biāo)與對(duì)象計(jì)算后的坐標(biāo)
x = e.clientX - el.offsetWidth - $("#menu").width();
//在支持 setCapture 做些東東
el.setCapture ? (
//捕捉焦點(diǎn)
el.setCapture(),
//設(shè)置事件
el.onmousemove = function (ev)
{
mouseMove(ev || event);
},
el.onmouseup = mouseUp
) : (
//綁定事件
$(document).bind("mousemove", mouseMove).bind("mouseup", mouseUp)
);
//防止默認(rèn)事件發(fā)生
e.preventDefault();
});
//移動(dòng)事件
function mouseMove(e)
{
//宇宙超級(jí)無(wú)敵運(yùn)算中...
els.width = e.clientX - x + 'px';
}
//停止事件
function mouseUp()
{
//在支持 releaseCapture 做些東東
el.releaseCapture ? (
//釋放焦點(diǎn)
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實(shí)現(xiàn)炫酷的疊加層自動(dòng)切換特效
這篇文章主要給大家分享的是一則jquery實(shí)現(xiàn)炫酷的疊加層自動(dòng)切換特效,效果非常棒,這里推薦給小伙伴們。2015-02-02ASP.NET jQuery 實(shí)例1(在TextBox里面創(chuàng)建一個(gè)默認(rèn)提示)
通常用戶在搜索內(nèi)容時(shí),在文本框輸入內(nèi)容前,文本框都會(huì)給出默認(rèn)提示,提示用戶輸入正確的內(nèi)容進(jìn)行搜索2012-01-01jquery實(shí)現(xiàn)的用戶注冊(cè)表單提示操作效果代碼分享
這篇文章主要介紹了jquery實(shí)現(xiàn)的用戶注冊(cè)表單提示操作效果,推薦給大家,有需要的小伙伴可以參考下。2015-08-08如何解決jQuery EasyUI 已打開(kāi)Tab重新加載問(wèn)題
最近在項(xiàng)目中遇到這樣的需求,要求實(shí)現(xiàn)點(diǎn)擊左側(cè)已經(jīng)打開(kāi)的tab可以刷新重新加載datagrid。下面給大家分享實(shí)現(xiàn)代碼,一起看看吧2016-12-12Javascript中的Array數(shù)組對(duì)象詳談
這篇文章主要介紹了Javascript中的Array數(shù)組對(duì)象,需要的朋友可以參考下2014-03-03jQuery 入門級(jí)學(xué)習(xí)筆記及源碼
本周公司技術(shù)講座輪到我了,準(zhǔn)備說(shuō)說(shuō)現(xiàn)在流行的jQuery,下面是概要提綱,關(guān)鍵是看custom.js,已上傳源碼,程序中“//##”是分段用的,大家可以根據(jù)每一段取消注釋,然后找到相應(yīng)的id或class名查看效果。2010-01-01