[removed] Invert color on all elements of a page

后端 未结 5 1918
逝去的感伤
逝去的感伤 2020-12-02 05:47

Note: I am keeping an up-to-date version of the bookmarklet in my question which works well and is based on Jacob\'s answer. If you are looking for a bookma

5条回答
  •  一个人的身影
    2020-12-02 05:47

    First things first, grab the awesome RGBColor class here.

    Here goes: jsFiddle example

    //set up color properties to iterate through
    var colorProperties = ['color', 'background-color'];
    
    //iterate through every element in reverse order...
    $("*").get().reverse().each(function() {
        var color = null;
    
        for (var prop in colorProperties) {
            prop = colorProperties[prop];
    
            //if we can't find this property or it's null, continue
            if (!$(this).css(prop)) continue; 
    
            //create RGBColor object
            color = new RGBColor($(this).css(prop));
    
            if (color.ok) { 
                //good to go, let's build up this RGB baby!
                //subtract each color component from 255
                $(this).css(prop, 'rgb(' + (255 - color.r) + ', ' + (255 - color.g) + ', ' + (255 - color.b) + ')');
            }
            color = null; //some cleanup
        }
    });
    

    Screenshot:

    alt text

    EDIT: Here's a bookmarklet you can now copy-paste into your browser (http://jsfiddle.net/F7HqS/1/)

    javascript:function load_script(src,callback){var s=document.createElement('script');s.src=src;s.onload=callback;document.getElementsByTagName('head')[0].appendChild(s);}function invertColors(){var colorProperties=['color','background-color'];$('*').each(function(){var color=null;for(var prop in colorProperties){prop=colorProperties[prop];if(!$(this).css(prop))continue;color=new RGBColor($(this).css(prop));if(color.ok){$(this).css(prop,'rgb('+(255-color.r)+','+(255-color.g)+','+(255-color.b)+')');}color=null;}});}load_script('http://www.phpied.com/files/rgbcolor/rgbcolor.js',function(){if(!window.jQuery)load_script('https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',invertColors);else invertColors();});
    

提交回复
热议问题