简体   繁体   English

每种字体的可用值 font-weight

[英]Available values font-weight for each font

I am working on something like text editor using Google fonts and I want to build selector for font-weight, but I need only available values for font (for example Open Sans has 300, 400, 600, 700, 800), and so for everyone.我正在研究使用 Google 字体的文本编辑器之类的东西,我想为字体粗细构建选择器,但我只需要字体的可用值(例如 Open Sans 有 300、400、600、700、800),等等每个人。

How can I get list of available values font-weight for each font?如何获取每种字体的可用值 font-weight 列表?

you can use <select> tag to input font-weight values.您可以使用<select>标签来输入字体粗细值。

here how you can do this.在这里你怎么能做到这一点。

you can use javascript code to add options of font-weight in <select> when a user selects a font-family当用户选择字体系列时,您可以使用 javascript 代码在<select>添加字体粗细选项

CODE代码


HTML HTML

<select id="font-family">
    <option value="'Times New Roman', Times, serif">Times New Roman</option>
    <option value="font-family:Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif">Impact</option>
    <option value="Arial, Helvetica, sans-serif;">Arial</option>
</select>
<select id="font-weight"></select>

JavaScript JavaScript

var fntwgts = { 'FW_NewTimes': [100, 200, 300], 'FW_Arial': [300, 500, 600], 'FW_Impact': [200, 500, 700] };

document.getElementById('font-family').addEventListener("change", () => {
    var FW = document.getElementById('font-weight');
    if(FW.children.length>0){
        FW.innerHTML = '<option value="Default">Default</option>';
    }
    var val = document.getElementById('font-family').value;
    for (var i = 0; i < fntwgts[val].length; i++) {
        var opt = document.createElement("option");
        opt.setAttribute("value", fntwgts[val][i].toString());
        opt.innerHTML = fntwgts[val][i];
        FW.appendChild(opt);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM