Resize elements by dragging divider handler

前端 未结 3 683
一向
一向 2020-12-16 08:34

I want to be able to drag a divider up and down to resize the divs above and below the divider on a fixed page height. These divs could be in a table, although they need not

3条回答
  •  臣服心动
    2020-12-16 09:08

    Here is another alternative to Nick's version, it automatically moves the horizontal handler to the top and to zero as a nice effect. Also, it adjusts the height size of both sections mutually.

    $('.horizontal_handler').draggable({
    
            axis: 'y', 
            containment: 'parent',
            helper: 'clone', 
            drag: function (event, ui) { 
    
                var height = ui.offset.top - 220 // 220 : initial top margin to the previousDiv
                , referenceHeight = nextDivHeight + previousDivHeight  //500 px initial height size
                , previousSection = $(this).prev()
                , nextSection = $(this).next();
    
                if ((nextSection.height() === 0) && (referenceHeight - height < 0)){
                    return;
                }
    
                previousSection.height(height); 
                nextSection.height(referenceHeight - height ) ;
    
    
                if (nextSection.height()< 20){
                        previousSection.height( height+ nextSection.height() );
                        nextSection.height(0);
                    }
    
                if (previousSection.height()< 20){
                        nextSection.height(referenceHeight - height - previousSection.height() );
                        previousSection.height(0); 
                    }
            } 
        });
    

提交回复
热议问题