jQuery - 左右拖动分隔条

1、实现效果:

2、代码:

<!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的绝对坐标的功能函数,即使是非绝对定位,一样能获取到
         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>

3、Demo:jQuery-左右拖动分隔条.rar

时间: 2024-08-18 19:56:28

jQuery - 左右拖动分隔条的相关文章

借用JQuery在网页中实现分隔条的功能

在C/S系统中有专门的分隔条控件,很方便实现,但在Asp.net中却没有.本文介绍了一种使用JQuery技术实现分隔条的功能. Javascript代码如下,将该代码保存成JS文件后在HTML中引用. jsplit 1 jQuery.noConflict(); 2 jQuery.fn.extend({ 3 jsplit: function (j) { 4 return this.each(function () { 5 j = j || {}; 6 j.Btn = j.Btn || {}; 7

最好用的jquery列表拖动排列(由项目提取)

代码压缩包下载: http://pan.baidu.com/s/1mgxAIy0 代码一预览: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">

jquery 鼠标拖动排序Li或Table

1.前端页面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="拖动排序Li或Table.aspx.cs" Inherits="拖动排序Li或Table" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o

jQuery UI 拖动(Draggable) - 事件

定义和用法 draggable 上的 start.drag 和 stop 事件.拖拽开始时触发 start 事件,拖拽期间触发 drag 事件,拖拽停止时触发  stop 事件 示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"&

jQuery UI 拖动(Draggable) - Handles和Cancel

定义和用法 只有当光标在 draggable 上指定部分时才允许拖拽.使用 handle 选项来指定用于拖拽对象的元素(或元素组)的 jQuery 选择 器或者当光标在 draggable 内指定元素(或元素组)上时不允许拖拽.使用 cancel选项来指定取消拖拽功能的 jQuery 选择器 示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equi

jQuery UI 拖动(Draggable) - 还原位置

定义和用法 当带有布尔值 revert 选项的 draggable 停止拖拽时,返回 draggable(或它的助手)到原始位置 示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>jQuery 

Duilib教程-自动布局3-分隔条

先看一个常用的图,如下: 左边是导航栏,右边是信息区. 中间可以自由拉伸. XML如下: <?xml version="1.0" encoding="utf-8" standalone="yes" ?> <Window size="695,542"> <HorizontalLayout width="695" height="542" bkcolor=&qu

jQuery UI 拖动(Draggable) - 延迟开始

定义和用法 通过 delay 选项设置延迟开始拖拽的毫秒数.通过 distance 选项设置光标被按下且拖拽指定像素后才允许拖拽 示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>jQuery 

基于jQuery加载进度条特效代码

基于jQuery加载进度条特效代码是一款简单的加载新数据,获取数据jQuery进度条代码. 在线预览   源码下载 实现的代码. html代码: <div id="main"> <div class="demo"> <div class="bars"> <span id="bar">55</span> </div> <div class="