哀悼之余,分享网页总体置灰的经验

新华社北京4月3日电 为表达全国各族人民对抗击新冠肺炎疫情斗争牺牲烈士和逝世同胞的深切哀悼,国务院今天发布公告,决定2020年4月4日举行全国性哀悼活动。在此期间,全国和驻外使领馆下半旗志哀,全国中止公共娱乐活动。4月4日10时起,全国人民默哀3分钟,汽车、火车、舰船鸣笛,防空警报鸣响。

—— 人民网css

幸得有你,山河无恙。吾辈青年,定当图强!!!前端


今天,能够看到不少网站都总体展示出灰色,表达全国各族人民对抗击新冠肺炎疫情斗争牺牲烈士和逝世同胞的深切哀悼。哀悼之余,做为前端程序员也得学会这个网页总体置灰技巧。vue

采用filter

filter兼容性

兼容性具体查看 caniuse 程序员

方案

直接上代码: 在网页得根节点上加上以下css便可web

支持chrome和 firefox 最新的浏览器
{
     filter:grayscale(.95);
    -webkit-filter:grayscale(.95);
}
兼容 ie浏览器
{
 filter: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='grayscale'><feColorMatrix type='matrix' values='0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0'/></filter></svg>#grayscale");
    filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=.95 );
}

使用案例 (vue为例)

template 动态绑定class样式chrome

<template>
  <div id="app" :class="[{ 'grayAll': isGrayTime }]">
      <router-view/>
  </div>
</template>

data浏览器

data() {
    return {
      isGrayTime: false // 默认不是置灰的时间
    }
}

jsmethod中添加方法app

setGray() {
    let date = new Date();
    let nowMonth = date.getMonth() + 1;
    let nowDate = date.getDate();
    let now = `${nowMonth}${nowDate}`
    
    let someTime = ['44','1213'] // 4月4日· 和 12月13日国家公祭日 选择置灰
    if(someTime.indexOf(now) > -1) {
        this.isGrayTime = true
    }
}

created中 调用svg

created () {
    this.setGray();
},

style样式网站

.grayAll {
    filter:grayscale(.95);
    -webkit-filter:grayscale(.95);

    filter: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='grayscale'><feColorMatrix type='matrix' values='0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0'/></filter></svg>#grayscale");
    filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=.95 );
}
相关文章
相关标签/搜索