网页变灰怎么实现?

在我们遇到公祭日等日子的时候,某些网页可能会有变灰的需求。

主流网页如何变灰?
b站
在这里插入图片描述
淘宝

在这里插入图片描述

以上是哔哩哔哩与淘宝的变灰方式,它们都使用了CSS中的filter属性。以下是W3C对它的解释:

filter 属性定义元素(通常是 <img>)的视觉效果(如模糊和饱和度)。

比如:需求是把所有图片都改为黑白色:

<!DOCTYPE html>
<html>
<head>
<style>
	img {
  		filter: grayscale(100%);
	}
</style>
</head>
<body>

<h1>filter属性</h1>

<p>将图像转换为灰阶:</p>

<img src="picture.jpg" alt="Picture" width="300" height="300">

<p><b>注释:</b>Edge 12 或 Internet Explorer 不支持 filter 属性。</p>

</body>
</html>

filter的默认值是none,不支持继承。浏览器支持如下:
在这里插入图片描述
当然,filter属性不止有变灰的作用,属性值改变还有其他作用:

  • none:默认值。规定无效果。
  • blur(px) :对图像应用模糊效果。较大的值将产生更多的模糊。如果为指定值,则使用 0。
  • brightness(%):调整图像的亮度。0% 将使图像完全变黑。默认值是 100%,代表原始图像。值超过 100% 将提供更明亮的结果。
  • contrast(%):调整图像的对比度。0% 将使图像完全变黑。默认值是 100%,代表原始图像。超过 100% 的值将提供更具对比度的结果。
  • grayscale(%):将图像转换为灰阶。0% (0) 是默认值,代表原始图像。100% 将使图像完全变灰(用于黑白图像)。注释:不允许负值。
  • opacity(%):设置图像的不透明度级别。opacity-level 描述了透明度级别,其中:0% 为完全透明。100% (1) 是默认值,代表原始图像(不透明)。注释:不允许负值。提示:这个滤镜类似 opacity 属性。

其他的请参考官网:CSS filter属性

最后对所有滤镜函数进行演示:

<!DOCTYPE html>
<html>
<head>
<style>
img {
  width: 33%;
  height: auto;
  float: left; 
}
.blur {filter: blur(4px);}
.brightness {filter: brightness(0.30);}
.contrast {filter: contrast(180%);}
.grayscale {filter: grayscale(100%);}
.huerotate {filter: hue-rotate(180deg);}
.invert {filter: invert(100%);}
.opacity {filter: opacity(50%);}
.saturate {filter: saturate(7);}
.sepia {filter: sepia(100%);}
.shadow {filter: drop-shadow(8px 8px 10px green);}
</style>
</head>
<body>

<h1>filter 属性</h1>

<img src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="blur" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="brightness" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="contrast" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="grayscale" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="huerotate" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="invert" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="opacity" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="saturate" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="sepia" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">
<img class="shadow" src="/i/photo/tulip.jpg" alt="Tulip" width="300" height="300">

<p><b>注释:</b>Edge 12 或 Internet Explorer 不支持 filter 属性。</p>

</body>
</html>

感谢阅读!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不吃冰2323

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值