[0] | 1 | $(function() { |
---|
| 2 | $('.colorpicker').colorPicker(); |
---|
| 3 | }); |
---|
| 4 | |
---|
| 5 | jQuery.fn.colorPicker = function() { |
---|
| 6 | $(this).each(function() { |
---|
| 7 | var colbox, f; |
---|
| 8 | var colbox = $('#jquery-colorpicker')[0]; |
---|
| 9 | |
---|
| 10 | if (colbox == undefined) { |
---|
| 11 | colbox = document.createElement('div'); |
---|
| 12 | colbox.id = 'jquery-colorpicker'; |
---|
| 13 | colbox.linkedto = null; |
---|
| 14 | |
---|
| 15 | $(colbox).css({border: '1px solid #000', width: '195px', background: '#fff', position: 'absolute'}); |
---|
| 16 | $(colbox).css({display: 'none'}); |
---|
| 17 | $('body').append(colbox); |
---|
| 18 | } |
---|
| 19 | f = $.farbtastic(colbox); |
---|
| 20 | f.linkTo(this); |
---|
| 21 | |
---|
| 22 | var handler = $(document.createElement('img')); |
---|
| 23 | handler.attr('src','images/picker.png'); |
---|
| 24 | handler.attr('alt',''); |
---|
| 25 | |
---|
| 26 | var span = $(document.createElement('span')); |
---|
| 27 | |
---|
| 28 | if ($(this).css('position') == 'absolute') { |
---|
| 29 | span.css('position','absolute'); |
---|
| 30 | span.css('top',$(this).css('top')); |
---|
| 31 | span.css('left',$(this).css('left')); |
---|
| 32 | $(this).css('position','static'); |
---|
| 33 | } else { |
---|
| 34 | span.css('position','relative'); |
---|
| 35 | } |
---|
| 36 | span.css('display','block'); |
---|
| 37 | |
---|
| 38 | span.css('width',($(this).width()+12)+'px'); |
---|
| 39 | span.css('padding','0 5px 0 0'); |
---|
| 40 | $(this).wrap(span); |
---|
| 41 | $(this).after(handler); |
---|
| 42 | |
---|
| 43 | var offset = $(this).offset(); |
---|
| 44 | handler.css({ |
---|
| 45 | position: 'absolute', |
---|
| 46 | top: 3, |
---|
| 47 | right: 0 |
---|
| 48 | }); |
---|
| 49 | |
---|
| 50 | handler.css({cursor: 'default'}); |
---|
| 51 | |
---|
| 52 | var This = this; |
---|
| 53 | handler.click(function() { |
---|
| 54 | if ($(colbox).css('display') == 'none' || this != colbox.linkedto) { |
---|
| 55 | f.linkTo(This); |
---|
| 56 | This.focus(); |
---|
| 57 | var offset = $(This).offset(); |
---|
| 58 | $(colbox).css({ |
---|
| 59 | zIndex: 1000, |
---|
| 60 | top: offset.top + $(this).height() + 5, |
---|
| 61 | left: offset.left |
---|
| 62 | }); |
---|
| 63 | if (document.all) { $('select').hide(); } |
---|
| 64 | $(colbox).show(); |
---|
| 65 | colbox.linkedto = this; |
---|
| 66 | |
---|
| 67 | } else { |
---|
| 68 | $(colbox).hide(); |
---|
| 69 | if (document.all) { $('select').show(); } |
---|
| 70 | colbox.linkedto = null; |
---|
| 71 | } |
---|
| 72 | }); |
---|
| 73 | $(this).blur(function() { |
---|
| 74 | $(colbox).hide(); |
---|
| 75 | if (document.all) { $('select').show(); } |
---|
| 76 | }); |
---|
| 77 | }); |
---|
| 78 | return this; |
---|
| 79 | }; |
---|