tags:

views:

170

answers:

1

I have a jQuery jPicker colorpicker that is bound to an input text field and opens onclick of a small picker.gif icon. The problem I'm having is that (1) The colorpicker that opens does not appear to be positioned according to the x/y position of the picker.gif (it opens far away from the click point) and (2) The colorpicker does not seem to be aware of the viewport's scroll position (the top of the colorpicker is partially hidden at the top of the window).

I'd like to use jQuery to reposition the colorpalette (1) Based on the x/y position of the input that its bound to and (2) reset it's top position based on the viewport's visible Y position.

Here is the script where I am creating a new jPicker and binding it to my input text fields for header and sidebar...

$('#theme_header_color').jPicker
(
    {position: { x: $(this).offset.left + $(this).width(), y: ($(this).offset.top - $(window).scrollTop()) + $(this).height() }},
    function(color) 
    { 
        $(this).val(color.get_Hex());
    },
    function(color) 
    { 
        $(this).val(color.get_Hex()); 
    }
);

$('#theme_sidebar_color').jPicker
(
    {position: { x: $(this).offset.left + $(this).width(), y: ($(this).offset.top - $(window).scrollTop()) + $(this).height() }},
    function(color) 
    { 
        $(this).val(color.get_Hex());
    },
    function(color) 
    { 
        $(this).val(color.get_Hex()); 
    }
)
A: 

jPicker is already setup to take x/y coordinates so you should be able to pass the offset of the opening button. You might have to adjust for the height and width of the button and any padding depending on your taste. It's untested but adding this to the jPicker settings should do it.

position: { x: $(this).offset.left, y: $(this).offset.top }

To account for the buttons height and width.

position: { x: $(this).offset.left + $(this).width(), y: $(this).offset.top + $(this).height() }

UPDATE

To account for the vertical scroll position

position: { x: $(this).offset.left + $(this).width(), y: ($(this).offset.top - $(window).scrollTop()) + $(this).height() }
Shaun Humphries
Thanks Shaun, where in my code above, would you place the position statement? I've put it into the empty brackets {}, but its not setting the top relative to the scroll position (ie, the top of the colorpicker is still hidden when the picker.gif icon is near the top of the window when clicked.
Scott B
You put it in the right plcae. I have updated the above code to account for the scroll position.
Shaun Humphries
Shaun, I'm not sure what I'm doing wrong, but the code does not appear to be affecting the x/y position. I've edited my question with the updated code. Do you see anything wrong?
Scott B
You need to put the position inside the window setting{ window: { position: { y: '', x: '' } } }After creating a demo to test my answer out I realised that there is no easy way to do this especially in the given context. My suggestion would be to builda function that is called on the load of the color picker and can check the scroll position/window size to it's relative position.
Shaun Humphries