Skip to content

Commit 57c1996

Browse files
committed
New positionCallback
Added new callback to customary position the colorPicker on toggle.
1 parent cd0e084 commit 57c1996

File tree

6 files changed

+47
-7
lines changed

6 files changed

+47
-7
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ $('.color').colorPicker({
4949
renderCallback: function($elm, toggled) {}, // this === instance; $elm: the input field;toggle === true -> just appeared; false -> opposite; else -> is rendering on pointer move
5050
// toggled true/false can for example be used to check if the $elm has a certain className and then hide alpha,...
5151
buildCallback: function($elm) {}, // this === instance; $elm: the UI
52+
positionCallback: function($elm) {return {top: y, left: x}}, // this === instance; $elm: the trigger element;
5253
css: '', // replaces existing css
5354
cssAddon: '', // adds css to existing
5455
margin: '', // positioning margin (can also be set in cssAddon)
@@ -63,6 +64,26 @@ $('.color').colorPicker({
6364
```
6465
#### Some tips
6566

67+
The positionCallback can be used to optionally position the colorPicker different from its default; in case you want it to also show above or to the left of the input field etc.
68+
The callback will also be called on scroll.
69+
You need to return an object that holds ```left``` and ```top``` to position the colorPicker. See ./demo/index.js for an example:
70+
71+
```javascript
72+
positionCallback: function($elm) {
73+
var _$UI = this.$UI, // this is the instance; this.$UI is the colorPicker DOMElement
74+
position = $elm.offset(), // $elm is the current trigger that opened the UI
75+
gap = this.color.options.gap, // this.color.options stores all options
76+
top = 0,
77+
left = 0;
78+
79+
// do here your calculations with top and left and...
80+
return { // the object will be used as in $('.something').css({...});
81+
left: left,
82+
top: top
83+
}
84+
}
85+
```
86+
6687
The renderCallback can be used as openCallback and closeCallback:
6788

6889
```javascript

demo/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ $(function(){
6363
// append css after just generated / use cssAddon instead if you want
6464
$('#colorPickerMod').appendTo('head');
6565
},
66+
positionCallback: function($elm) { // optional function to position colorPicker on toggle
67+
var _$UI = this.$UI, // this is the instance; this.$UI is the colorPicker DOMElement
68+
position = $elm.offset(), // $elm is the current trigger / element that opened the colorPicker
69+
$window = $(window),
70+
gap = this.color.options.gap; // this.color.options stores all options
71+
72+
return { // this demo is a copy of the internal usage (to show how it works);
73+
'left': (_$UI._left = position.left) -
74+
((_$UI._left += _$UI._width -
75+
($window.scrollLeft() + $window.width())) + gap > 0 ?
76+
_$UI._left + gap : 0),
77+
'top': (_$UI._top = position.top + $elm.outerHeight()) -
78+
((_$UI._top += _$UI._height -
79+
($window.scrollTop() + $window.height())) + gap > 0 ?
80+
_$UI._top + gap : 0)
81+
}
82+
},
6683
renderCallback: function($elm, toggled) {
6784
var colors = this.color.colors; // the whole color object
6885
var rgb = colors.RND.rgb; // the RGB color in 0-255

jqColorPicker.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
function extractValue(elm) {
6666
return elm.value || elm.getAttribute('value') ||
67-
$(elm).css('background-color') || '#fff';
67+
$(elm).css('background-color') || '#FFF';
6868
}
6969

7070
function resolveEventType(event) {
@@ -90,8 +90,9 @@
9090

9191
_colorPicker.$trigger = $this;
9292

93-
(_$UI || build()).css({
94-
'left': (_$UI._left = position.left) -
93+
(_$UI || build()).css(
94+
_options.positionCallback.call(_colorPicker, $this) ||
95+
{'left': (_$UI._left = position.left) -
9596
((_$UI._left += _$UI._width -
9697
($window.scrollLeft() + $window.width())) + gap > 0 ?
9798
_$UI._left + gap : 0),
@@ -275,6 +276,7 @@
275276
opacity: true,
276277
renderCallback: noop,
277278
buildCallback: noop,
279+
positionCallback: noop,
278280
body: document.body,
279281
scrollResize: true,
280282
gap: 4,

0 commit comments

Comments
 (0)