mirror of
https://github.com/PitPik/tinyColorPicker
synced 2025-07-02 23:54:29 -06:00
added support for triggering nested elements
(Nested) elements besides input fields can now also be used for triggering the colorPicker
This commit is contained in:
parent
9ef3cc8712
commit
2e20c96ee7
37
index.css
37
index.css
@ -27,3 +27,40 @@ pre {
|
|||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
width: 140px;
|
width: 140px;
|
||||||
}
|
}
|
||||||
|
.div-toggles {
|
||||||
|
margin-top: 1em;
|
||||||
|
}
|
||||||
|
.div-toggles .color {
|
||||||
|
position: relative;
|
||||||
|
display: inline-block;
|
||||||
|
width: 40px;
|
||||||
|
padding: 5px;
|
||||||
|
height: 25px;
|
||||||
|
border: 1px solid #666;
|
||||||
|
border-radius: 2px;
|
||||||
|
background: #eee;
|
||||||
|
}
|
||||||
|
.div-toggles .color:after {
|
||||||
|
content: "";
|
||||||
|
display: block;
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
right: 5px;
|
||||||
|
margin-top: -2px;
|
||||||
|
border: 5px solid;
|
||||||
|
border-color: #000 transparent transparent;
|
||||||
|
}
|
||||||
|
.div-toggles .active:after {
|
||||||
|
margin-top: -8px;
|
||||||
|
border-color: transparent transparent #000;
|
||||||
|
}
|
||||||
|
.div-toggles .color div {
|
||||||
|
display: inline-block;
|
||||||
|
width: 23px;
|
||||||
|
height: 23px;
|
||||||
|
background: #fff url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMAQMAAABsu86kAAAABlBMVEXMzMwAAADTMzNkAAAAAnRSTlP/AOW3MEoAAAARSURBVAjXY2D+wICG/jCgIQDPPwub2NOsgwAAAABJRU5ErkJggg==');
|
||||||
|
}
|
||||||
|
.div-toggles .color div div {
|
||||||
|
border: 1px solid #666;
|
||||||
|
background: #aaa;
|
||||||
|
}
|
23
index.html
23
index.html
@ -14,7 +14,7 @@
|
|||||||
This <strong>4.4KB</strong> small HSB color picker is based on a subset of <a href="https://github.com/PitPik/colorPicker/blob/master/colors.js">colors.js</a> from it's big brother <a href="http://dematte.at/colorPicker/">colorPicker</a>, supports all modern features like touch and MS pointer, GPU accelerated rendering, battery friendly requestAnimationFrame and provides a lot of hooks for developers to write plugins (See demo plugins below in <a href="#demo">Demo</a>).</p>
|
This <strong>4.4KB</strong> small HSB color picker is based on a subset of <a href="https://github.com/PitPik/colorPicker/blob/master/colors.js">colors.js</a> from it's big brother <a href="http://dematte.at/colorPicker/">colorPicker</a>, supports all modern features like touch and MS pointer, GPU accelerated rendering, battery friendly requestAnimationFrame and provides a lot of hooks for developers to write plugins (See demo plugins below in <a href="#demo">Demo</a>).</p>
|
||||||
<a name="demo" id="demo"></a>
|
<a name="demo" id="demo"></a>
|
||||||
<h2>Demo</h2>
|
<h2>Demo</h2>
|
||||||
On all inputs with className 'color': <pre>$('input.color').colorPicker();</pre>
|
On all elements with className 'color': <pre>$('.color').colorPicker();</pre>
|
||||||
<p>Switch between plugins:
|
<p>Switch between plugins:
|
||||||
<select id="pluginSelect">
|
<select id="pluginSelect">
|
||||||
<option value="desktop">Desktop (no plugin)</option>
|
<option value="desktop">Desktop (no plugin)</option>
|
||||||
@ -28,11 +28,16 @@ On all inputs with className 'color': <pre>$('input.color').colorPicker();</pre>
|
|||||||
</select>
|
</select>
|
||||||
(see index.html for more details...)
|
(see index.html for more details...)
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<div class="input-toggles">
|
||||||
<input class="color" value="#B6BD79" />
|
<input class="color" value="#B6BD79" />
|
||||||
<input class="color" value="rgb(162, 63, 3)" />
|
<input class="color" value="rgb(162, 63, 3)" />
|
||||||
<input class="color" value="hsl(32, 95%, 23%)" />
|
<input class="color" value="hsl(32, 95%, 23%)" />
|
||||||
</p>
|
</div>
|
||||||
|
<div class="div-toggles">
|
||||||
|
<div class="color" value="#44A647"><div><div></div></div></div>
|
||||||
|
<div class="color" value="rgb(100, 86, 70)"><div><div></div></div></div>
|
||||||
|
<div class="color" value="hsl(167, 29%, 68%)"><div><div></div></div></div>
|
||||||
|
</div>
|
||||||
<h2>Features</h2>
|
<h2>Features</h2>
|
||||||
<p>Tiny jQuery colorPicker only loads if triggered to show. It uses battery saving technologies and super fast rendering for best performance on desktop and mobile browsers.<br>
|
<p>Tiny jQuery colorPicker only loads if triggered to show. It uses battery saving technologies and super fast rendering for best performance on desktop and mobile browsers.<br>
|
||||||
This colorPicker is very flexible to modify and customize and there is an easy way to write plugins to extend functionality, look and feel...<br>
|
This colorPicker is very flexible to modify and customize and there is an easy way to write plugins to extend functionality, look and feel...<br>
|
||||||
@ -96,6 +101,7 @@ window.myColorPicker = $('input.color').colorPicker({
|
|||||||
plugin.mobile = {
|
plugin.mobile = {
|
||||||
customBG: '#222',
|
customBG: '#222',
|
||||||
margin: '4px -2px',
|
margin: '4px -2px',
|
||||||
|
doRender: 'div div',
|
||||||
|
|
||||||
// demo on how to make plugins... mobile support plugin
|
// demo on how to make plugins... mobile support plugin
|
||||||
buidCallback: function($elm) {
|
buidCallback: function($elm) {
|
||||||
@ -126,6 +132,7 @@ window.myColorPicker = $('input.color').colorPicker({
|
|||||||
plugin.input = {
|
plugin.input = {
|
||||||
customBG: '#222',
|
customBG: '#222',
|
||||||
margin: '4px -2px',
|
margin: '4px -2px',
|
||||||
|
doRender: 'div div',
|
||||||
|
|
||||||
buidCallback: function($elm) {
|
buidCallback: function($elm) {
|
||||||
var colorInstance = this.color,
|
var colorInstance = this.color,
|
||||||
@ -184,6 +191,7 @@ window.myColorPicker = $('input.color').colorPicker({
|
|||||||
plugin.memory = {
|
plugin.memory = {
|
||||||
customBG: '#222',
|
customBG: '#222',
|
||||||
margin: '4px -2px',
|
margin: '4px -2px',
|
||||||
|
doRender: 'div div',
|
||||||
opacity: false,
|
opacity: false,
|
||||||
|
|
||||||
buidCallback: function($elm) {
|
buidCallback: function($elm) {
|
||||||
@ -224,13 +232,15 @@ window.myColorPicker = $('input.color').colorPicker({
|
|||||||
// the default; no extra stuff
|
// the default; no extra stuff
|
||||||
plugin.desktop = {
|
plugin.desktop = {
|
||||||
customBG: '#222',
|
customBG: '#222',
|
||||||
margin: '4px -2px'
|
margin: '4px -2px',
|
||||||
|
doRender: 'div div'
|
||||||
};
|
};
|
||||||
|
|
||||||
// the default without alpha chanel though
|
// the default without alpha chanel though
|
||||||
plugin.noalpha = {
|
plugin.noalpha = {
|
||||||
customBG: '#222',
|
customBG: '#222',
|
||||||
margin: '4px -2px',
|
margin: '4px -2px',
|
||||||
|
doRender: 'div div',
|
||||||
opacity: false
|
opacity: false
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -238,6 +248,7 @@ window.myColorPicker = $('input.color').colorPicker({
|
|||||||
plugin.small = {
|
plugin.small = {
|
||||||
customBG: '#222',
|
customBG: '#222',
|
||||||
margin: '4px -2px',
|
margin: '4px -2px',
|
||||||
|
doRender: 'div div',
|
||||||
cssAddon: // could also be in a css file instead
|
cssAddon: // could also be in a css file instead
|
||||||
'.cp-color-picker{padding:5px 5px 0;}' +
|
'.cp-color-picker{padding:5px 5px 0;}' +
|
||||||
'.cp-xy-slider{width:100px; height:100px; margin-bottom:3px;}' +
|
'.cp-xy-slider{width:100px; height:100px; margin-bottom:3px;}' +
|
||||||
@ -250,6 +261,7 @@ window.myColorPicker = $('input.color').colorPicker({
|
|||||||
plugin.skinned = {
|
plugin.skinned = {
|
||||||
customBG: '#222',
|
customBG: '#222',
|
||||||
margin: '5px -2px',
|
margin: '5px -2px',
|
||||||
|
doRender: 'div div',
|
||||||
cssAddon: // could also be in a css file instead
|
cssAddon: // could also be in a css file instead
|
||||||
'.cp-color-picker{border:1px solid #999; padding:10px;' +
|
'.cp-color-picker{border:1px solid #999; padding:10px;' +
|
||||||
'background:#eee; overflow:visible; border-radius:3px;}' +
|
'background:#eee; overflow:visible; border-radius:3px;}' +
|
||||||
@ -276,6 +288,7 @@ window.myColorPicker = $('input.color').colorPicker({
|
|||||||
plugin.dev_skinned = {
|
plugin.dev_skinned = {
|
||||||
customBG: '#222',
|
customBG: '#222',
|
||||||
margin: '5px -2px',
|
margin: '5px -2px',
|
||||||
|
doRender: 'div div',
|
||||||
|
|
||||||
buidCallback: function($elm) {
|
buidCallback: function($elm) {
|
||||||
$elm.append('<div class="cp-patch"><div></div></div><div class="cp-disp"></div>');
|
$elm.append('<div class="cp-patch"><div></div></div><div class="cp-disp"></div>');
|
||||||
@ -331,7 +344,7 @@ window.myColorPicker = $('input.color').colorPicker({
|
|||||||
window.location = './?type=' + this.value + '#demo'
|
window.location = './?type=' + this.value + '#demo'
|
||||||
});
|
});
|
||||||
|
|
||||||
window.myColorPicker = $('input.color').colorPicker(
|
window.myColorPicker = $('.color').colorPicker(
|
||||||
plugin[type] || plugin.desktop
|
plugin[type] || plugin.desktop
|
||||||
);
|
);
|
||||||
})(window);
|
})(window);
|
||||||
|
@ -46,7 +46,8 @@
|
|||||||
ColorPicker.prototype.toggle = toggle;
|
ColorPicker.prototype.toggle = toggle;
|
||||||
|
|
||||||
function extractValue(elm) {
|
function extractValue(elm) {
|
||||||
return elm.value || $(elm).css('background-color') || '#fff';
|
return elm.value || elm.getAttribute('value') ||
|
||||||
|
$(elm).css('background-color') || '#fff';
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveEventType(event) {
|
function resolveEventType(event) {
|
||||||
@ -54,13 +55,17 @@
|
|||||||
event.originalEvent.touches[0] : event;
|
event.originalEvent.touches[0] : event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findElement($elm) {
|
||||||
|
return $($elm.find(_options.doRender)[0] || $elm[0]);
|
||||||
|
}
|
||||||
|
|
||||||
function toggle(event) {
|
function toggle(event) {
|
||||||
var $this = $(this),
|
var $this = $(this),
|
||||||
position;
|
position;
|
||||||
|
|
||||||
if (event) {
|
if (event) {
|
||||||
position = $this.offset();
|
position = $this.offset();
|
||||||
_cache.$element = $this;
|
_cache.$element = findElement($this);
|
||||||
_options.preventFocus && event.preventDefault();
|
_options.preventFocus && event.preventDefault();
|
||||||
|
|
||||||
(_$UI || build()).css({
|
(_$UI || build()).css({
|
||||||
@ -69,12 +74,13 @@
|
|||||||
}).show(_options.animationSpeed, function() {
|
}).show(_options.animationSpeed, function() {
|
||||||
_cache.alphaWidth = $('.cp-alpha', _$UI).width();
|
_cache.alphaWidth = $('.cp-alpha', _$UI).width();
|
||||||
_cache.sliderWidth = $('.cp-xy-slider', _$UI).width();
|
_cache.sliderWidth = $('.cp-xy-slider', _$UI).width();
|
||||||
_color.setColor(extractValue($this[0]));
|
_color.setColor(extractValue(_cache.$element[0]));
|
||||||
_animate(function(){render(true)});
|
render(true);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
$(_$UI).hide(_options.animationSpeed, function() {
|
$(_$UI).hide(_options.animationSpeed, function() {
|
||||||
_cache.$element.blur();
|
_cache.$element.blur();
|
||||||
|
render(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -248,12 +254,15 @@
|
|||||||
_colorPicker = new ColorPicker(options);
|
_colorPicker = new ColorPicker(options);
|
||||||
|
|
||||||
$document.on('touchstart mousedown pointerdown', function(e) {
|
$document.on('touchstart mousedown pointerdown', function(e) {
|
||||||
if ($.inArray(e.target, $that) === -1 &&
|
var $target = $(e.target);
|
||||||
!$(e.target).closest(_$UI).length) {
|
|
||||||
|
if ($.inArray($target.closest($that.selector)[0],
|
||||||
|
$that) === -1 &&
|
||||||
|
!$target.closest(_$UI).length) {
|
||||||
toggle();
|
toggle();
|
||||||
}
|
}
|
||||||
}).
|
}).
|
||||||
on('focus', this.selector, toggle).
|
on('focus click', this.selector, toggle).
|
||||||
on('change', this.selector, function() {
|
on('change', this.selector, function() {
|
||||||
_color.setColor(this.value);
|
_color.setColor(this.value);
|
||||||
$that.colorPicker.render();
|
$that.colorPicker.render();
|
||||||
@ -267,7 +276,8 @@
|
|||||||
mode = value.split('(');
|
mode = value.split('(');
|
||||||
// save initial color mode and set color and bgColor
|
// save initial color mode and set color and bgColor
|
||||||
$(this).data('colorMode', mode[1] ? mode[0].substr(0, 3) : 'HEX');
|
$(this).data('colorMode', mode[1] ? mode[0].substr(0, 3) : 'HEX');
|
||||||
options.doRender && $(this).css({'background-color': value,
|
options.doRender && findElement($(this)).
|
||||||
|
css({'background-color': value,
|
||||||
'color': function() {
|
'color': function() {
|
||||||
return _color.setColor(value).
|
return _color.setColor(value).
|
||||||
rgbaMixBGMixCustom.luminance > 0.22 ? '#222' : '#ddd'
|
rgbaMixBGMixCustom.luminance > 0.22 ? '#222' : '#ddd'
|
||||||
|
File diff suppressed because one or more lines are too long
4
jqColorPicker.min.js
vendored
4
jqColorPicker.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user