代码之家  ›  专栏  ›  技术社区  ›  MM. trayzey

如何仅使用jQuery而不使用插件来垂直调整DIV的大小?

  •  6
  • MM. trayzey  · 技术社区  · 16 年前

    编辑: 我把这段代码放在jsbin中: http://jsbin.com/eneru


    <div id="frame" style="border: 1px solid green; height: 350px">
        <div style="height: 100%">Lorem ipsum blah blah</div>
        <span id="frame-grip" style="display: block; width: 100%; height: 16px; background: gray"></span>
    </div>
    

    $(document).ready(function(){
        var resizing = false;
        var frame = $("#frame");
        var origHeightFrame = frame.height();
        var origPosYGrip = $("#frame-grip").offset().top;
        var gripHeight = $("#frame-grip").height();
    
    
        $("#frame-grip").mouseup(function(e) {
            resizing = false;
        });
    
        $("#frame-grip").mousedown(function(e) {
            resizing = true;
        });
    
        $("#frame-grip").mousemove(function(e) {
            if(resizing) {
                frame.height(e.pageY - origPosYGrip + origHeightFrame - gripHeight/2);
            }
        });
    });
    

    4 回复  |  直到 16 年前
        1
  •  13
  •   Paolo Bergantino    16 年前

        2
  •  7
  •   Henrik    13 年前

        $(document).ready(function()
    {
        var resizing = false;
        var frame = $("#frame").height();
    
        $(document).mouseup(function(event)
        {
            resizing = false;
            frame = $("#frame").height();
        });
    
        $("#frame-grip").mousedown(function(event)
        {
            resizing = event.pageY;
        });
    
        $(document).mousemove(function(event)
        {
            if (resizing)
            {
                $("#frame").height(frame + resizing - event.pageY);
            }
        });
    });
    

    我如何使用它的实时示例,按下红色按钮,缺少图像,所以我用简单的颜色替换了它。 http://jsbin.com/ufuqo/23

        3
  •  3
  •   Emmett    16 年前

    $(document).ready(function(){
        var resizing = false;
        var frame = $("#frame");
    
        $(document).mouseup(function(e) {
            resizing = false;
        });
    
        $("#frame-grip").mousedown(function(e) {
            e.preventDefault();
            resizing = true;
        });
    
        $(document).mousemove(function(e) {
            if(resizing) {
              var origHeightFrame = frame.height();
              var origPosYGrip = $("#frame-grip").offset().top;
              var gripHeight = $("#frame-grip").height();
              frame.height(e.pageY - origPosYGrip + origHeightFrame - gripHeight/2);
            }
        });
    });
    
        4
  •  2
  •   Stobor    16 年前

    通过仅在实际拖动时绑定mousemove函数,您可以在不调整大小时不调用任何处理程序:

    $(document).ready(function(){
        var resizing = function(e) {
            var frame = $("#frame");
            var origHeightFrame = frame.height();
            var origPosYGrip = $("#frame-grip").offset().top;
            var gripHeight = $("#frame-grip").height();
            frame.height(e.pageY - origPosYGrip + origHeightFrame - gripHeight/2);
            return false;
        }
    
        var stopResizing = function(e) {
            $(document).unbind("mouseover", resizing);
        }
    
        $("#frame-grip").mouseup(stopResizing);
    
        $("#frame-grip").mousedown(function(e) {
            e.preventDefault();
            $(document).bind("mouseover", resizing).mouseup(stopResizing);
        });
    
    });
    

    (样本位于 http://jsbin.com/ufuqo )