mirror of https://github.com/layui/layui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
6.9 KiB
6.9 KiB
<div id="ID-colorpicker-demo-1"></div> <div id="ID-colorpicker-demo-2" style="margin-left: 16px;"></div> <!-- import layui --> <script> layui.use(function(){ var colorpicker = layui.colorpicker; var layer = layui.layer; // 渲染 colorpicker.render({ // eg1 elem: '#ID-colorpicker-demo-1', // 绑定元素 change: function(color){ // 颜色改变的回调 layer.tips('选择了:'+ color, this.elem, { tips: 1 }); } }); colorpicker.render({ // eg2 elem: '#ID-colorpicker-demo-2', color: '#2ec770', // 设置默认色 done: function(color){ // 选择完毕的回调 layer.tips('选择了:'+ color, this.elem); } }); }); </script>
将颜色值赋给表单
<form class="layui-form" action=""> <div class="layui-form-item"> <div class="layui-input-inline" style="width: 120px;"> <input type="text" name="color" value="" placeholder="请选择颜色" class="layui-input" id="ID-colorpicker-demo-form-color"> </div> <div class="layui-inline" style="left: -11px;"> <div id="ID-colorpicker-demo-form"></div> </div> </div> </form> <!-- import layui --> <script> layui.use(function(){ var colorpicker = layui.colorpicker; var $ = layui.$; // 渲染 colorpicker.render({ elem: '#ID-colorpicker-demo-form', color: '#1c97f5', done: function(color){ $('#ID-colorpicker-demo-form-color').val(color); } }); }); </script>
设置 RGB / RGBA
<div id="ID-colorpicker-demo-rgb"></div> <div id="ID-colorpicker-demo-rgba" style="margin-left: 16px;"></div> <!-- import layui --> <script> layui.use(function(){ var colorpicker = layui.colorpicker; // 渲染 colorpicker.render({ // eg1 elem: '#ID-colorpicker-demo-rgb', color: 'rgb(68,66,66)', format: 'rgb' // 默认为 hex }); colorpicker.render({ // eg2 elem: '#ID-colorpicker-demo-rgba', color: 'rgba(68,66,66,0.5)', format: 'rgb', alpha: true // 开启透明度滑块 }); }); </script>
开启透明度
<div id="ID-colorpicker-demo-alpha-1"></div> <div id="ID-colorpicker-demo-alpha-2" style="margin-left: 16px;"></div> <div id="ID-colorpicker-demo-alpha-3" style="margin-left: 16px;"></div> <!-- import layui --> <script> layui.use(function(){ var colorpicker = layui.colorpicker; // 渲染 colorpicker.render({ // eg1 elem: '#ID-colorpicker-demo-alpha-1', color: '#16baaa', // hex alpha: true, // 开启透明度 format: 'rgb' }); colorpicker.render({ // eg2 elem: '#ID-colorpicker-demo-alpha-2', color: 'rgb(0,150,136,0.6)', // rgba alpha: true, format: 'rgb' }); colorpicker.render({ // eg3 elem: '#ID-colorpicker-demo-alpha-3', alpha: true, format: 'rgb' }); }); </script>
预定义颜色项
<div id="ID-colorpicker-demo-predefine-1"></div> <div id="ID-colorpicker-demo-predefine-2" style="margin-left: 16px;"></div> <!-- import layui --> <script> layui.use(function(){ var colorpicker = layui.colorpicker; // 渲染 colorpicker.render({ // eg1 elem: '#ID-colorpicker-demo-predefine-1', color: '#c71585', predefine: true // 开启预定义颜色 }); colorpicker.render({ // eg2 elem: '#ID-colorpicker-demo-predefine-2', color: '#9d8a0e', predefine: true, // 开启预定义颜色 colors: ['#ff8c00','#00ced1','#9d8a0e'] //自定义预定义颜色项 }); }); </script>
全功能和回调的使用
<button class="layui-btn" id="ID-colorpicker-demo-all-btn">主题按钮</button> <hr> <div id="ID-colorpicker-demo-all"></div> <span style="padding-left: 6px;">演示:选择颜色并确定,改变上方按钮背景色</span> <!-- import layui --> <script> layui.use(function(){ var colorpicker = layui.colorpicker; var layer = layui.layer; var $ = layui.$; // 渲染 colorpicker.render({ elem: '#ID-colorpicker-demo-all', color: '#16baaa', predefine: true, alpha: true, done: function(color){ layer.msg('选择的值:'+ color); // 选择完毕的颜色值 // 清空或取消选择时也执行 change color || this.change(color); }, change: function(color){ // 给当前页面头部和左侧设置主题色 $('#ID-colorpicker-demo-all-btn').css('background-color', color); }, cancel: function(color){ // 取消颜色选择的回调 --- 2.8+ this.change(color); } }); }); </script>
颜色框尺寸
<div id="ID-colorpicker-demo-size-lg"></div> <div id="ID-colorpicker-demo-size-sm" style="margin-left: 16px;"></div> <div id="ID-colorpicker-demo-size-xs" style="margin-left: 16px;"></div> <!-- import layui --> <script> layui.use(function(){ var colorpicker = layui.colorpicker; // 渲染 colorpicker.render({ elem: '#ID-colorpicker-demo-size-lg', size: 'lg' // 大号下拉框 }); colorpicker.render({ elem: '#ID-colorpicker-demo-size-sm' //,size: 'sm' // 默认 sm }); colorpicker.render({ elem: '#ID-colorpicker-demo-size-xs', size: 'xs' // mini 下拉框 }); }); </script>
同时绑定多个元素
<div class="demo-class-colorpicker" lay-options="{color: '#FF0000'}"></div> <div class="demo-class-colorpicker" lay-options="{color: '#008000'}"></div> <div class="demo-class-colorpicker" lay-options="{color: '#0000FF'}"></div> <!-- import layui --> <script> layui.use(function(){ var colorpicker = layui.colorpicker; // 渲染 colorpicker.render({ elem: '.demo-class-colorpicker', done: function(color){ console.log(this.elem, color); } }); }); </script>