这几天在清理电脑文件夹,发现了N年前的一些小demo,顺手记录一下,都是一些比较简单的案例,话不多说了,直接看下文!
提示:以下是本篇文章正文内容,下面案例仅供参考
没啥技术难点,主要是通过样式代码对图片容器进行自定义样式效果的渲染,从而达到变色的效果。
body {
text-align:center;
}
.box {
margin:100px auto;
cursor:pointer;
}
.box img:hover {
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
filter: grayscale(100%);
filter: gray;
}
代码如下(示例):
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>css滤镜实现鼠标悬停图片变黑白(灰色)title>
<style>
body {
text-align:center;
}
.box {
margin:100px auto;
cursor:pointer;
}
.box img:hover {
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
filter: grayscale(100%);
filter: gray;
}
style>
head>
<body>
<div class="box">
<img src="http://k.sinaimg.cn/n/tech/crawl/0/w500h300/20190522/2255-hxhyiun2822255.jpg/w700h350z1l10t107a0.jpg" />
div>
body>
html>
演示如下(示例):

以上就是CSS滤镜实现鼠标悬停图片变黑白(灰色)的一种方式,也是非常简单的一种实现方式。