开发者

Resizable split screen divs using jquery UI

开发者 https://www.devze.com 2023-02-15 09:37 出处:网络
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 c

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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消