答案 0 :(得分:2)
您可以使用window.getComputedStyle
访问:before伪元素的样式。见http://davidwalsh.name/pseudo-element。但是,这会获得元素的css高度和宽度,而不是旋转变换后的边界框。
进入hacky领域,我从http://upshots.org/javascript/jquery-copy-style-copycss借用代码,将所有样式从伪元素复制到实际元素,将其添加到DOM并使用getBoundingClientRect来获取边界框。 / p>
var style = window.getComputedStyle(
document.querySelector(".arrow"), ":before"
)
var dest = {}
if (style.length) {
for (var i = 0, l = style.length; i < l; i++) {
prop = style[i];
camel = prop.replace(/\-([a-z])/, camelize);
val = style.getPropertyValue(prop);
dest[camel] = val;
}
} else {
for (prop in style) {
camel = prop.replace(/\-([a-z])/, camelize);
val = style.getPropertyValue(prop) || style[prop];
dest[camel] = val;
}
}
var copy = $("<div />").css(dest)
copy.appendTo(".arrow")
var boundingRect = copy[0].getBoundingClientRect()
console.log(boundingRect.height)
console.log(boundingRect.width)
copy.remove()
function camelize(a, b) {
return b.toUpperCase();
}