您能够经过浏览DEMO页面源代码了解基本的使用方法html
const ColorPicker = require(`./lib/color-picker.js`).ColorPicker const colorPickerObject = new ColorPicker({ dom: document.getElementById('colorPicker'), // DOM 对象 value: '#00FF00' //和 {r:0,g:255,b:0} , '0F0' 以及 '00FF00' 具备相同做用 })
<head> <!-- .../ --> <!-- 引入css样式表 --> <link rel="stylesheet" href="./color-picker.css"> <!-- .../ --> </head>
<body> <!-- .../ --> <!-- 引入JS --> <script src="./color-picker.js"></script> <!-- .../ --> <body>
而后,您只须要实例化一个 ColorPicker 对象便可git
const pickers = [ new ColorPicker({ dom: document.getElementById('picker1'), value: '#0F0' }), new ColorPicker({ dom: document.getElementById('picker2'), value: { r: 0, g: 0, b: 255 } }), new ColorPicker({ dom: document.getElementById('picker3') }) ]
// 建立了一个 ColorPicker 数组 const pickers = [ new ColorPicker({ dom: document.getElementById('picker1'), value: '#0F0' }), new ColorPicker({ dom: document.getElementById('picker2'), value: { r: 0, g: 0, b: 255 } }), new ColorPicker({ dom: document.getElementById('picker3') }) ] pickers.forEach(colorP => colorP.addEventListener('change', event => { pickers.forEach(colorPs => { if (colorP !== colorPs) colorPs.value = colorP.value }) }) ) // 您能够按照如下多种方式格式化值 "hex", "rgb", "hsb" 或是简单的hex颜色 // 示例 const valueGetSample = new ColorPicker({ value: '#ABC' }) // 与 "#AABBCC" 相同 const hex = valueGetSample.getValue('hex') // "AABBCC" const rgb = valueGetSample.getValue('rgb') // {r: 170, g: 187, b: 204} const hsb = valueGetSample.getValue('hsb') // {h: 210, s: 0.16666666666666663, b: 0.8} const value = valueGetSample.getValue('value') // 与 valueGetSample.value 返回值相同
纯粹是由于github上找不到感受符合一个小玩具的设计风格基于原生的JS颜色选择器,
因而干脆本身写一个好了,万一还能骗几个Star呢?(逃)github
最后在玩着海王星和睡觉穿插着算是写了一个初版,草草写了一下readme就发出来了,以上,所有。api