js实例教程-jquery实现div拖拽宽度示例代码

发布时间:2018-12-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了js实例教程-jquery实现div拖拽宽度示例代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

本例是个非常简单的p拖动,有需要的朋友可根据自己的需求,添加相应的代码。欢迎拍砖

. 代码如下:


<!DOCTYPE htML PubLIC "-//W3C//DTD HTML 4.01 TransITional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html style="height:100%;">
<head>
<;meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<title>p width resize</title>
<!--引用jquery-->
<script src="https://code.jquery.COM/jquery-1.8.0.min.js"
type="text/javascript"></script>
<script type="text/javascript">
function bindResize(el)
{
//初始化参数
VAR els = document.getElementById(&#39;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 PResize=function(){
var totalHeight=$("html").height();
console.LOG(totalHeight);
var topHeight=$("#top").height()
$("#menu").height(totalHeight-topHeight);
$("#rightbar").height(totalHeight-topHeight);
}
$(function() {
pResize();
$(window).resize(pResize);

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>
--%>
<p>
<p id="top" style="width: 100%; height: 80px;"></p>
<p style="float: left;" id="menu" class="content">
<span>待拖拽的p</span>
</p>
<p id="rightbar"
style="width: 2px; background: #cccccc; cursor: e-resize; float: left;"></p>
</p>
</body>
</html>

本例是个非常简单的p拖动,有需要的朋友可根据自己的需求,添加相应的代码。欢迎拍砖

. 代码如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html style="height:100%;">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>p width resize</title>
<!--引用jquery-->
<script src="https://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 pResize=function(){
var totalHeight=$("html").height();
console.log(totalHeight);
var topHeight=$("#top").height()
$("#menu").height(totalHeight-topHeight);
$("#rightbar").height(totalHeight-topHeight);
}
$(function() {
pResize();
$(window).resize(pResize);

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>
--%>
<p>
<p id="top" style="width: 100%; height: 80px;"></p>
<p style="float: left;" id="menu" class="content">
<span>待拖拽的p</span>
</p>
<p id="rightbar"
style="width: 2px; background: #cccccc; cursor: e-resize; float: left;"></p>
</p>
</body>
</html>

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦!&nbsp;js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的js实例教程-jquery实现div拖拽宽度示例代码全部内容,希望文章能够帮你解决js实例教程-jquery实现div拖拽宽度示例代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。