Skip to content

Instantly share code, notes, and snippets.

@megurock
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save megurock/04258e1bec09d3aeaebc to your computer and use it in GitHub Desktop.

Select an option

Save megurock/04258e1bec09d3aeaebc to your computer and use it in GitHub Desktop.

Revisions

  1. megurock revised this gist Jul 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ail-filter.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    /*global $, jQuery, window */
    /* global $, jQuery, window */

    (function($) {

  2. megurock revised this gist Jul 30, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ail-filter.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    /*global $, jQuery, window */

    (function($) {

    /**
  3. megurock revised this gist Jul 29, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ail-filter.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@
    var regex = /url\(["']*(.*?)["']*\)/;
    var src = $node.css("background-image").replace(regex, "$1");
    var sizing = (sizingMethod === undefined) ? 'image' : sizingMethod;
    console.log("Filter onBgImage", $node, sizing);
    if (src !== 'none') {
    $node.css({
    backgroundImage: "none",
    @@ -22,7 +21,6 @@
    function applyFilterOnImage($node, sizingMethod) {
    var src = $node.attr('src');
    var sizing = (sizingMethod === undefined) ? 'image' : sizingMethod;
    console.log("Filter onImage", $node, sizing);
    if (src.indexOf('.png') !== -1) {
    $node.css({
    filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="' + sizing + '")'
  4. megurock created this gist Jul 29, 2014.
    76 changes: 76 additions & 0 deletions ail-filter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    (function($) {

    /**
    *
    */
    function applyFilterOnBackgroundImage($node, sizingMethod) {
    var regex = /url\(["']*(.*?)["']*\)/;
    var src = $node.css("background-image").replace(regex, "$1");
    var sizing = (sizingMethod === undefined) ? 'image' : sizingMethod;
    console.log("Filter onBgImage", $node, sizing);
    if (src !== 'none') {
    $node.css({
    backgroundImage: "none",
    filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="' + sizing + '")'
    });
    }
    }

    /**
    *
    */
    function applyFilterOnImage($node, sizingMethod) {
    var src = $node.attr('src');
    var sizing = (sizingMethod === undefined) ? 'image' : sizingMethod;
    console.log("Filter onImage", $node, sizing);
    if (src.indexOf('.png') !== -1) {
    $node.css({
    filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="' + sizing + '")'
    });
    }
    }

    /**
    *
    */
    function isImageElement($node) {
    var isImage, tagName;

    try {
    tagName = $node.get(0).tagName.toLowerCase();
    } catch(error) {}

    isImage = (tagName === 'img');
    return isImage;
    }

    /**
    *
    */
    function applyFilter($node, sizingMethod) {
    if (isImageElement($node)) {
    applyFilterOnImage($node, sizingMethod);
    } else {
    applyFilterOnBackgroundImage($node, sizingMethod);
    }
    }

    /**
    *
    */
    function isLessThanIE9() {
    return (typeof window.addEventListener === 'undefined' && typeof document.getElementsByClassName === 'undefined');
    }


    /**
    * @param sizingMethod:String 'image(default)', 'scale' or 'crop'
    */
    $.fn.applyAILFilter = function(sizingMethod) {
    this.each(function() {
    applyFilter($(this), sizingMethod);
    });
    return this;
    }

    })(jQuery);