This time I will share jQuery Plugin and tutorial about Custom Color Picker With Predefined Colors – simple-color, hope it will help you in programming stack.
simple-color is a jQuery plugin that converts an input field into a customizable color picker where the users are able to pick a color from a predefined palette.
More Features:
- Change the background color of the input field accordingly.
- Live preview mode that dynamically changes the background when hovering over a color grid.
- Allows you to display the color code (hex) in the input field.
- Allows you to set color & close all color pickers programmatically.
How to use it:
1. Load the jquery.simple-color.min.js
script after loading the latest jQuery.
1 |
< script src = "/path/to/cdn/jquery.slim.min.js" ></ script > |
2 |
< script src = "/path/to/jquery.simple-color.min.js" ></ script > |
2. Create a normal input field for the color picker. You can specify the initial color using the value
attribute as follows:
1 |
< input class = "simple_color" value = "#336699" /> |
3. Just call the function simpleColor
on the input field and the plugin will do the rest.
2 |
$( '.simple_color' ).simpleColor(); |
4. You can also specify the initial color using the ‘defaultColor’ option. Default: ‘#fff’.
1 |
$( '.simple_color' ).simpleColor({ |
2 |
defaultColor: '#ff0000' |
5. Customize the pre-defined colors for the palette.
1 |
$( '.simple_color' ).simpleColor({ |
2 |
colors: [ '990033' , 'ff3366' , 'cc0033' , 'ff0033' ] |
6. Hide the regular input and create a custom ‘fake’ color input.
1 |
$( '.simple_color' ).simpleColor({ |
4 |
'border-style' : 'dashed' , |
6 |
'margin-bottom' : '2px' } |
7. Determine whether to display the color code in the input field. Default: false.
1 |
$( '.simple_color' ).simpleColor({ |
2 |
displayColorCode: true , |
3 |
colorCodeAlign: 'center' , |
8. Enable the live preview mode. Default: false.
1 |
$( '.simple_color' ).simpleColor({ |
9. Customize the appearance of the color picker.
1 |
$( '.simple_color' ).simpleColor({ |
10. Callback functions.
01 |
$( '.simple_color' ).simpleColor({ |
02 |
onSelect: function (colorCode, inputElement){ |
05 |
onCellEnter: function (colorCode, inputElement){ |
08 |
onClose: function (inputElement){ |
11. Set color.
1 |
$( 'input' ).setColor( '#cc3333' ); |
12. Close the color picker.
1 |
$( 'input' ).closeChooser(); |
This awesome jQuery plugin is developed by recurser. For more Advanced Usages, please check the demo page or visit the official website.