使用jquery在堆叠div之间拖动水平条



所以我有两个div在彼此的上下。上面的是内容区,下面的是关于内容的注释部分。我想在两个分割线之间放一个不可见的条我可以拖动两个分割线的高度。如果可能的话,我还想把它扣到顶部或底部。

我将附上一个当前外观的例子,但只要html中的类保持不变,设计就可以改变。感谢任何帮助。谢谢!
jsfiddle.net/jv4edcc4/

试试这个-代码很简单,你可以在这里检查它是如何工作的- http://jsfiddle.net/Bek9L/3142/

HTML:

<div class="clearfix">
<div id="sidebar">
    <span id="position"></span>
    sidebar
    <div id="dragbar"></div>
</div>
<div id="main">
    main
</div>
</div>
<div id="console"></div>
CSS:

body,html{width:100%;height:100%;padding:0;margin:0;}
.clearfix {
    height: 100%;
}
.clearfix:after {
    content: '';
    display: table;
    clear: both;
}
#main{
   background-color: BurlyWood;
   height: 50%;
    width: 100%;
}
#sidebar{
   background-color: IndianRed;
   width:100%;
   height:50%;
   overflow-y: hidden;
   position: relative;
}
#dragbar{
   background-color:black;
   height:10px;
   width: 100%;
   cursor: row-resize;
    position: absolute;
    bottom: 0px;
}
#ghostbar{
    width: 100%;
    height:5px;
    background-color:#000;
    opacity:0.5;
    position:absolute;
    cursor: col-resize;
    z-index:999
}

和JS(使用jQuery):

var i = 0;
var dragging = false;
   $('#dragbar').mousedown(function(e){
       e.preventDefault();
       dragging = true;
       var main = $('#main');
       var ghostbar = $('<div>',
                        {id:'ghostbar',
                         css: {
                                width: main.outerWidth(),
                                top: main.offset().top,
                                left: main.offset().left
                               }
                        }).appendTo('body');
        $(document).mousemove(function(e){
          ghostbar.css("top",e.pageY+2);
       });
    });
   $(document).mouseup(function(e){
       if (dragging) 
       {
           var percentage = (e.pageY / window.innerHeight) * 100;
           var mainPercentage = 100-percentage;
           //$('#console').text("side:" + percentage + " main:" + mainPercentage);
           $('#sidebar').css("height",percentage + "%");
           $('#main').css("height",mainPercentage + "%");
           $('#ghostbar').remove();
           $(document).unbind('mousemove');
           dragging = false;
       }
    });

最新更新