jquery实现div拖拽宽度示例代码
2016-07-19 10:41
746 查看
<!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 src="../Resources/javascripts/jquery-1.10.2.min.js"></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;">
<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;">12</div>
</div>
</body>
</html>
<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 src="../Resources/javascripts/jquery-1.10.2.min.js"></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;">
<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;">12</div>
</div>
</body>
</html>
相关文章推荐
- jQuery小技巧
- jQuery小技巧
- jQuery中ready与load事件
- jQuery EasyUI提交表单验证
- 使用 jquery.qrcode.js生成二维码
- jquery操作HTML属性和控件
- 使用jQuery获取父页面元素及子页面元素的方法
- jQuery Validate 表单验证
- jQuery 帮助文档
- JavaScript && jQuery获取radio/下拉框的选中值
- jQuery悬停文字提示框插件jquery.tooltipster.js用法示例【附demo源码下载】
- jquery判断元素是否出现在可视区
- jquery中的绑定事件和合成事件
- jquery ajax
- jquery父子隶属关系
- 【js与jquery】订单提交页发送短信功能
- jQuery——总结
- Python全栈开发之16、jquery
- jquery插件开发
- jQuery插件之ajaxFileUpload