jQuery實現(xiàn)分隔條左右拖動功能
更新時間:2015年11月21日 15:40:24 作者:Jimmy Cheung11
這篇文章主要介紹了jQuery實現(xiàn)分隔條左右拖動功能,需要的朋友可以參考下
本文實例講述了jQuery實現(xiàn)分隔條左右拖動功能的實現(xiàn)代碼。分享給大家供大家參考。具體如下:
運行效果截圖如下:


具體內(nèi)容如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<script type="text/javascript" src="jquery.min.js"></script>
<style type="text/css">
html, body, div {
margin: 0;
padding: 0;
border: 0;
-moz-user-select: none;
-webkit-user-select: none;
}
.gf_s {
float: left;
width: 4px;
cursor: e-resize;
background-color: #fff;
border: #99BBE8 1px solid;
}
.gf_s_g {
float: left;
width: 4px;
display: none;
cursor: e-resize;
position: absolute;
background-color: #F0F0F0;
border: #99BBE8 1px solid;
filter: alpha(opacity=60);
-moz-opacity: 0.6;
-khtml-opacity: 0.6;
opacity: 0.6;
z-index: 1000;
}
</style>
</head>
<body>
<div id="divP" style="width:100%; height:100%;">
<div id="divLeft" style="background-color: green; float: left; "></div>
<div id="divS" class="gf_s" style="float: left;"></div>
<div id="divSG" class="gf_s_g" style="float: left;"></div>
<div id="divRight" style="background-color: blue; float: left;"></div>
</div>
<script type="text/javascript">
var $sliderMoving = false;
//兼容各種瀏覽器的,獲取鼠標真實位置
function mousePosition(ev) {
if (!ev) ev = window.event;
if (ev.pageX || ev.pageY) {
return { x: ev.pageX, y: ev.pageY };
}
return {
x: ev.clientX + document.documentElement.scrollLeft - document.body.clientLeft,
y: ev.clientY + document.documentElement.scrollTop - document.body.clientTop
};
};
//獲取一個DIV的絕對坐標的功能函數(shù),即使是非絕對定位,一樣能獲取到
function getElCoordinate(dom) {
var t = dom.offsetTop;
var l = dom.offsetLeft;
dom = dom.offsetParent;
while (dom) {
t += dom.offsetTop;
l += dom.offsetLeft;
dom = dom.offsetParent;
};
return { top: t, left: l };
};
//分隔條幽靈左右拖動(mousemove)
function sliderGhostMoving(e) {
$("#divSG").css({ left: mousePosition(e).x - 2, display: "block" });
};
//完成分隔條左右拖動(mouseup)
function sliderHorizontalMove(e) {
var lWidth = getElCoordinate($("#divSG")[0]).left - 2;
var rWidth = $(window).width() - lWidth - 6;
$("#divLeft").css("width", lWidth + "px");
$("#divRight").css("width", rWidth + "px");
$("#divSG").css("display", "none");
};
function reinitSize() {
var width = $(window).width() - 6;
var height = $(window).height();
$("#divLeft").css({ height: height + "px", width: width * 0.75 + "px" });
$("#divS").css({ height: height - 2 + "px", width: "4px" });
$("#divSG").css({ height: height - 2 + "px", width: "4px" });
$("#divRight").css({ height: height + "px", width: width * 0.25 + "px" });
}
$(document).ready(function () {
reinitSize();
$("#divS").on("mousedown", function (e) {
$sliderMoving = true;
$("divP").css("cursor", "e-resize");
});
$("#divP").on("mousemove", function (e) {
if ($sliderMoving) {
sliderGhostMoving(e);
}
});
$("#divP").on("mouseup", function (e) {
if ($sliderMoving) {
$sliderMoving = false;
sliderHorizontalMove(e);
$("#divP").css("cursor", "default");
}
});
});
$(window).resize(function () {
reinitSize();
});
</script>
</body>
</html>
希望本文所述對大家學(xué)習(xí)jquery分隔條有所幫助。
相關(guān)文章
jQuery實現(xiàn)響應(yīng)鼠標背景變化的動態(tài)菜單效果代碼
這篇文章主要介紹了jQuery實現(xiàn)響應(yīng)鼠標背景變化的動態(tài)菜單效果代碼,涉及jquery鼠標mouseover事件操作頁面元素屬性的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
jQuery使用$.ajax進行異步刷新的方法(附demo下載)
這篇文章主要介紹了jQuery使用$.ajax進行異步刷新的方法,涉及jQuery實現(xiàn)ajax異步刷新實現(xiàn)數(shù)據(jù)交互的相關(guān)技巧,并提供了完整示例demo供讀者下載參考,需要的朋友可以參考下2015-12-12
js jquery驗證銀行卡號信息正則學(xué)習(xí)
銀行卡號格式驗證如何錯誤將提示:格式錯誤,應(yīng)該是19位數(shù)字,利用正則實現(xiàn),感興趣的朋友可以了解下,希望本文對你學(xué)習(xí)正則有所幫助2013-01-01
Jquery選擇子控件"大于號"和" "區(qū)別介紹及使用示例
Jquery選擇子控件”>“:在給定的父元素下匹配所有的子元素;另一個就是在給定的祖先元素下匹配所有的后代元素,具體概述及使用示例如下,感興趣的朋友可以參考下哈2013-06-06
jQuery UI Autocomplete 1.8.16 中文輸入修正代碼
jQuery UI Autocomplete 1.8.16 中文輸入修正代碼,使用jQuery UI Autocomplete的朋友可以參考下2012-04-04
jQuery中對未來的元素綁定事件用bind、live or on
這篇文章主要介紹了jQuery中對未來的元素綁定事件用bind、live or on,需要的朋友可以參考下2014-04-04

