我在CSS方面遇到了问题。当我尝试去做的时候
-webkit-filter: blur(1px);
-moz-filter: blur(1px);
-ms-filter: blur(1px);
-o-filter: blur(1px);
filter: blur(1px);
在Safari和Chrome中看起来完美,但在Firefox、Opera或Internet Explorer中完全没有模糊效果。这些浏览器是否支持它?或者是否有其他方法可以使整个页面模糊?
我在CSS方面遇到了问题。当我尝试去做的时候
-webkit-filter: blur(1px);
-moz-filter: blur(1px);
-ms-filter: blur(1px);
-o-filter: blur(1px);
filter: blur(1px);
在Safari和Chrome中看起来完美,但在Firefox、Opera或Internet Explorer中完全没有模糊效果。这些浏览器是否支持它?或者是否有其他方法可以使整个页面模糊?
尝试使用SVG滤镜。
img {
filter: blur(3px);
-webkit-filter: blur(3px);
-moz-filter: blur(3px);
-o-filter: blur(3px);
-ms-filter: blur(3px);
filter: url(#blur);
filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius='3');
}
<img src="https://istack.dev59.com/oURrw.webp" />
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<filter id="blur">
<feGaussianBlur stdDeviation="3" />
</filter>
</svg>
feGaussianBlur stdDeviation="3"></feGaussianBlur>
。 - user1357678filter: blur(3px);
-webkit-filter: blur(3px);
-ms-filter: blur(3px);
filter: url("data:image/svg+xml;utf9,<svg%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'><filter%20id='blur'><feGaussianBlur%20stdDeviation='3'%20/></filter></svg>#blur");
filter:progid:DXImageTransform.Microsoft.Blur(PixelRadius='3');
body {
font-family: Verdana;
font-size: 14px;
font-weight: bold;
}
.container {
height: 500px;
width: 500px;
position: relative;
overflow: hidden;
}
#image {
background-image: url('http://i.imgur.com/HoWL6o3.jpg');
background-size: contain;
background-repeat: no-repeat;
height: 100%;
width: 100%;
position: absolute;
}
#image.blur {
transform: matrix3d(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 0.05);
background-size: 0 0;
}
#image.blur:before {
transform: scale(0.05);
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
content: '';
background-image: inherit;
background-size: contain;
background-repeat: inherit;
}
<div class="container">
<div id="image" class="blur"></div>
</div>
我尝试了以上所有方法,但通常情况下,Internet Explorer / Microsoft Edge希望以不同的方式处理事情,因此我无法使其保持一致。
最终,我不得不跳过一系列障碍,才能使它在所有现代浏览器中按照我想要的方式工作:
Use the CSS already mentioned so it would work on non-IE browsers without the overhead of the fix I had to use for IE:
-webkit-filter: blur(1px);
-moz-filter: blur(1px);
-ms-filter: blur(1px);
-o-filter: blur(1px);
filter: blur(1px);
Detect whether Internet Explorer / Microsoft Edge was being used using this JavaScript (thanks Mario).
If Internet Explorer / Microsoft Edge was detected then load the image into a hidden img
field on the page. You have to do this using the onload
function to make sure the image has loaded before you operate on it otherwise the next step fails (thanks Josh Stodola):
var img = new Image();
img.onload = function() { blurTheImage(); }
img.src = "http://path/to/image.jpg";
Use StackBlur JavaScript which works on IE and Edge (thanks Mario Klingemann). The image should be available by CORS.
HTML:
<div id="ie-image-area" style="display: none;">
<img id="ie-image"/>
<canvas id="ie-canvas"></canvas>
</div>
JavaScript:
function blurTheImage() {
$("#ie-image").attr("src", "http://path/to/image.jpg");
StackBlur.image('ie-image', 'ie-canvas', 1, false);
}
As it happens I wanted to set is as a background, so I had to convert the image before setting the data (thanks user3817470):
imageLinkOrData = document.querySelector('#ie-canvas').toDataURL("image/png");
$('#background-image').css('background-image', 'url(' + imageLinkOrData + ')');
Once loaded and blurred I then faded it in using CSS and a transparency class as jQuery fadeTo
wasn't looking great (thanks Rich Bradshaw):
$('#background-image').toggleClass('transparent');
哎呀,我想我需要喝杯茶休息一下(或许需要更强力的东西!)
这是它运行的示例(感谢Alex78191):
function detectIE() {
return navigator.userAgent.search(/MSIE|Trident|Edge/) >= 0;
}
let image = document.querySelector('#img-for-blur');
image.src = image.getAttribute('data-src');
if (detectIE()) {
image.onload = function() {
StackBlur.image('img-for-blur', 'ie-canvas', 4, false);
};
} else {
image.style.display = 'block';
}
img {
filter: blur(4px);
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/stackblur-canvas/1.4.0/stackblur.js"></script>
<img id="img-for-blur" data-src="https://istack.dev59.com/oURrw.webp">
<canvas id="ie-canvas"></canvas>