jquery的颜色选择插件

忙了一夜,终于把jquery的颜色选择插件写完了,不多说了,看看代码吧。

  1. (function($){
  2. $.fn.extend({
  3. selectColor:function(){
  4. var_d=newDate();
  5. var_tem=_d.getTime();
  6. returnthis.each(function(){
  7. varshowColor=function(_obj){
  8. var_left=parseInt($(_obj).offset().left);
  9. var_top=parseInt($(_obj).offset().top);
  10. var_width=parseInt($(_obj).width());
  11. var_height=parseInt($(_obj).height());
  12. var_maxindex=function(){
  13. var___index=0;
  14. $.each($("*"),function(i,n){
  15. var__tem=$(n).css("z-index");
  16. if(__tem>0)
  17. {
  18. if(__tem>___index)
  19. {
  20. ___index=__tem+1;
  21. }
  22. }
  23. });
  24. return___index;
  25. }();
  26. varcolorH=newArray('00','33','66','99','CC','FF');
  27. varScolorH=newArray('FF0000','00FF00','0000FF','FFFF00','00FFFF','FF00FF');
  28. var_str=newArray();
  29. for(varn=0;n<6;n++)
  30. {
  31. _str.push('<trheight="11">');
  32. _str.push('<tdstyle="width:11px;background-color:#'+colorH[n]+colorH[n]+colorH[n]+'"></td>');
  33. for(vari=0;i<3;i++)
  34. {
  35. for(varj=0;j<6;j++)
  36. {
  37. _str.push('<tdstyle="width:11px;background-color:#'+colorH[i]+colorH[j]+colorH[n]+'"></td>');
  38. }
  39. }
  40. _str.push('</tr>');
  41. }
  42. for(varn=0;n<6;n++)
  43. {
  44. _str.push('<trheight="11">');
  45. _str.push('<tdstyle="width:11px;background-color:#'+ScolorH[n]+'"></td>')
  46. for(vari=3;i<6;i++)
  47. {
  48. for(varj=0;j<6;j++)
  49. {
  50. _str.push('<tdstyle="width:11px;background-color:#'+colorH[i]+colorH[j]+colorH[n]+'"></td>');
  51. }
  52. }
  53. _str.push('</tr>');
  54. }
  55. varcolorStr='<divid="colorShowDiv_'+_tem+'"style="width:229px;position:absolute;z-index:'+_maxindex+';left:'+(_left+_width)+'px;top:'+(_top+_height)+'px;"><tablestyle="width:100%;height:30px;background-color:#CCCCCC;"><tr><tdstyle="width:40%;"><divid="colorShow_'+_tem+'"style="width:80px;height:18px;border:solid1px#000000;background-color:#FFFFFF;"></div></td><tdstyle="width:60%;"><inputid="color_txt_'+_tem+'"type="text"style="width:100px;height:16px;"value="#FFFFFF"/></td></tr></table><tableid="colorShowTable_'+_tem+'"cellpadding="0"cellspacing="1"style="background-color:#000000;">'+_str.join('')+'</table></div>'
  56. $("body").append(colorStr);
  57. var_currColor;
  58. var_currColor2;
  59. $("#colorShowTable_"+_tem+"td").mouseover(function(){
  60. var_color=$(this).css("background-color");
  61. if(_color.indexOf("rgb")>=0)
  62. {
  63. var_tmeColor=_color;
  64. _tmeColor=_color.replace("rgb","");
  65. _tmeColor=_tmeColor.replace("(","");
  66. _tmeColor=_tmeColor.replace(")","");
  67. _tmeColor=_tmeColor.replace(newRegExp("","gm"),"");
  68. var_arr=_tmeColor.split(",");
  69. var_tmeColorStr="#";
  70. for(varii=0;ii<_arr.length;ii++)
  71. {
  72. var_temstr=parseInt(_arr[ii]).toString(16);
  73. _temstr=_temstr.length<2?"0"+_temstr:_temstr;
  74. _tmeColorStr+=_temstr;
  75. }
  76. }
  77. $("#color_txt_"+_tem).val(_tmeColorStr);
  78. $("#colorShow_"+_tem).css("background-color",_color);
  79. _currColor=_color;
  80. _currColor2=_tmeColorStr;
  81. $(this).css("background-color","#FFFFFF");
  82. });
  83. $("#colorShowTable_"+_tem+"td").mouseout(function(){
  84. $(this).css("background-color",_currColor);
  85. });
  86. $("#colorShowTable_"+_tem+"td").click(function(){
  87. $(_obj).val(_currColor2);
  88. });
  89. }
  90. $(this).click(function(){
  91. showColor(this);
  92. });
  93. var_sobj=this;
  94. $(document).click(function(e){
  95. e=e?e:window.event;
  96. vartag=e.srcElement||e.target;
  97. if(_sobj.id==tag.id)return;
  98. var_temObj=tag;
  99. while(_temObj)
  100. {
  101. if(_temObj.id=="colorShowDiv_"+_tem)return;
  102. _temObj=_temObj.parentNode;
  103. }
  104. $("#colorShowDiv_"+_tem).remove();
  105. });
  106. });
  107. }
  108. });
  109. })(jQuery);

使用方法:

  1. $(document).ready(function(){
  2. $("#要绑定的ID").selectColor();
  3. });

注意:要绑定的对像一定要是文本输入框

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值