请各位读者添加一下做者的微信公众号,之后有新的文章,将在微信公众号直接推送给各位,很是感谢。
javascript![]()
注意:
若文章中发现图片没法加载,请移步做者其余博客。css
最近在用手机的过程当中,发现苹果在很是多的地方都使用了模糊的效果。html
高斯效果虽然炫酷,但是我们身为前端人员,又该如何去模仿这个效果呢?前端
这时候咱们就要说到 CSS 3 中的一个属性,Filter 了。java
CSS滤镜(filter):
提供的图形特效,像模糊,锐化或元素变色。
过滤器一般被用于调整图片,背景和边界的渲染。web
Filter 的一些基础内容:浏览器
名称 | 说明 |
---|---|
初始值 | none |
适用元素 | 全部元素;在SVG,它适用于容器元素不包括def元素和全部图形元素 |
继承性 | 不继承 |
适用效果 | 视觉处理 |
增添版本 | CSS3 |
JS 语法 | object.style.WebkitFilter="grayscale(100%)" |
关于适用浏览器版本:微信
平时推荐你们仍是用 Chrome 浏览器来查看效果吧。ide
除此以外,再补充一下在 MDN 上给出的兼容性说明。函数
出自以外,Filter
虽然在属性介绍中说明是不继承的,可是它其实和 Opacity
很是相似,都会致使子级发生问题。
这时候咱们就要用到另一个内容,Filter()
函数了。
可是,我们仍是按部就班,先继续来看 Filter
吧。
在介绍了 Filter
的基础使用以后,咱们接下来介绍一下它的属性。
属性名 | 属性介绍 | 填写方式 |
---|---|---|
none | 无效果 | 默认值 |
grayscale | 灰度 | 百分比 |
sepia | 褐色 | 百分比 |
saturate | 饱和度 | 百分比 |
hue-rotate | 色相旋转 | deg |
invert | 反色 | 百分比 |
opacity | 透明度 | 百分比 |
brightness | 亮度 | 百分比 |
contrast | 对比度 | 百分比 |
blur | 模糊 | px |
drop-shadow | 阴影 | h-shadow v-shadow blur spread color |
既然知道了都有哪些属性,那么咱们接下来就来一块儿看看。
这些属性都是如何去使用的吧。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:grayscale(1);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
grayscale :灰度
</h3>
</body>
</html>复制代码
灰度会将咱们的照片进行灰白处理,其实说白了就是由本来的彩色照片变成 黑白照片而已。
须要注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:sepia(1);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
sepia : 褐色
</h3>
</body>
</html>复制代码
sepia 是将图像转换为深褐色。
处理事后能实现一种相似于“老照片”的泛黄感受。
除此以外,须要注意:
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:saturate(4);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
saturate :饱和度
</h3>
</body>
</html>复制代码
saturate 是用于转换图像饱和度。
虽然转换成 0 的话,看上去实现效果和“灰度”差很少。
可是饱和度是能够设置超过本来图片色彩的。
除此以外,须要注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:hue-rotate(90deg);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
hue-rotate :色相旋转
</h3>
</body>
</html>复制代码
hue-rotate 是给图像应用色相旋转。
数值设定图像会被调整的色环角度值。
注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:invert(1);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
invert :反色
</h3>
</body>
</html>复制代码
invert 是反转输入图像。
就像咱们照相机的底片同样。
注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:opacity(.2);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
opacity :透明度
</h3>
</body>
</html>复制代码
opacity 是转化图像的透明程度。
注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:brightness(2);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
brightness :亮度
</h3>
</body>
</html>复制代码
brightness 是给图片应用一种线性乘法,使其看起来更亮或更暗。
注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:contrast(2);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
contrast :对比度
</h3>
</body>
</html>复制代码
contrast 是调整图像的对比度。
注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:blur(2px);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
blur :模糊
</h3>
</body>
</html>复制代码
终于到重头戏了,模糊。
blur 是给图像设置高斯模糊。
"radius"一值设定高斯函数的标准差,或者是屏幕上以多少像素融在一块儿, 因此值越大越模糊;
注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:drop-shadow(5px 5px 5px #ccc);
}
h3{
margin: 0;
text-align: center;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
drop-shadow :阴影
</h3>
</body>
</html>复制代码
drop-shadow 是给图像设置一个阴影效果。
阴影是合成在图像下面,能够有模糊度的,能够以特定颜色画出的遮罩图的偏移版本。
函数接受
除了"inset"关键字是不容许的。
该函数与已有的box-shadow box-shadow属性很类似;
不一样之处在于,经过滤镜,一些浏览器为了更好的性能会提供硬件加速。
在这里带着你们回顾一下以前 box-shadow 的属性说明:
URL函数接受一个XML文件,该文件设置了 一个SVG滤镜,且能够包含一个锚点来指定一个具体的滤镜元素.
.target { filter: url(#c1); }
.mydiv { filter: url(commonfilters.xml#large-blur) }复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>李鹏 --> QQ:3206064928</title>
<style type="text/css">
div{
width: 467px;
height: 700px;
border: 1px solid #000;
margin: 0px auto;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
img{
width: 100%;
height: 100%;
}
div:hover{
-webkit-filter:contrast(175%) brightness(70%);
}
h3{
margin: 0;
text-align: center;
font-size: 20px;
}
</style>
</head>
<body>
<div>
<img src="IMG_3479.JPG">
</div>
<h3>
你能够组合任意数量的函数来控制渲染。下面的例子能够加强图像的对比度和亮度。
</h3>
</body>
</html>复制代码
你能够组合任意数量的函数来控制渲染。
例如同时去加强图像的对比度和亮度等等。
如下部份内容出自 W3Plus。
不少时候,只是但愿元素的背景作效果调整,又不但愿他会影响其余元素。
并且又没有backdrop-filter属性的情形之下,filter()就显得格外的重要。
在继续往下阅读以前,你要注意一点,filter()并不等于之前介绍过的filter属性。
简单的理解,一个是函数,一个是属性。那么咱们今天要说的是filter()函数。
为了能更好的与filter属性区分,filter()函数接受两个参数:
filter(<url>, <filter-function-list>)复制代码
其中
这二者结合在一块儿将会返回一个处理过的新图像。
例如:
.element {
background: filter(url(path/to/img.jpg), blur(5px));
}复制代码
所以,你能够给图片使用过滤效果,而后填充到元素中,好比background-filter、background-opacity、background-blur等等。
####注意:
值得一提的是,backdrop-filter和filter()可使用CSS3 transition和animation实现一些圆滑的过分效果或动画,甚至还可使用JavaScript。
终于写完了,用时 5 个小时。
其中查找资料就花费了大量的事件。
可是最后仍是可以完成,发送给你们,同时也但愿你们可以在将来用到的时候可以记得看过这篇文章。
好啦,我也要去吃饭啦,拜拜了您嘞!
本文做者 : 李鹏
QQ :3206064928
微信:lipeng_0524
欢迎骚扰。
本文请勿随意转载,做者保留法律追究权利。