一个简单的选择颜色的jQuery,就像你在Adobe Photoshop中选择颜色一样
MIT和GPL许可证下的双重许可
平坦模式
$('#colorpickerHolder').ColorPicker({flat: true});
自定义皮肤并使用平面模式在自定义小部件中显示颜色选择器。
附加到文本字段并使用回调函数用字段的值更新颜色,并通过提交颜色将值设置回字段中。
$('#colorpickerField1, #colorpickerField2, #colorpickerField3').ColorPicker({
onSubmit: function(hsb, hex, rgb, el) {
$(el).val(hex);
$(el).ColorPickerHide();
},
onBeforeShow: function () {
$(this).ColorPickerSetColor(this.value);
}
})
.bind('keyup', function(){
$(this).ColorPickerSetColor(this.value);
});
附加到DOMElement并使用回调实时预览颜色和添加动画。
$('#colorSelector').ColorPicker({
color: '#0000ff',
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
$('#colorSelector div').css('backgroundColor', '#' + hex);
}
});
colorpicker.zip (73 kb): 包含js和css文件
将Javascript和CSS文件加到文档中。编辑CSS文件,修复图片的路径,改变颜色以适应你的网站主题。
<link rel="stylesheet" media="screen" type="text/css" href="css/colorpicker.css" />
<script type="text/javascript" src="js/colorpicker.js"></script>
您所要做的就是以jQuery方式选择元素并调用插件。
$('input').ColorPicker(options);
参数的散列。所有参数都是可选的。
| eventName | string | The desired event to trigger the colorpicker. Default: 'click' |
| color | string or hash | The default color. String for hex color or hash for RGB and HSB ({r:255, r:0, b:0}) . Default: 'ff0000' |
| flat | boolean | Whatever if the color picker is appended to the element or triggered by an event. Default false |
| livePreview | boolean | Whatever if the color values are filled in the fields while changing values on selector or a field. If false it may improve speed. Default true |
| onShow | function | Callback function triggered when the color picker is shown |
| onBeforeShow | function | Callback function triggered before the color picker is shown |
| onHide | function | Callback function triggered when the color picker is hidden |
| onChange | function | Callback function triggered when the color is changed |
| onSubmit | function | Callback function triggered when the color it is chosen |
如果你想设置一个新的颜色
$('input').ColorPickerSetColor(color);
The 'color' argument is the same format as the option color, string for hex color or hash for RGB and HSB ({r:255, r:0, b:0}).