【问题标题】:Pass a linear gradient to a Sass mixin将线性渐变传递给 Sass mixin
【发布时间】:2021-06-11 12:17:52
【问题描述】:

我想将整个线性渐变传递给我的 mixin。 我已经尝试了我能想到的任何方法,但它总是导致它出现“无”,它用白色覆盖了我的图像。

@mixin webp-backgroundGradient($imgpath, $type: '.jpg') {
    background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.8) 10%, white 80%), url('#{$imgpath}#{$type}');
}

【问题讨论】:

    标签: css sass mixins


    【解决方案1】:

    似乎工作得很好,请参阅 codepen...

    https://codepen.io/joshmoto/pen/GRNegrP

    我猜可能是您的图像路径有问题?如果没有看到您的控制台源代码,很难判断。

    我从你的 mixin 参数中删除了 $type: '.jpg' 并直接传递了图像 url。

    @mixin webp-backgroundGradient($img) {
      background-size: cover;
      background-repeat: no-repeat;
      background-image: linear-gradient(
          to bottom,
          rgba(white, 0.5) 10%,
          rgba(white, 1) 90%
        ),
        url("#{$img}");
    }
    
    .image {
      height: 100vh;
      @include webp-backgroundGradient("https://i.imgur.com/UNV29z8.jpeg");
    }
    

    这是输出...

    .image {
      height: 100vh;
      background-size: cover;
      background-repeat: no-repeat;
      background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5) 10%, white 90%), url("https://i.imgur.com/UNV29z8.jpeg");
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" rel="stylesheet"/>
    
    <div class="image"></div>



    更新您的评论...

    @mixin 传递背景图片 url 和可选的背景渐变叠加...

    在此处查看 codepen 示例https://codepen.io/joshmoto/pen/JjbzOoj

    @mixin bg_img_gradient($img,$gradient:false) {
      background-size: cover;
      background-repeat: no-repeat;
      @if $gradient != false {
        background-image: #{$gradient}, url("#{$img}");
      } @else {
        background-image: url("#{$img}");
      }
    }
    
    .image-1 {
      height: 100vh;
      width:50%;
      float: left;
      @include bg_img_gradient(
        "https://i.imgur.com/UNV29z8.jpeg"
      );
    }
    
    .image-2 {
      height: 100vh;
      width:50%;
      float: left;
      @include bg_img_gradient(
        "https://i.imgur.com/UNV29z8.jpeg",
        linear-gradient(
          to bottom,
          rgba(white, 0.5) 10%,
          rgba(white, 1) 90%
        )
      );
    }
    

    这里是css 输出...

    .image-1 {
      height: 100vh;
      width: 50%;
      float: left;
      background-size: cover;
      background-repeat: no-repeat;
      background-image:url("https://i.imgur.com/UNV29z8.jpeg");
    }
    
    .image-2 {
      height: 100vh;
      width: 50%;
      float: left;
      background-size: cover;
      background-repeat: no-repeat;
      background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5) 10%, white 90%), url("https://i.imgur.com/UNV29z8.jpeg");
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" rel="stylesheet"/>
    
    <div class="image-1"></div>
    <div class="image-2"></div>

    【讨论】:

    • 我想将渐变作为这样的参数传递:'at'include webp-backgroundGradient("i.imgur.com/UNV29z8.jpeg", linear-gradient(to bottom, rgba(255, 255, 255) , 0.5) 10%, 白色 90%));这个想法是我想动态设置渐变,因为不同的图片有不同的渐变或根本没有。
    • @itshinkswhenitscold 看到更新的答案,这是你所追求的吗?
    • 完美运行。谢谢
    猜你喜欢
    • 2021-10-01
    • 2016-01-26
    • 2017-10-31
    • 2016-03-25
    • 2014-05-05
    • 2018-12-30
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多