您能夠經過瀏覽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