Sass mixin 将背景透明度恢复到 IE8
我是 Sass 新手,并为此苦苦挣扎。我无法在 hex
(对于 IE)和 rgba
中渲染颜色。每一个小片段都让我感到沮丧,因为我还没有掌握语法,而且 Sass 的 Google 结果仍然很少。
这是 mixin:
@mixin transparent($hex, $a){
/* for IEGR8 */
background: transparent;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#{$a}#{$hex},endColorstr=#{$a}#{$hex});
zoom: 1;
/* for modern browsers */
background-color: rgba(#{$hex},.#{$a});
}
这样 @include透明(#FFF,.4)
应该生成下面漂亮的、与浏览器兼容的透明代码:
background: transparent;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#40FFFFFF,endColorstr=#40FFFFFF);
zoom: 1;
background-color: rgba(100,100,100,.40);
我已经对以下内容感兴趣了几个小时:
#
。- rgba alpha 所需的
.
。
调用 rgba()
时需要包含两者,但 IE #AARRGGBB
中不能包含 #,否则看起来像 #AA #RRGGBB
,以及 .无法包含在 IE 中,或者拒绝 #.AARRGGBB
。
我是否缺少一种更简单的方法来做到这一点?这可以通过 Sass 字符串操作或 Sass 中任何稍微聪明的颜色转换函数来完成吗?它已经为我处理了这个问题?
I'm new to Sass and struggling with this. I can't get the color to render in both hex
(for IE) and rgba
. Every little piece is frustrating me because I haven't mastered the syntax yet, and Google results for Sass are still sparse.
Here's the mixin:
@mixin transparent($hex, $a){
/* for IEGR8 */
background: transparent;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#{$a}#{$hex},endColorstr=#{$a}#{$hex});
zoom: 1;
/* for modern browsers */
background-color: rgba(#{$hex},.#{$a});
}
Such that @include transparent(#FFF,.4)
should produce the nice, browser compatible transparent code below:
background: transparent;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#40FFFFFF,endColorstr=#40FFFFFF);
zoom: 1;
background-color: rgba(100,100,100,.40);
I've been noobing on the following for a couple hours:
- The
#
required for #RGB format. - The
.
required for rgba alpha.
Both need to be included for the call to rgba()
, however the # can't be included for the IE #AARRGGBB
or it will look like #AA#RRGGBB
, and the . can't be included for IE or it rejects #.AARRGGBB
.
Am I missing a much simpler way to do this? Can this be done with Sass string manipulation or any slightly clever color cast function in Sass which handles this for me already?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意: ie-hex-str 仅在最新版本中可用,但不确定何时引入
NOTE: The ie-hex-str is only available in recent versions, not sure when it was introduced though
我想当我想将 url 传递给 mixin 时遇到了类似的问题。我没有只发送 url,而是将整个 url 参数作为参数发送给 mixin。如果你明白我的意思吗?
示例:
插入:
另外,这可能不适合您的应用程序,但我通常使用不透明度来解决该问题:
I think I encountered a similar problem when I wanted to pass a url to the mixin. Instead of sending only the url I had the send the whole url parameter as a parameter to the mixin. If you understand what I mean?
example:
insted of:
Also, this might not the suited for your application, but I usually work around that problem using opacity:
如果没有适当的垫片,这可能是防弹的。
为了构建 seutje 的答案,如果您在 IE 上执行
background-color
,则可以使用ms-filter
透明度,但是如果如果您知道要使其透明的元素后面的元素的颜色,则可以使用 Sass 的“混合”功能来混合两种颜色并获得假透明度 - 对于任何类型的颜色。这意味着边框和文本以及所有这些令人兴奋的东西。它仍然是手动后备,但它会给您准确您尝试用实心十六进制模拟的颜色。SCSS:
要在蓝色背景上获得
border-color: rgba(255, 0, 0, 0.5)
,您可以这样使用它:Sass 自动将 100% 不透明度颜色转回十六进制代码,因此
淡入
为100%。唯一没有颜色不透明度的浏览器是 IE8 <=8 和 Opera <=9.6 以及 IE 8有过滤器,因此这只对
background-color
以外的颜色有帮助。原理是将背景颜色和前景色混合在一起形成一个扁平的六角形。ie-hex-str
现在已经一岁了所以你一定会拥有它。This probably is as bulletproof as you'll get without a proper shim.
To build on seutje's answer, this lets you use
ms-filter
transparency if you're doingbackground-color
on IE, but if you if you know the colour of the element behind the element you want to make transparent, you can use Sass's "mix" function to mix the two colours and get fake transparency - for any kind of colour. That means borders and text and all that jive. It's still a manual fallback but it'll give you the exact colour you're trying to simulate with a solid hex.SCSS:
To get
border-color: rgba(255, 0, 0, 0.5)
on a blue background, you'd use it like:Sass automatically turns 100% opacity colors back into a hex code, hence the
fade-in
of 100%.The only browsers without colour opacity are IE8 <=8 and Opera <=9.6, and IE 8 has filters so this only helps for colors other than
background-color
. The principle is that you mix the background and foreground colours together into a flat hex.ie-hex-str
is like a year old now so you'll definitely have it.