一般在清明节,全国哀悼日,大地震的日子,以及一些影响力很大的伟人逝世或纪念日的时候,身为站长的我们都会让自己的网站的全部网页变成灰色(黑白色),以表示我们对逝者的悼念。
那么今天就说说,通过几行简单的代码,来实现这个功能。
第一种:修改CSS文件
我们可以在网页的CSS文件中添加以下的CSS代码,来实现网页黑白色,也就是网站变灰。
代码
{pboot:pre}
html { filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); -webkit-filter: grayscale(100%); }
{/pboot:pre}
第二种:在网页的<head>标签内加入以下代码
如果你不想改动CSS文件,你可以通过在网页头部中的< head >标签内部加入内联CSS代码的形式实现网站网页变灰。
代码
{pboot:pre}
<style type="text/css"> html { filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); -webkit-filter: grayscale(100%); } </style>
{/pboot:pre}
第三种:修改<html>标签加入内联样式
如里上面的两种方式都不喜欢,可以通过修改< html >标签,以加入内联样式的方法,达到网页变灰的效果
代码
{pboot:pre}
<html style="filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);-webkit-filter: grayscale(100%);" >
{/pboot:pre}
第四种:作者本人用的CSS代码
代码:
{pboot:pre}
body *{ -webkit-filter: grayscale(100%); /* webkit */ -moz-filter: grayscale(100%); /*firefox*/ -ms-filter: grayscale(100%); /*ie9*/ -o-filter: grayscale(100%); /*opera*/ filter: grayscale(100%); filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); filter:gray; /*ie9- */ }
{/pboot:pre}
第五中:通过nginx使网站变灰
在Nginx负载均衡服务器上,利用sub_filter指令在输出的HTML中增加一行。
就可以实现在IE及IE内核浏览器下,所有网站变灰色。步骤如下:
1、重新编译Nginx,增加http_sub_module模块:
{pboot:pre}
--with-http_sub_module
{/pboot:pre}
2、在nginx.conf配置文件的http {...}大括号内增加以下两行:
{pboot:pre}
sub_filter '</head>' '<style type="text/css">html{filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);}</style>'; sub_filter_once on;
{/pboot:pre}
3、保存后,重新加载配置文件:
{pboot:pre}
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
{/pboot:pre}
这样,整个页面就都变灰色了。
PS:以上几种方法,都是通过CSS的滤镜来控制页面的显示而已,唯一不同的就CSS代码调用的方式。各位,喜欢哪种就自己挖去吧!