我有2个本地字体,每个字体有2种风格(正常和粗体),我想在一个网站上使用,我正在尝试设置字体外观观察器来加载它们。我列出了两种具有相同名称的字体,以便浏览器可以选择普通字体和粗体字体,而不必触摸css的每一个部分来为粗体设置特定的字体。我认为通常这应该可以很好地工作,但是添加了字体观察器后,我不确定现在是否需要指定粗细,然后相应地修改我的css。我在https://github.com/bramstein/fontfaceobserver上看到字体粗细不是强制的,但如果不指定,字体粗细将被设置为“正常”。
所以这就是:
var fontHeading = new FontFaceObserver("Oxygen", { weight: 400 });
var fontMain = new FontFaceObserver("Rubik", { weight: 400 });
Promise.all([
fontHeading.load(),
fontMain.load(),
]).then(function() {
document.documentElement.className += " fonts-loaded";
});或者这样:
var fontHeading = new FontFaceObserver("Oxygen", { weight: 400 });
var fontHeadingBold = new FontFaceObserver("Oxygen", { weight: 700 });
var fontMain = new FontFaceObserver("Rubik", { weight: 400 });
var fontMainBold = new FontFaceObserver("Rubik", { weight: 700 });
Promise.all([
fontHeading.load(),
fontHeadingBold.load(),
fontMain.load(),
fontMainBold.load(),
]).then(function() {
document.documentElement.className += " fonts-loaded";
});我的css:
@font-face {
font-family: 'Rubik';
font-style: normal;
font-weight: 400;
src:
url(../fonts/rubik-v6-latin-regular.woff2) format('woff2'),
url(../fonts/rubik-v6-latin-regular.woff) format('woff');
}
@font-face {
font-family: 'Rubik';
font-style: normal;
font-weight: 700;
src:
url(../fonts/rubik-v6-latin-700.woff2) format('woff2'),
url(../fonts/rubik-v6-latin-700.woff) format('woff');
}
@font-face {
font-family: 'Oxygen';
font-style: normal;
font-weight: 400;
src:
url(../fonts/oxygen-webfont.woff) format('woff'),
url(../fonts/oxygen-webfont.woff2) format('woff2');
}
@font-face {
font-family: 'Oxygen';
font-style: normal;
font-weight: 700;
src:
url(../fonts/oxygen-bold-webfont.woff) format('woff'),
url(../fonts/oxygen-bold-webfont.woff2) format('woff2');
}css:
h3 {
font-weight: bold;
font-family: "oxygen-bold";
}https://stackoverflow.com/questions/47659536
复制相似问题