简体   繁体   中英

Resizable split screen divs using jquery UI

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 div s. The problem with the current code is that the two div s resize away from each other, not with one following the other. How can I make the two div s 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 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 commercial 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.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM