我不确定我能说得多好,但让我试一试。
我有一个非常基于jQuery的网站;意思是它的大部分内容是创造,测量和由jQuery设置。
所有这些测量都是px。我需要将它们转换为em,或者最好创建一个能够为我转换它们的函数。如果所有字体大小都是16px(或100%),这将很容易;然而,事实并非如此。我需要它来检测容器的字体大小&从那里开始。
如果措辞不好,我道歉。如果你能理解这个&帮助我,这将是美好的。如果您认为不可能,请不要回复,一切皆有可能,这只是您愿意提出多少努力的问题。
答案 0 :(得分:1)
我明白了。 您可以看到代码here。
function px_to_em(px_value, container) {
////////////////////////////////
// Detect Contianer Font Size //
////////////////////////////////
//Prepend a span tag to the container for testing
$(container).prepend("<span id='wraping_span_to_test'></span>");
//Set the span to height 1em and then save the pixel height to the container_ft_sz variable
container_ft_size = $("#wraping_span_to_test").css({
"font-family": "inherit",
"font-size": "inherit",
"font-style": "inherit",
"font-variant": "inherit",
"font-weight": "inherit",
"line-height": "inherit",
"margin": "0 !important",
"padding": "0 !important",
"border": "0 !important",
"outline": "0 !important",
"background-color": "aqua",
"height": "1em",
"width": " 1em",
"display": "block"
}).height();
//Remove the testing span
$("#wraping_span_to_test").remove();
//Use values to convert to ems
var round_to = 1000;
return Math.round((px_value / container_ft_size) * round_to) / round_to;
}
这应该允许您使用jQuery(通过像素和&amp ;;通过调用一个简单的函数将它们转换为em测量值。
答案 1 :(得分:0)
这并不容易。想一想,你可以暂时插入宽度为1em的内容,以px为单位测量宽度,并从那里计算你需要的em。