JQuery UI Resizable + box-sizing:border-box = height bug?

时间:2013-08-20 20:18:50

标签: jquery html css resizable

我正在尝试使用box-sizing调整DIV的大小:border-box; 即使我只允许调整宽度,每次调整大小事件停止(高度变化)时,DIV都会缩小。

我的CSS

.test{
    width: 200px;
    height: 100px;
    border: 1px solid red;
    box-sizing: border-box;

}

的Javascript

$(document).ready(function() {
    $('.test').resizable({handles: 'e', stop: function(event, ui){
        $('.wdt').text(ui.size.height);
    }});
});

HTML

<div class="test">
    Test
</div>
<br/>
<br/>
<span>Height =</span>
<span class='wdt'></span>

调整大小,你会看到。

任何人都可以帮助我吗? JSFiddle:http://jsfiddle.net/WuQtw/4/

我试过jquery 1.8.x .. 1.9.x ......没什么变化。 我不能使用box-sizing:content-box。

5 个答案:

答案 0 :(得分:9)

我不知道为什么会这样,但似乎你的border属性就是问题。

如果您希望它的功能相同,则可以改为使用outline

http://jsfiddle.net/WuQtw/10/

.test{
    width:200px;
    height: 100px;
    outline:1px solid red;
    box-sizing: border-box;
}

答案 1 :(得分:1)

我刚刚发现了同样的问题并构建了这个片段 - 可能对某人有帮助。

// initiate correction
var iHeightCorrection = 0;

var oElement = $('#elementToResize');

// init resize
oElement.resizable({
        handles: 's',
        minHeight: 30,
        // on start of resize
        start: function(event, ui) {
            // calculate correction
            iHeightCorrection = $(oElement).outerHeight() - $(oElement).height();
        },
        // on resize
        resize: function(event, ui) {
            // manual correction
            ui.size.height += iHeightCorrection;
        },
        // on stop of resize
        stop: function(event, ui) {
            // reset correction
            iHeightCorrection = 0;
        }
    });

Fiddle

答案 2 :(得分:0)

你正在寻找宝宝吗?

等待一年将jquery更新为2.0.2

然后它起作用

http://jsfiddle.net/WuQtw/37/

//it worked on jsfiddle using  jQuery UI 1.10.3
$(document).ready(function() {
  
    $('.test').resizable({ 
      
      stop: function(event, ui){
        
        $('.wdt').text(ui.size.height);
        
    }});
});
.test{
    width: 200px;
    height: 100px;
    border: 1px solid red;
    box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div class="test">
      Test
</div>
<br/>
<br/>
<span>Height =</span>
<span class='wdt'></span>

答案 3 :(得分:0)

我通过包装元素解决了类似问题。使用边框为0的div来包装原始元素。

.wrapperDiv {
    position:absolute; 
    height:100px; 
    width:100px; 
    border: 0px;
}

.innerDiv{
    height:100%; 
    width:100%;
    background-color:rgba(0,0,0,0);
    border: 3px solid gold;
    border-radius: 5px;
    box-sizing: border-box
}
...
var tSelectionDiv = $('<div class="wrapperDiv"><div class="innerDiv"></div></div>');
...
tSelectionDiv.resizable({
    handles: 'e, w', 
    containment:someContainerElement
});

答案 4 :(得分:0)

box-sizing: border-box更改为box-sizing: content-box应该可以。

JSFiddle:http://jsfiddle.net/WuQtw/65/

参考:https://bugs.jqueryui.com/ticket/9137