好久没写博客了。以前还想着最少一个月抽空写几篇。结果没做到O(∩_∩)O~~。好吧。现在努力,继续坚持。

看着以前写的东西,感觉自己在逐渐成长。

先上图:

本文主要记录如上图一样的。文字或内容不透明,但背景有一个“透明蒙版”的效果。

平时一般用的方法有三种:

① 先创建一个透明度跟“透明蒙版”一样的1X1大小的透明png图片。然后采用填充背景图的方法(repeat),实现背景透明而不影响文字的透明性。

优点:感觉兼容性最好。

缺点:要多弄一张透明图片。

这个就不写样式了。比较简单。

②采用opacity透明度。当然,要使文字不透明opacity不能加在父容器里,可以多加一个子标签或使用伪类实现。具体的方法就是使用绝对定位把需要透明的子标签或伪类覆盖在容器上。再设置z-index:-1,父容易设置z-index:0;

值得注意的是,当透明度加在伪类的时候,ie8是不支持的。

优点:兼容性较好。

缺点:要多加伪类或标签,使用伪类 ie8不支持。使用标签则需要多加一个标签。

使用的opacoty代码为

css:

filter:alpha(opacity=50); opacity:0.5;-moz-opacity:0.5;

sass:调用 :@include opacity(50);

@mixin opacity($pre:0){
filter:alpha(opacity=$pre); opacity:$pre/100;-moz-opacity:$pre/100;
}

③使用滤镜跟rgba透明颜色实现。(目前使用)

优点:不会有多余的标签。兼容性也可以。

缺点:ie9同时兼容了滤镜和rgba所以在ie9上透明度颜色会重叠(颜色显得更深),滤镜采用的十六进制颜色代码要计算。

css:

filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#00FFFFFF', endColorstr='#00FFFFFF');background-color:rgba(255,255,255,0);

sass:调用:@include bgcolor(#000000,0.5);

@mixin bgcolor($color,$opacity){
$num:floor($opacity*255);
$sixteen:tenToSixteen($num);
$colorStr:'#'+$sixteen+str-slice('#{$color}',2);
filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#{$colorStr}', endColorstr='#{$colorStr}');background-color:rgba($color,$opacity);
}
@function tenToSixteen($num){
$sixteen:"00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F","10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F","20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F","30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F","40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F","50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F","60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F","70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F","80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F","90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F","A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF","B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF","C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF","D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF","E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF","F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF";
@return nth($sixteen,$num+1)
}

平时用sass封装方法不多,所以遇到了一些坑:

1、sass的raga函数颜色值不能接受一个string类型的值,而滤镜需要计算十六进制的值然后进行组合字符串。所以当时考虑颜色参数是带#还是不带#遇到了问题。带#号rgba函数不会报错(如果参数使用带引号的会报错)。但滤镜哪里的颜色值就不好搞了。后来又看了下文档。才终于找到str-slice()函数把颜色值的#排除掉了。

2、滤镜中的颜色是#AARRGGBB形式的。其中RRGGBB代表的就是十六进制的颜色值。即平时使用的FFFFFF之类的值。主要是其中的AA这个对应的是rgba中的a是转换成十六进制后的颜色,如果不明白,不用急,只需两步就可以算出来。继续看:

①算出透明度相对于十进制的位置(0-255) :floor($opacity*255); 例如:透明度是 0.5 那么相对于十进制的位置就是127。

②找出对应的十六进制字符。因为没有找到相应的转换函数。所以只能自己写一个咯。就是上面的 @function tenToSixteen($num)。如上面的127。就是这里函数里对应的第128个(从1开始)。函数列举了0-255的十进制对应的十六进制(比较傻的做法)。还可以用算法算出对应的十六进制的。考虑到256个也不是很多,也懒得算了。直接就全部列举出来就好了。

感觉还有很多不足的地方。请高手不吝赐教。

THE END

css 背景透明文字(内容)不透明三种实现方法的更多相关文章

  1. css实现纯文字内容元素透明背景(兼容IE6)

    HTML: <div class="title-wrapper"> <span class="title"> <span clas ...

  2. 通过IE私有滤镜让IE6 7 8支持背景透明,内容不透明效果。

    CSS3已经支持背景rgba的rgba透明度,这一方法可以避免元素内容也随背景一起变透明(详情请阅http://www.cssha.com/css3-new-knowledge-student).但是 ...

  3. Css:背景色透明,内容不透明之终极方法!兼容所有浏览器

    转载 http://www.cnblogs.com/jikey/archive/2012/08/31/2665880.html <!DOCTYPE html PUBLIC "-//W3 ...

  4. Jquery中each的三种遍历方法

    Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...

  5. Liunx 环境下vsftpd的三种实现方法(超详细参数)

    以下文章介绍Liunx 环境下vsftpd的三种实现方法 ftp://vsftpd.beasts.org/users/cevans/vsftpd-2.0.3.tar.gz,目前已经到2.0.3版本.假 ...

  6. css_三种引入方法

    CSS是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 详请:http://www.w3school.com.cn/h.asp 其存在方式有三种:元素内联 ...

  7. Android开发 ---Button的OnClickListener的三种实现方法

    button的OnClickListener的三种实现方法 onclick事件的定义方法,分为三种,分别为 1.在xml中进行指定方法: 2.在Actitivy中new出一个OnClickListen ...

  8. Spring Boot Maven Plugin打包异常及三种解决方法:Unable to find main class

    [背景]spring-boot项目,打包成可执行jar,项目内有两个带有main方法的类并且都使用了@SpringBootApplication注解(或者另一种情形:你有两个main方法并且所在类都没 ...

  9. 谈谈vector容器的三种遍历方法

    说明:本文仅供学习交流.转载请标明出处.欢迎转载!          vector容器是最简单的顺序容器,其用法相似于数组.实际上vector的底层实现就是採用动态数组.在编敲代码的过程中.经常会变量 ...

随机推荐

  1. SQL(oracle) 取得分组后最大值记录

    方法一 select t1.a,t1.b,t1.c from test t1 inner join (seelct a,max(b) as b from test group by a) t2 on  ...

  2. django queryset values&values_list

    values返回是字典列表; values_list返回的是元组列表, values_list加上 flat=True 1 1 之后返回值列表

  3. Mifare系列7-安全性(转)

    文/闫鑫原创转载请注明出处http://blog.csdn.net/yxstars/article/details/38087245 飞利浦的MIFARE卡 由于它的高安全性在市场上得到广泛应用,比如 ...

  4. 绝对炫的3D幻灯片-SLICEBOX

    绝对炫的3D幻灯片-SLICEBOX http://www.jq22.com/jquery-info31

  5. spark的standlone模式安装和application 提交

    spark的standlone模式安装 安装一个standlone模式的spark集群,这里是最基本的安装,并测试一下如何进行任务提交. require:提前安装好jdk 1.7.0_80 :scal ...

  6. DuiLib 源码分析之CDuiString

    duilib是一个比较常见的界面库,闲来无事看看别人写的代码,跟自己写的一比, 才看到了差距呀,感觉自己写的乱七八糟,keep moving CduiString是duilib提供的一个字符串类,功能 ...

  7. JavaScript中的方法重载

    对js有些了解的人都知道,在js中根本就不存在像C#中的那种方法重载,而有的只是方法的覆盖,当你在js中敲入两个或多个同名的方法的时候,不管方法(函数)的参数个数怎么个不同,这个方法名只能属于最后定义 ...

  8. 上传文件fileupload

    文件上传: 需要使用控件-fileupload 1.如何判断是否选中文件? FileUpload.FileName -  选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.va ...

  9. miniui

    //android提供了一个库minui用于简单的UI输出,源码在bootable/recovery/minui中, //gr_init()和gr_font_size()为minui库提供方法,gr_ ...

  10. MongoDB简介与增删改查

    一.简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB 将数据存储为一个文档,数据 ...