【问题标题】:horizontal bar drag between stacked divs using jquery [closed]使用jquery在堆叠的div之间拖动水平条[关闭]
【发布时间】:2015-10-22 18:54:05
【问题描述】:

所以我有两个上下的 div。上面的一个是内容区域,下面的一个是关于内容的注释部分。我想在两个 div 之间放置一个不可见的栏,我可以在其中拖动两个 div 的高度。如果可能的话,我还想捕捉到顶部或底部。

我将附上当前外观的示例,但只要类在 html 中保持不变,设计就可以更改。任何帮助表示赞赏。谢谢!

jsfiddle.net/jv4edcc4/

【问题讨论】:

  • @AndrewEvt 这是jsfiddle link
  • drag the height of the two divs 这是什么意思?
  • @AndrewEvt 我希望能够在两个 div 之间使用一个栏,该栏基本上控制注释部分的高度。我希望主要内容是最大的区域,但人们可以使用栏来扩大笔记部分或根据需要隐藏它
  • @AndrewEvt 这是我找到的一个例子,但希望它基本上是水平的,而不是这个垂直的例子link
  • 事实上,jsfiddle 完全符合我的要求。 HTML 和 jquery 之间的栏是我想要实现的。你可以拖动那个栏来调整你想要更大的部分。

标签: jquery html css height draggable


【解决方案1】:

试试这个 - 代码很简单,你可以在这里查看它是如何工作的 - 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;
       }
    });

【讨论】:

  • 在示例中不拖动
  • 立即尝试,忘记更新...jsfiddle.net/Bek9L/3142
  • 繁荣就是这样。感谢您的帮助!
  • 欢迎您!祝你有美好的一天)请为我的评论设置 +1 :)
猜你喜欢
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多