Resize a div on border drag and drop without adding extra markup

后端 未结 3 1373
萌比男神i
萌比男神i 2020-11-29 23:57

I have an absolute positioned side panel and I need to change its width by dragging this border. Also I need to change cursor on border hover. Is it possible to do this with

3条回答
  •  星月不相逢
    2020-11-30 00:51

    i hope it may help you

    http://jsfiddle.net/T4St6/82/

    left content!
    right content!

    CSS

    body, html {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }
    #container {
        width: 100%;
        height: 100%;
    
    }
     #left_panel {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        right: 100px;
        background: grey;
    }
    
    #right_panel {
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        width: 200px;
        color:#fff;
        background: black;
    }
     #drag {
        position: absolute;
        left: -4px;
        top: 0;
        bottom: 0;
        width: 8px;
        cursor: w-resize;
    }
    

    JQUERY

    var isResizing = false,
        lastDownX = 0;
    
    $(function () {
        var container = $('#container'),
            left = $('#left_panel'),
            right = $('#right_panel'),
            handle = $('#drag');
    
        handle.on('mousedown', function (e) {
            isResizing = true;
            lastDownX = e.clientX;
        });
    
        $(document).on('mousemove', function (e) {
            // we don't want to do anything if we aren't resizing.
            if (!isResizing) 
                return;
    
            var offsetRight = container.width() - (e.clientX - container.offset().left);
    
            left.css('right', offsetRight);
            right.css('width', offsetRight);
        }).on('mouseup', function (e) {
            // stop resizing
            isResizing = false;
        });
    });
    

提交回复
热议问题