HEX/RGB图像颜色选择器插件 tinycolorpicker.js

  • 源码大小:207.94KB
  • 所需积分:1积分
  • 源码编号:19JP-3294
  • 浏览次数:544次
  • 最后更新:2023年05月27日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

一个小巧、可定制、移动友好的颜色选择器插件,允许您在提供的图像中选择像素颜色值

这个颜色选择器目前支持两种颜色模型(RGB和HEX),并与jQuery和Vanilla JavaScript完美配合。当你希望用户能够从他们已经添加到页面的图像中选择颜色时,这是理想的选择。

参见:

  • JavaScript中的10个最佳颜色选择器插件

如何起诉:

1.下载插件并将主JavaScript导入到文档中。

  1. <!-- jQuery Version -->
  2. <script src="/path/to/cdn/jquery.min.js"></script>
  3. <script src="/path/to/jquery.tinycolorpicker.min.js"></script>
  4.  
  5. <!-- Vanilla JavaScript -->
  6. <script src="/path/to/tinycolorpicker.min.js"></script>

2.为图像颜色选择器构建HTML。

  1. <div id="colorPicker">
  2. <a class="color"><div class="colorInner"></div></a>
  3. <div class="track"></div>
  4. <ul class="dropdown"><li></li></ul>
  5. <input type="hidden" class="colorInput"/>
  6. </div>

3.为图像颜色选择器设置样式的示例CSS。不要忘记替换轨道

  1. #colorPicker
  2. {
  3. width: 30px;
  4. height: 30px;
  5. position: relative;
  6. clear: both;
  7. margin: 80px auto 20px;
  8. }
  9.  
  10. #colorPicker .track {
  11. background: #EFEFEF url(example.png) no-repeat 50% 50%;
  12. height: 150px;
  13. width: 150px;
  14. padding: 10px;
  15. position: absolute;
  16. cursor: crosshair;
  17. float: left;
  18. left: -71px;
  19. top: -71px;
  20. display: none;
  21. border: 1px solid #ccc;
  22. z-index: 10;
  23. -webkit-border-radius: 150px;
  24. -moz-border-radius: 150px;
  25. border-radius: 150px;
  26. }
  27.  
  28. #colorPicker .color {
  29. width: 25px;
  30. height: 25px;
  31. padding: 1px;
  32. border: 1px solid #ccc;
  33. display: block;
  34. position: relative;
  35. z-index: 11;
  36. background-color: #EFEFEF;
  37. -webkit-border-radius: 27px;
  38. -moz-border-radius: 27px;
  39. border-radius: 27px;
  40. cursor: pointer;
  41. }
  42.  
  43. #colorPicker .colorInner {
  44. width: 25px;
  45. height: 25px;
  46. -webkit-border-radius: 27px;
  47. -moz-border-radius: 27px;
  48. border-radius: 27px;
  49. }
  50.  
  51. #colorPicker .dropdown {
  52. list-style: none;
  53. display: none;
  54. width: 27px;
  55. position: absolute;
  56. top: 28px;
  57. border: 1px solid #ccc;
  58. left: 0;
  59. z-index: 1000;
  60. }
  61.  
  62. #colorPicker .dropdown li{
  63. height: 25px;
  64. cursor: pointer;
  65. }

4.调用函数锡色拾取器在顶部容器上,插件将完成其余工作。

  1. // jQuery Version
  2. $(document).ready(function(){
  3. var $box = $('#colorPicker');
  4. $box.tinycolorpicker();
  5. });
  6.  
  7. // The plain javascript api is very similar to the jquery version with some exceptions.
  8. // There is no chaining like in the jquery api.
  9. // So when you create a instance it will return all methods and properties.
  10. var $picker = document.getElementById("colorPicker"),
  11. picker = tinycolorpicker($picker);

5.您还可以使用背景URL选项

  1. var $box = $('#colorPicker',{
  2. backgroundUrl: '/path/to/image',
  3. });

6.定义一个后备颜色数组。仅在旧版浏览器上需要。

  1. var $box = $('#colorPicker',{
  2. colors : ["#ffffff", "#A7194B","#FE2712"],
  3. });

7.API方法。

  1. // set color
  2. picker.setColor("#FF0000");
  3.  
  4. // convert HEX to RGB
  5. picker.hexToRgb();
  6.  
  7. // convert RGB to HEX
  8. picker.rgbToHex();
  9.  
  10. // close the color picker
  11. picker.close();

8.每次选择颜色时都会触发一个功能。

  1. var box = $box.data("plugin_tinycolorpicker");
  2. $box.on("change", function(){
  3. console.log(box.colorRGB);
  4. console.log(box.colorHEX);
  5. });

预览截图