jquery实现div拖拽宽度

1 篇文章 0 订阅
1 篇文章 0 订阅

本文参考于:http://www.cnblogs.com/yelaiju/archive/2012/02/16/2354602.html

本例是个非常简单的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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值