admin管理员组

文章数量:1289910

I have a design in mind that involves a split panel view in html, similar to a winforms split panel. I've been expirimenting with jQuery UI - Resizable and I like the function, I just can't seem to co-ordinate the resizing of the two divs. The problem with the current code is that the two divs resize away from each other, not with one following the other. How can I make the two divs work together?

<html>
    <head>
        <title>Test</title>
        <link rel="stylesheet" type="text/css" href="css/custom.css" />
        <link rel="stylesheet" type="text/css" href="css/superfish.css" media="screen">
        <link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.10.custom.css" media="screen">
        <script type="text/javascript" src="script/jquery-1.5.1.js"></script>
        <script type="text/javascript" src="script/superfish.js"></script>
        <script type="text/javascript" src="script/jquery-ui-1.8.10.custom.min.js"></script>
        <script type="text/javascript">


        // initialise plugins
        $(function(){
            try {
                $('ul.sf-menu').superfish();

                //set up divs
                $('#Content').resizable({ handles: 'e', alsoResize: $('#Attributes')});

            }catch(ex){
                alert(ex.message);
            }
        });

        </script>
    </head>
    <body>
       <div id="Header">
            <div id="Menu">
                <ul class="sf-menu" id="nav">
                    <!-- Snip menu -->
                </ul>
            </div>
        </div>
        <div id="Middle">
            <div id="Content" class="ui-widget-content">This is where the view is.<br/>
            Imagine an image here ...
            <br/>
            <br/>
            <br/>
            </div>
            <div id="Attributes" class="ui-widget-content">
                <ul>
                    <li>A</li>
                    <li>B</li>
                    <li>C</li>
                </ul>
            </div>
        </div>
        <div id="FootBreak"/>
        <div id="Footer">
            <a href="#">Help</a>
        </div>
    </body>
</html>

I have a design in mind that involves a split panel view in html, similar to a winforms split panel. I've been expirimenting with jQuery UI - Resizable and I like the function, I just can't seem to co-ordinate the resizing of the two divs. The problem with the current code is that the two divs resize away from each other, not with one following the other. How can I make the two divs work together?

<html>
    <head>
        <title>Test</title>
        <link rel="stylesheet" type="text/css" href="css/custom.css" />
        <link rel="stylesheet" type="text/css" href="css/superfish.css" media="screen">
        <link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.10.custom.css" media="screen">
        <script type="text/javascript" src="script/jquery-1.5.1.js"></script>
        <script type="text/javascript" src="script/superfish.js"></script>
        <script type="text/javascript" src="script/jquery-ui-1.8.10.custom.min.js"></script>
        <script type="text/javascript">


        // initialise plugins
        $(function(){
            try {
                $('ul.sf-menu').superfish();

                //set up divs
                $('#Content').resizable({ handles: 'e', alsoResize: $('#Attributes')});

            }catch(ex){
                alert(ex.message);
            }
        });

        </script>
    </head>
    <body>
       <div id="Header">
            <div id="Menu">
                <ul class="sf-menu" id="nav">
                    <!-- Snip menu -->
                </ul>
            </div>
        </div>
        <div id="Middle">
            <div id="Content" class="ui-widget-content">This is where the view is.<br/>
            Imagine an image here ...
            <br/>
            <br/>
            <br/>
            </div>
            <div id="Attributes" class="ui-widget-content">
                <ul>
                    <li>A</li>
                    <li>B</li>
                    <li>C</li>
                </ul>
            </div>
        </div>
        <div id="FootBreak"/>
        <div id="Footer">
            <a href="#">Help</a>
        </div>
    </body>
</html>
Share Improve this question edited Mar 4, 2011 at 21:24 C. Ross asked Mar 4, 2011 at 21:17 C. RossC. Ross 31.9k44 gold badges151 silver badges239 bronze badges 3
  • Have you tried putting something like style="display:inline;float:left/(right for the other)" ? – asawyer Commented Mar 4, 2011 at 21:33
  • @asawyer, yes. They are sticking to the sides of the screen, but div2 isn't resizing ... – C. Ross Commented Mar 4, 2011 at 21:35
  • Related: Split pane using CSS3 Resize property, same effect, except I need it to work in IE. I will settle for solutions other than jQuery, I just thought it best b/c it's already browser independent (mostly). – C. Ross Commented Mar 5, 2011 at 0:07
Add a ment  | 

3 Answers 3

Reset to default 5

I have worked out a reasonable hack, using the resize event.

<script type="text/javascript">

    var WIDTH_ATTR = 'initWidth';

    // initialise plugins
    $(function(){
        try {
            $('#Content').resizable({ handles: 'e', resize: resizeAttr }); 
            $('#Content').attr(WIDTH_ATTR, $('#Content').width());
            $('#InfoPanel').attr(WIDTH_ATTR, $('#InfoPanel').width());
        }catch(ex){
            alert(ex.message);
        }
    });

    function resizeAttr(event, ui){
        var change = ui.size.width - $('#Content').attr(WIDTH_ATTR);
        $('#InfoPanel').width($('#InfoPanel').attr(WIDTH_ATTR) - change);
    };

</script>

I'm still open to cleaner answers from others...

I am not sure if this meets the requirements, but ExtJS handles split panes with ease and works cross-browser. For example, see this RSS feed client in ExtJS. Be aware the ExtJS has mercial licensing restrictions if your intent is to sell your product.

Here is an example with a horizontal split (one top div, one bottom div), maybe slightly more minimalistic:

HTML:

<html>
  <head>
    <link rel="stylesheet" href="//code.jquery./ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery./ui/1.12.1/jquery-ui.js"></script>
  </head>
  <body>
    <div id="div1">1st</div> 
    <div id="div2">2nd</div>
  </body>
</html>

CSS:

#div1, #div2 {
  position: absolute;
  left: 0;
  right: 0;
  outline: solid 1px #ccc; /* just for making the divs visible */
  margin: 5px; /* just for making the divs visible */
}
#div1 {
  height: 100px;
  top: 0;
}
#div2 {
  top: 110px;
  bottom: 0;
}

JavaScript:

$('#div1').resizable({
  handles: 's',
  resize: resizeEventHandler
});
function resizeEventHandler(event, ui){
  var new_height = ui.size.height;
  $('#div2').css('top', new_height + 10);
}

See demo here.

本文标签: javascriptResizable split screen divs using jquery UIStack Overflow