zoukankan      html  css  js  c++  java
  • [SCSS] Loop Over Data with the SCSS @each Control Directive

    The SCSS @for directive is great when we know how many iterations are required and we only need 1 variable. What if we need multiple variables to iterate over data sets? Hello SCSS maps and the @each control directive! In this lesson, we’ll check out iterating with lists and looping over data sets with maps and the @each directive.

    // work with simple array
    $superheroes: wonder-woman, spiderman, batman, superman;
    @each $hero in $superheroes {
      .#{$hero}-logo {
        content: "#{$hero}";
      }
    }
    
    // we get
    .wonder-woman-logo {
      content: "wonder-woman"; }
    
    .spiderman-logo {
      content: "spiderman"; }
    
    .batman-logo {
      content: "batman"; }
    
    .superman-logo {
      content: "superman"; }

    key: value pairs using map-get():

    // work with key: value pair
    $breakpoints: (sm: 375px, md: 768px, lg: 1200px);
    $container-widths: (sm: 250px, md: 500px, lg: 750px);
    @each $size, $bp in $breakpoints {
      @media only screen and (min- $bp) {
        .container-width {
          // because $breakpoints and $container-widths have the same key
          // we can use map_get(target_ary, key) to get value
           map_get($container-widths, $size);
        }
      }
    }
    
    // we get
    @media only screen and (min- 375px) {
      .container-width {
         250px; } }
    
    @media only screen and (min- 768px) {
      .container-width {
         500px; } }
    
    @media only screen and (min- 1200px) {
      .container-width {
         750px; } }

    index: values pair using nth()

    $hero-media:  (1 375px 768px crimson),
                  (2 768px 1000px darkred),
                  (3 1200px 1400px grey),
                  (4 768px 1200px blue);
    
    // we get
    
    @media only screen and (min- 375px) and (max- 768px) {
      .wonder-woman {
        background-color: crimson; } }
    
    @media only screen and (min- 768px) and (max- 1000px) {
      .spiderman {
        background-color: darkred; } }
    
    @media only screen and (min- 1200px) and (max- 1400px) {
      .batman {
        background-color: grey; } }
    
    @media only screen and (min- 768px) and (max- 1200px) {
      .superman {
        background-color: blue; } }
  • 相关阅读:
    LintCode-174.删除链表中倒数第n个节点
    LintCode-165.合并两个排序链表
    LintCode-371.用递归打印数字
    LintCode-140.快速幂
    LintCode-373.奇偶分割数组
    NOI 2015 品酒大会 (后缀数组+并查集)
    NOI 2016 优秀的拆分 (后缀数组+差分)
    POJ 2774 Long Long Message (后缀数组+二分)
    BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)
    POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6873635.html
Copyright © 2011-2022 走看看