一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

怎么让页面变灰实现代码

时间:2012-09-13 编辑:简简单单 来源:一聚教程网

CSS1

 代码如下 复制代码
 html{filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);}

今天发现其实也可以让Firefox和Chrome变成灰色,不过Opera和Safari没成功,暂时先不理会,有一点东西就先跟大家分享。首先来看一下正常的页面好多漂亮的彩图;再看一下我把源码下载,添加了一行CSS样式后的效果(点这里查看)。

 代码如下 复制代码

 html {
 filter: grayscale(100%);
 -webkit-filter: grayscale(100%);
 -moz-filter: grayscale(100%);
 -ms-filter: grayscale(100%);
 -o-filter: grayscale(100%);
 filter: url(desaturate.svg#grayscale);
 filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);
 -webkit-filter: grayscale(1);
}

大面积的操作降低页面性能是肯定的,这里分享这个也只是想跟大家说一下,最近我看到这个样式,感觉太厉害了。在这个样式代码中,涉及到了一个文件,就是desaturate.svg,从文件扩展名中可以看到,利用到了SVG技术了,代码如下:

 

 代码如下 复制代码
 
 

 

 

只需要将这代码保存为desaturate.svg,再通过url链接就可以了,十分简单。在前面看到的demo页面中是在body中增加了样式,影响页面中所有的元素,如果有需要的话,可以单独设置一个公共库中的类名来调用,如:

 代码如下 复制代码

 .pic_gray {
 filter: grayscale(100%);
 -webkit-filter: grayscale(100%);
 -moz-filter: grayscale(100%);
 -ms-filter: grayscale(100%);
 -o-filter: grayscale(100%);
 filter: url(desaturate.svg#grayscale);
 filter: gray;
 -webkit-filter: grayscale(1);
}

这样操作的话,可以让某个区域变成灰色,一切尽在你的掌握。


国外网站参考


The CSS3 greyscale filter
Desaturating a color image couldn’t be simpler with CSS3. We’ll apply the filter as a class, as you’d typically desire several images to be affected by the code at the same time:

 代码如下 复制代码

img.desaturate { filter: grayscale(100%); }Naturally, all current browsers implement CSS3 filters via vendor prefixes, so our first job is to insert that code, writing in CSS that does not yet exist in order to future-proof our work:

img.desaturate { filter: grayscale(100%);-webkit-filter: grayscale(100%);-moz-filter: grayscale(100%);-ms-filter: grayscale(100%);-o-filter: grayscale(100%);}Applying the class to the image is easy:

 代码如下 复制代码
Lena Söderberg

Add An SVG Filter Effect
The CSS shown to this point works only in Chrome 18+, with support in other browsers expected to arrive soon. To gain the same effect in Firefox 4+, we need to use an SVG filter, which I’ll create as a separate document named desaturate.svg. The code for that file will be:

If the SVG code looks slightly daunting – and the matrix math behind it is somewhat complex – don’t worry. This is one piece of code that I’d actually encourage you to copy and paste as a generic “recipe”. I’ll explain matrix transformations in a future article.

With the SVG file saved beside our HTML page and test image, we will extend the CSS to become:

 代码如下 复制代码

img.desaturate{
filter: grayscale(100%);
-webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);
-ms-filter: grayscale(100%); -o-filter: grayscale(100%);
filter: url(desaturate.svg#greyscale);
}

Add Support for IE
So far our code covers future browsers, recent versions of Chrome and Firefox 4+. To include IE 6 – 9, we'll apply Microsoft’s simple but proprietary use of filter:

 代码如下 复制代码
img.desaturate{
filter: grayscale(100%);
-webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);
-ms-filter: grayscale(100%); -o-filter: grayscale(100%);
filter: url(desaturate.svg#greyscale);filter: gray;}

If you want to add in support for older versions of Webkit:

 代码如下 复制代码
img.desaturate{
filter: grayscale(100%);
-webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);
-ms-filter: grayscale(100%); -o-filter: grayscale(100%);
filter: url(desaturate.svg#greyscale);filter: gray;
-webkit-filter: grayscale(1);
}

Unfortunately Safari and Opera are still left out of the picture, but Safari 5.2 – due to be released very soon – should support the –webkit CSS3 filter prefix, and Opera’s support for CSS3 is continuing to improve.

Note
If you wanted to achieve the same visual result across absolutely every browser (assuming your visitors were running JavaScript) you could use Desaturate with JQuery or Greyscale.js to modify your images.

The CSS we've written here allows us to visually convert an image to black and white on the fly in our browser, with no need to save new versions in PhotoShop. Using CSS also makes the image much easier to modify: for example, you’ll see that lowering the percentage used in our declaration from 100% to 50% causes a visual blend of the desaturation effect with the original color image.

热门栏目