Two divs 50% each. Make middle draggable to make bigger or smaller

别说谁变了你拦得住时间么 提交于 2019-12-03 20:29:45

Here's a method which I quickly wrote, this does not use jQuery though.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example Slider</title>
<style type="text/css">
    #bar1 {
        position: absolute;
        background: red;
        height: 250px;
        width: 400px;
        z-index: 1;
    }

    #bar2 {
        position: absolute;
        background: green;
        height: 250px;
        width: 800px;
    }

    #slider {
        position: relative;
        background: blue;
        height: 100%;
        width: 10px;
        float: right;
    }
</style>

<script type="text/javascript">
    var down = false;
    var mouse_x;
    var interval;
    var IE = document.all?true:false

    if (!IE) document.captureEvents(Event.MOUSEMOVE)
    document.onmousemove = get_mouse_x;

    function get_mouse_x(e) {
        if (IE)
            mouse_x = event.clientX;
        else
            mouse_x = e.pageX;
    }

    function drag() {
        interval = setInterval("update()", 1);
    }

    function release() {
        clearInterval(interval);
    }

    function update() {
        if ((mouse_x - document.getElementById('bar1').offsetLeft) >= document.getElementById('bar2').offsetWidth) {
            release();
            document.getElementById('bar1').style.width = document.getElementById('bar2').offsetWidth + "px";
        } else if (mouse_x <= document.getElementById('bar1').offsetLeft) {
            release();
            document.getElementById('bar1').style.width = document.getElementById('bar1').offsetLeft + "px";
        } else {
            document.getElementById('bar1').style.width = (mouse_x - document.getElementById('bar1').offsetLeft) + "px";
    }
    }
</script>
</head>

<body onmouseup="javascript:release();">
    <div id="bar1">
        <div id="slider" onmousedown="javascript:drag();"> </div>
    </div>

    <div id="bar2"></div>
</body>
</html>

I don't recommend you use that on your website, instead learn from it or improve so that it is completely stable. But hopefully that gives you an idea on how to tackle this problem.

Also there's a bug in IE where, after sliding, it does not lose focus from the slider div. So this means that when you re-slide it, it doesn't release properly. To avoid this: after initial sliding, focus something else, and then slide again. Other then that it works fine.

Hopefully this was helpful in some way :)

I don't have a complete answer for you, but one thing that might be worth a look is the Resizable interaction from jQuery's UI: http://jqueryui.com/demos/resizable/#synchronous-resize

It will largely depend on how you want the final product to work. You can probably set it up so that one DIV is resizable, and the adjacent one is just filling the gap.

What you're describing is commonly referred to as a Spliter. Judging by the demo, this jQuery plugin looks like it does exactly what you want.

Insead of doing css percentages, I would find the width of the window, split it in half (minus bar width), and set pixel widths. this makes it easier to change when the bar is moved.

This may be WAY overkill depending what else your application UI needs are, but ExtJS does this sort of thing very well.

One other unconventional option that should be mentioned: a frameset. Frames were the darling of the mid-90s, but they are still supported by just about every browser without much fuss.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!