如何使用纯JS导入字体? [英] How to import fonts with pure JS?

查看:714
本文介绍了如何使用纯JS导入字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要完全不使用 CSS 来完成网站.

I need to complete a site without using CSS at all.

一切正常,但是我不知道如何使用纯 JS 来更改我的字体系列.

Every thing goes right but i don't know how to change my font-family with pure JS.

有人知道仅使用 JS 可以导入字体吗?

Does anyone know how it is possible to import fonts using JS only?

谢谢!

推荐答案

我建议使用Google的 Webfontloader ,使用起来非常简单:

I would suggest using Google's Webfontloader, it's incredibly simple to use:

WebFont.load({
    google: {
        families: ['Droid Sans', 'Droid Serif']
    }
});

这将允许您使用由Google 此处托管的庞大的网络字体库,而无需明确指定CSS.如果您愿意使用样式标签或.css文件(或者可以通过javascript注入@ font-face,也可以使用自定义字体).还有其他与Webfontloader一起使用的网络字体提供程序在github页面上.

Which would allow you to use the massive web font library hosted by google here, without needing to explicitly specify the CSS. You can use custom fonts as well if you're willing to use a style tag or a .css file (or you can inject the @font-face via javascript.) There are other web font providers that work with the Webfontloader, just look on the github page.

如果您有兴趣学习如何在javascript中手动执行此操作,则通过javascript注入CSS的任何查询都会对您进行排序,但是从技术上讲,这仍然是使用CSS.不确定这是否是您想要的.

If you're interested in learning how to do this manually in javascript, any query on injecting CSS via javascript will sort you, but, that's still technically using CSS. Not sure if this/that is what you want.

这篇关于如何使用纯JS导入字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆