测试高度为0元素的宽度尺寸

by redky

HTML

<div class="test" id="box"></div>


<div id="r"></div>

CSS

.test {
    width: 400px;
    margin: 0 auto;
    background: #f1f1f1;
    height: 120px;
}

.trigger {
    height: 0;
    line-height: 0;
    overflow: hidden;
}

#r {
    background: #000;
    color: #fff;
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 120px;
    overflow: auto;
}

JavaScript

var box = document.querySelector('#box');
var r = document.querySelector('#r');

document.addEventListener('dblclick', function(e) {
    box.classList.toggle('trigger');
    var width = Math.max(box.scrollWidth, box.offsetWidth);
    r.innerHTML = '' + new Date + ': box width: ' + width + 'px;<br />' + r.innerHTML;    
    
}, false);