Fontpicker.js是Fontselect插件的继任者,该插件将输入字段转换为谷歌网页字体和系统/自定义本地字体的全功能字体选择器。
1.加载样式表jquery.fontpicker.css查询
和JavaScriptjquery.fontpicker.js查询
在HTML文件中。
- <link href="/path/to/dist/jquery.fontpicker.min.css" rel="stylesheet" />
- <script src="/path/to/cdn/jquery.slim.min.js"></script>
- <script src="/path/to/dist/jquery.fontpicker.js"></script>
2.为字体选择器创建一个普通输入字段。
- <input id="myFontPicker" type="text">
3.只需调用函数字体选择器
在输入字段上,插件将处理其余部分。
- $('#myFontPicker').fontpicker();
4.使用改变
事件
- function applyFont(element, fontSpec) {
- // Split font into family and weight/style
- var tmp = fontSpec.split(':'),
- family = tmp[0],
- variant = tmp[1] || '400',
- weight = parseInt(variant,10),
- italic = /i$/.test(variant);
- // Set selected font on paragraphs
- var css = {
- fontFamily: "'" + family + "'",
- fontWeight: weight,
- fontStyle: italic ? 'italic' : 'normal'
- };
- $(element).css(css);
- }
- $('#myFontPicker').fontpicker()
- .on('change', function() {
- applyFont('#yourElement', this.value);
- });
5.确定是否显示显示字体变体。默认值:true。
- $('#myFontPicker').fontpicker({
- variants: false
- });
6.自定义要使用的系统字体。将选项设置为虚假的
将禁用系统字体。
- $('#myFontPicker').fontpicker({
- localFonts: {// Default: web safe fonts
- "Arial": {
- "category": "sans-serif",
- "variants": "400,400i,600,600i"
- },
- "Courier New": {
- "category": "monospace",
- "variants": "400,400i,600,600i"
- },
- "Georgia": {
- "category": "serif",
- "variants": "400,400i,600,600i"
- },
- "Tahoma": {
- "category": "sans-serif",
- "variants": "400,400i,600,600i"
- },
- "Times New Roman": {
- "category": "serif",
- "variants": "400,400i,600,600i"
- },
- "Trebuchet MS": {
- "category": "sans-serif",
- "variants": "400,400i,600,600i"
- },
- "Verdana": {
- "category": "sans-serif",
- "variants": "400,400i,600,600i",
- }
- }
- });
7.将您自己的自定义字体添加到字体选择器中。指定包含字体文件的字体文件夹的路径。
- $('#myFontPicker').fontpicker({
- localFontsUrl: '/fonts/',
- localFontsType: 'woff', // Either 'ttf', 'woff', 'woff2' or 'otf'
- });
8.确定字体选择器弹出窗口所附的父容器。默认值:“body”。
- $('#myFontPicker').fontpicker({
- parentElement: '#customContainer'
- });
9.确定是否启用字体延迟加载。默认值:true。
- $('#myFontPicker').fontpicker({
- lazyLoad: true
- });
10.确定要记住最近选择的字体数量(如“最喜欢的字体”部分所示)。默认值:3。
- $('#myFontPicker').fontpicker({
- nrRecents: 5
- });
11.选择字体时执行回调。
- $('#myFontPicker').fontpicker({
- onSelect: function(e) {
- fontType: fontType,
- fontFamily: fontFamily,
- fontStyle: italic ? 'italic' : 'normal',
- fontWeight: weight,
- fontSpec: value
- }
- });
12.确定是否显示清除按钮。
- $('#myFontPicker').fontpicker({
- showClear: true
- });
13.设置字体选择器的语言。可用语言:“en”、“de”、“fr”、“es”、“nl”。
- $('#myFontPicker').fontpicker({
- lang: 'en'
- });
14.API方法。
- // set font: weight 800, italic
- $('#myFontPicker').val('Open Sans:800i').trigger('change');
- // show/hide the font picker
- $('#myFontPicker').fontpicker('show');
- $('#myFontPicker').fontpicker('hide');
- // destroy the font picker
- $('#myFontPicker').fontpicker('destroy');
v1.6.0 (2023-03-22)
v1.5.0 (2022-01-19)
v1.4.5 (2021-09-29)
v1.4.4 (2021-08-06)
第1.4.3节(2021-06-09)
第1.4.2节(2021-05-29)
第1.4.1节(2021-04-28)
1.4版(2021-02-26)
2020-12-11
v1.3 (2020-12-09)
1.2版(2020-11-30)
$('#font').val('').trigger('change')
清除所选字体。v1.1 (2020-07-15)
v1.0.0 (2020-07-10)
版本0.9.1(2020-06-29)
v0.8 (2020-03-12)
版本0.7(2020-03-04)
v0.6 (2020-03-03)
v0.5 (2020-02-28)
Â