日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

marbin mysql_跨浏览器图像灰度(grayscale)解决方案

發布時間:2024/10/8 数据库 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 marbin mysql_跨浏览器图像灰度(grayscale)解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,你需要在一個svg里聲明濾鏡,該svg可以內嵌到html文件里,也可以單獨保存。

[html] view plaincopy

如果該文件保存成了單獨的文件gray.svg,我們可以在html文件里加以引用。

[css] view plaincopy

img{

filter:url('gray.svg#grayscale'); /*灰度濾鏡放在gray.svg文件的id叫做grayscale的濾鏡里*/

}

如果該文件內嵌入了html文件里,則是這樣引用

[css] view plaincopy

img{

filter:url('#grayscale'); /*svg濾鏡內嵌入了html文件*/

}

當然,我們也可以直接把svg文件打包嵌入到css里,如果你沒有代碼潔癖的話

[css] view plaincopy

img {

filter: url('url("data:image/svg+xml;utf8,

200.3333%200%200%200%200%200%201%200'/>

#grayscale");')

}

上面的方式,我們可以兼容大部分的瀏覽器(除了IE10、IE11),兼容的grayscale代碼如下。

[css] view plaincopy

img{

-webkit-filter: grayscale(100%); ? ? ? ? ? ?/* CSS3 filter方式,webkit內核方式,firefox外的絕大部分的現代瀏覽器*/

-moz-filter: grayscale(100%); ? ? ? ? ? ?/* 目前沒有實現 */

-ms-filter: grayscale(100%);

-o-filter: grayscale(100%);

filter: grayscale(100%); ? ? ? ? ? ?/* CSS3 filter方式,標準寫法*/

filter: url(filters.svg#grayscale); /* Firefox 4+ */

filter: gray; ? ? ? ? ? ? ? ? ? ? ? /* IE 6-9 */

}

img:hover{

-webkit-filter: grayscale(0%);

-moz-filter: grayscale(0%);

-ms-filter: grayscale(0%);

-o-filter: grayscale(0%);

filter: grayscale(0%);

filter: none; /* Firefox 4+, IE 6-9 */

}

4.js實現

對于IE10、11,我們怎么辦呢?就得用js啦。

[javascript] view plaincopy

var imgObj = document.getElementById('imgToGray');

function gray(imgObj) {

var canvas = document.createElement('canvas');

var canvasContext = canvas.getContext('2d');

var imgW = imgObj.width;

var imgH = imgObj.height;

canvas.width = imgW;

canvas.height = imgH;

canvasContext.drawImage(imgObj, 0, 0);

var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);

for (var y = 0; y < imgPixels.height; y++) {

for (var x = 0; x < imgPixels.width; x++) {

var i = (y * 4) * imgPixels.width + x * 4;

var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;

imgPixels.data[i] = avg;

imgPixels.data[i + 1] = avg;

imgPixels.data[i + 2] = avg;

}

}

canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);

return canvas.toDataURL();

}

imgObj.src = gray(imgObj);

該解決方案主要參考ajaxblender的《Convert Images to Grayscale》,大家可以繼續閱讀查閱詳情。

5.跨瀏覽器解決方案

該方案主要參考《Cross-Browser Grayscale image example using CSS3 + JS v2.0. With browser feature detection using Modernizr》實現,英語沒問題的童鞋可以移步觀賞。博主寫了兩篇關于使用js實現跨瀏覽器解決灰度圖像問題的博客,第一篇自行實現瀏覽器檢測,第二篇使用了Modernizr。

該解決方案使用了jQuery和Modernizr,所以需要引入,此處省略,不知道怎么引入的童鞋,請自覺撞南墻100次。

對于IE瀏覽器

[javascript] view plaincopy

// IE 10 only CSS properties

var ie10Styles = [

'msTouchAction',

'msWrapFlow'];

var ie11Styles = [

'msTextCombineHorizontal'];

/*

* Test all IE only CSS properties

*/

var d = document;

var b = d.body;

var s = b.style;

var brwoser = null;

var property;

// Tests IE10 properties

for (var i = 0; i < ie10Styles.length; i++) {

property = ie10Styles[i];

if (s[property] != undefined) {

brwoser = "ie10";

}

}

// Tests IE11 properties

for (var i = 0; i < ie11Styles.length; i++) {

property = ie11Styles[i];

if (s[property] != undefined) {

brwoser = "ie11";

}

}

//Grayscale images only on browsers IE10+ since they removed support for CSS grayscale filter

if(brwoser == "ie10" || brwoser == "ie11" ){

$('body').addClass('ie11'); // Fixes marbin issue on IE10 and IE11 after canvas function on images

$('.grayscale img').each(function(){

var el = $(this);

el.css({"position":"absolute"}).wrap("

").clone().addClass('img_grayscale ieImage').css({"position":"absolute","z-index":"5","opacity":"0"}).insertBefore(el).queue(function(){

var el = $(this);

el.parent().css({"width":this.width,"height":this.height});

el.dequeue();

});

this.src = grayscaleIe(this.src);

});

// Quick animation on IE10+

$('.grayscale img').hover(

function () {

$(this).parent().find('img:first').stop().animate({opacity:1}, 200);

},

function () {

$('.img_grayscale').stop().animate({opacity:0}, 200);

}

);

// Custom grayscale function for IE10 and IE11

function grayscaleIe(src){

var canvas = document.createElement('canvas');

var ctx = canvas.getContext('2d');

var imgObj = new Image();

imgObj.src = src;

canvas.width = imgObj.width;

canvas.height = imgObj.height;

ctx.drawImage(imgObj, 0, 0);

var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);

for(var y = 0; y < imgPixels.height; y++){

for(var x = 0; x < imgPixels.width; x++){

var i = (y * 4) * imgPixels.width + x * 4;

var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;

imgPixels.data[i] = avg;

imgPixels.data[i + 1] = avg;

imgPixels.data[i + 2] = avg;

}

}

ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);

return canvas.toDataURL();

};

};

對于其他瀏覽器

[javascript] view plaincopy

// If the browser does not support CSS filters filters, we are applying grayscale.js function

// This part of Grayscale images applies on Opera, Firefox and Safari browsers

if (!Modernizr.cssfilters) {

var $images = $(".grayscale img"), imageCount = $images.length, counter = 0;

// One instead of on, because it need only fire once per image

$images.one("load",function(){

// increment counter every time an image finishes loading

counter++;

if (counter == imageCount) {

// do stuff when all have loaded

grayscale($('.grayscale img'));

$(".grayscale img").hover(

function () {

grayscale.reset($(this));

},

function () {

grayscale($(this));

}

);

}

}).each(function () {

if (this.complete) {

// manually trigger load event in

// event of a cache pull

$(this).trigger("load");

}

});

}

DEMO就不自己做了,大家看老外的DEMO的。That's all.

---------------------------------------------------------------

前端開發whqet,關注web前端開發技術,分享網頁相關資源。

---------------------------------------------------------------

總結

以上是生活随笔為你收集整理的marbin mysql_跨浏览器图像灰度(grayscale)解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。