Grayscale image with CSS on Safari 5.x

后端 未结 4 1839

I am trying to show some images on a page where they should be shown in grayscale, except on mouse hover when they smoothly transition into color. I\'ve ma

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 12:05

    It's really simple, actually:

    • http://www.karlhorky.com/2012/06/cross-browser-image-grayscale-with-css.html
    • http://jsfiddle.net/KDtAX/487/

    I tried using the javascript fallback, but it really made no sense, and it was really slow on large images. This made a lot more sense. Note that there is a new syntax for grayscale, and I had to manually edit the resulting minified CSS from LESS.

    Here's my mixin:

    .filter (...) {
        -webkit-filter: @arguments;
        -moz-filter: @arguments;
        -ms-filter: @arguments;
        -o-filter: @arguments;
        filter: @arguments;
    }
    

    And my class:

    .grayscale-hover, .home-image {
        filter: url("data:image/svg+xml;utf8,#grayscale"); /* Firefox 10+, Firefox on Android see http://jsfiddle.net/KDtAX/487/*/
        .filter(grayscale(1) blur(1px));
        filter: gray; /* IE6-9 */
        -webkit-backface-visibility: hidden; /* Fix for transition flickering */
        &:hover {
            .filter(none);
            filter: url("data:image/svg+xml;utf8,#grayscale");
        }
    }
    

    Works and tested in IE 6+, Firefox, Chrome.

提交回复
热议问题