在 SCSS 中循环两级嵌套列表以创建动态 @font-face

费兰·比雷乌

我有一个类似的列表:

$fonts: (
  primary: (
    extra-light: (
      family:'Literata',
      weight: 200,
      style: normal,
      display: swap,
      src: url('/assets/fonts/Literata-Regular.woff2'),
    ),
    light: (
      family: 'Literata',
      weight: 300,
      style: normal,
      display: swap,
      src: url('/assets/fonts/Literata-Regular.woff2'),
    ),
    regular: (
      family: 'Literata',
      weight: 400,
      style: normal,
      display: swap,
      src: url('/assets/fonts/Literata-Regular.woff2'),
    ),
    bold: (
      family: 'Literata',
      weight: 700,
      style: normal,
      display: swap,
      src: url('/assets/fonts/Literata-Regular.woff2'),
    ),
  ),
  secondary: (
    light: (
      family: 'Zilla Slab',
      weight: 300,
      style: normal,
      display: swap,
      src: url('/assets/fonts/Zilla-Slab-Regular.woff2'),
    ),
    regular: (
      family: 'Zilla Slab',
      weight: 400,
      style: normal,
      display: swap,
      src: url('/assets/fonts/Zilla-Slab-Regular.woff2'),
    ),
    bold: (
      family: 'Zilla Slab',
      weight: 700,
      style: normal,
      display: swap,
      src: url('/assets/fonts/Zilla-Slab-Bold.woff2'),
    ),
),
  tertiary: (
    regular: (
      family: 'Montserrat',
      weight: 400,
      style: normal,
      display: swap,
      src: url('/assets/fonts/Montserrat-Regular.woff2'),
    ),
  ),
);

我的目标是遍历每种字体类型(primary,secondarytertiary),然后遍历每种类型的每种变体(extra-light, light, regular, bold)。

我想@font-face根据每个字体数据构建一个动态属性。

我尝试过这样的事情但没有成功:

@each $font-types in $fonts {
  $properties: family weight style src;

  @each $property, $values in $font-types {
    @if contains($properties, $property) {
      @font-face {
        @if $property != 'src' {
          font-#{$property}: map-get($font-properties, $property);
        } @else{
          src: map-get($font-properties, $property) format('woff2');
        }
      }
    }
  }
}

作为记录:contains是一个辅助函数:

@function contains($list, $values...) {
  @each $value in $values {
    @if type-of(index($list, $value)) != 'number' {
      @return false;
    }
  }

  @return true;
}
费兰·比雷乌

对于那些可能会有所帮助的人:我最终使用了以下方法:

@each $font-type, $font-variants in $fonts {
  @each $properties, $font-variant in $font-variants {
    @font-face {
      @each $property, $value in $font-variant {
        @if $property != 'src' {
          font-#{$property}: $value;
        } @else {
          #{$property}: $value format('woff2');
        }
      }
    }
  }
}

它输出以下内容:

在此处输入图像描述

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Stylus CSS中循环@ font-face

为什么@ font-face在样式表中失败?

@ font-face在Rails 3.2中行为异常

@ font-face中的自定义CSS属性

由于@Font-Face,Chrome 中的渲染问题

Font Face在IIS 8.0中不起作用

在Visual Studio中无法识别@ font-face

使用@font face时在QtWebEngine中忽略Google字体(ttf)

在Chrome中,@ font-face local()找不到本地字体

@ font-face在Chrome中不起作用

@ font-face在Android Cordova中无法使用Unicode Malayalam

Webpack在@ font-face失败

无法让@ font-face工作

使用Assetic的Synmfony 2中CSS文件中的@ font-face规则

__layout.svelte 中定义的@font-face 无法从组件中的表单元素访问?

在包含多个实例的 TTF 文件中为 @font-face 使用特定实例

在javascript / jquery IE与Chrome和FF中阅读@ font-face CSS

如何在Rails 4中使用@ font-face嵌入字体?

是否有可能通过JavaScript将@ font-face html实体输入到输入中?

svg @ font-face在svg中有效,但当包含在页面中时则无效

使Adobe字体与IE9中的CSS3 @ font-face一起使用

@ font-face在Firefox和Internet Explorer中不起作用

React Node.js-用于生成PDF的jsx中的自定义@ font-face

字体重量:粗体的@ font-face在IE9中不起作用

@ Font-face CSS规则在Firefix 68.0.1中不起作用

用 cssutils 替换 @font-face CSS 规则中的 URI 值

匹配CSS @ font-face规则中的local()的值是什么?

无法使用@ font-face加载字体

@ font-face字体不起作用