开发者

jQuery Color **Swatch** Picker

Has anyone coded up a jQuery query color picker that lets you pick colors from a predetermined list of colors? Something like a produ开发者_StackOverflow中文版ct color picker on an Ecommerce site.

Most of the searching I've done reveals a lot of general purpose, pick-any-color-in-the-rgb-spectrum, but few options for picking specific colors.


I made a colorpicker that provides this as an option if anyone is interested:

Here is a working demo: http://jsfiddle.net/bgrins/S45tW/

$("#showPaletteOnly").spectrum({
    color: "rgb(244, 204, 204)",  
    showPaletteOnly: true,
    palette: [
        ["rgb(0, 0, 0)", "rgb(67, 67, 67)", "rgb(102, 102, 102)",
        "rgb(204, 204, 204)", "rgb(217, 217, 217)","rgb(255, 255, 255)"],
        ["rgb(152, 0, 0)", "rgb(255, 0, 0)", "rgb(255, 153, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)",
        "rgb(0, 255, 255)", "rgb(74, 134, 232)", "rgb(0, 0, 255)", "rgb(153, 0, 255)", "rgb(255, 0, 255)"], 
        ["rgb(230, 184, 175)", "rgb(244, 204, 204)", "rgb(252, 229, 205)", "rgb(255, 242, 204)", "rgb(217, 234, 211)", 
        "rgb(208, 224, 227)", "rgb(201, 218, 248)", "rgb(207, 226, 243)", "rgb(217, 210, 233)", "rgb(234, 209, 220)", 
        "rgb(221, 126, 107)", "rgb(234, 153, 153)", "rgb(249, 203, 156)", "rgb(255, 229, 153)", "rgb(182, 215, 168)", 
        "rgb(162, 196, 201)", "rgb(164, 194, 244)", "rgb(159, 197, 232)", "rgb(180, 167, 214)", "rgb(213, 166, 189)", 
        "rgb(204, 65, 37)", "rgb(224, 102, 102)", "rgb(246, 178, 107)", "rgb(255, 217, 102)", "rgb(147, 196, 125)", 
        "rgb(118, 165, 175)", "rgb(109, 158, 235)", "rgb(111, 168, 220)", "rgb(142, 124, 195)", "rgb(194, 123, 160)",
        "rgb(166, 28, 0)", "rgb(204, 0, 0)", "rgb(230, 145, 56)", "rgb(241, 194, 50)", "rgb(106, 168, 79)",
        "rgb(69, 129, 142)", "rgb(60, 120, 216)", "rgb(61, 133, 198)", "rgb(103, 78, 167)", "rgb(166, 77, 121)",
        "rgb(91, 15, 0)", "rgb(102, 0, 0)", "rgb(120, 63, 4)", "rgb(127, 96, 0)", "rgb(39, 78, 19)", 
        "rgb(12, 52, 61)", "rgb(28, 69, 135)", "rgb(7, 55, 99)", "rgb(32, 18, 77)", "rgb(76, 17, 48)"]
    ]
});
​


There's Really Simple Color Picker which allows you to set predefined colours (I'm British :-P). It does allow you to change the colour with an input box, but a quick jQuery command would allow you to disable it if you wanted predefined colours only.


Hmm, I assume you don't want a drop down with named values.

What about picking your colours, and then doing a loop

$.each(['red', 'blue', 'green'], function(i, colour) {

    $('ul#my-colours').append('<li style="background-color: ' + colour + ';">Choose</li>');

});

$('ul#my-colours li').click(function() {
    setColour($(this).css('background-color'));
});

And then make them say 20 x 20 px and float them?


Maybe I am a little late to add my two pennies, but here is the best predefined colour swatch groups I have found: https://github.com/maxmackie/jquery.swatches


Hell yea they have! I found this gem the other day. http://acko.net/dev/farbtastic

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜