css - 将线性渐变传递给 Sass mixin
问题描述
我想将整个线性渐变传递给我的 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}');
}
解决方案
似乎工作得很好,请参阅codepen ...
https://codepen.io/joshmoto/pen/GRNegrP
我猜这可能是您的图像路径的问题?没有看到你的控制台源很难说。
我$type: '.jpg'
从您的 mixin 参数中删除并直接传递了图像 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>
推荐阅读
- javascript - Google 网站的自定义 CSS/JS
- pandas - 熊猫到日期时间检查格式
- r - 无法将因子转换为数字
- momentjs - Moment() 给出了错误的时间
- spring - 不要涵盖在事务方法中运行的事务中的方法
- java - Java 1.6 上的 Java Map compute() 示例
- node.js - 无法从主进程向 Electron Renderer 进程发送函数
- linux - 如何快速隐藏/恢复基于终端的应用程序?
- javascript - 从 Flask 读取 Javascript 中的变量名
- python - 在 Python 中使用文件的起始名称读取选定的文件