输入类型颜色样式

新手上路,请多包涵

我有一个小问题,因为我不太擅长 CSS,所以我想知道是否有任何方法可以重新创建如此漂亮的 input[type=color] 元素的样式?我的意思不是屏幕截图上的所有内容,而是输入 [type=color] 那个漂亮的圆圈。

在此处输入图像描述

这里我有一些用它编写的代码,但它没有像屏幕截图上那样设置输入 [type=color] 的样式…我还看到作者在他的项目中使用了 Vue.js …非常感谢帮助!

 <input type="color" id="primary_color" class="field-radio" name="primary-color" v-model="scheme.primary" @change="changeColor()">

 input[type="color"] {
    width: 3rem;
    height: 3rem;
    padding: .5rem;
    background-color: transparent;
    border: 0;
    border-radius: 100%;
}
input[type="color" i] {
    -webkit-appearance: square-button;
    width: 44px;
    height: 23px;
    background-color: buttonface;
    cursor: default;
    border-width: 1px;
    border-style: solid;
    border-color: rgb(169, 169, 169);
    border-image: initial;
    padding: 1px 2px;
}
input {
    -webkit-appearance: textfield;
    background-color: white;
    -webkit-rtl-ordering: logical;
    cursor: text;
    padding: 1px;
    border-width: 2px;
    border-style: inset;
    border-color: initial;
    border-image: initial;
}
input, textarea, select, button {
    text-rendering: auto;
    color: initial;
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    margin: 0em;
    font: 400 13.3333px Arial;
}
input, textarea, select, button, meter, progress {
    -webkit-writing-mode: horizontal-tb;
}

原文由 user8690818 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 452
2 个回答

这是您需要的代码:

 let colorButton = document.getElementById("primary_color");
let colorDiv = document.getElementById("color_val");

colorButton.oninput = function() {
    colorDiv.innerHTML = colorButton.value;
    colorDiv.style.color = colorButton.value;
}
 #primary_color{
    border-radius: 50%;
    height: 60px;
    width: 60px;
    border: none;
    outline: none;
    -webkit-appearance: none;
}

#primary_color::-webkit-color-swatch-wrapper {
    padding: 0;
}
#primary_color::-webkit-color-swatch {
    border: none;
    border-radius: 50%;
}
 <div class="container">
    <input type="color" id="primary_color" class="field-radio">
    <span class="container" id="color_val"></span>
</div>

基本上,它会在 DOM 中找到 colorButton 并侦听此元素上的 input 事件。当它触发时,文本和文本的颜色将被更新。

原文由 SeReGa 发布,翻译遵循 CC BY-SA 4.0 许可协议

我不确定仅使用 html 和 CSS 是否可以实现这一点。但是使用 Javascript 你可以做出如下的东西:

  • 将输入颜色标签包装在边框半径为 50% 的标签中。
  • 隐藏输入并为标签设置背景。
  • 使用 javascript 根据输入颜色容器中更改的颜色更改标签的颜色。

Java脚本:

 var color_picker = document.getElementById("primary_color");
var color_picker_wrapper = document.getElementById("test_wrapper");
color_picker.onchange = function() {
  color_picker_wrapper.style.background = color_picker.value;
}
 #primary_color {
  visibility: hidden;
}

#test_wrapper {
  background: linear-gradient(to bottom right, red, orange, yellow, green, blue, violet);
  height: 32px;
  width: 32px;
  position: fixed;
  border-radius: 50%;
  box-shadow: 1px 4px 10px black;
}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="test_wrapper"><input type="color" id="primary_color" class="field-radio" name="primary-color" v-model="scheme.primary" @change="changeColor()"> </label>

查询:

 $(document).on('change', '#primary_color', function() {
  $("#test_wrapper").css('background', "" + document.getElementById('primary_color').value);
});
 #primary_color {
  visibility: hidden;
}

#test_wrapper {
  background: linear-gradient(to bottom right, red, orange, yellow, green, blue, violet);
  height: 32px;
  width: 32px;
  position: fixed;
  border-radius: 50%;
  box-shadow: 1px 4px 10px black;
}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="test_wrapper"><input type="color" id="primary_color" class="field-radio" name="primary-color" v-model="scheme.primary" @change="changeColor()"> </label>

原文由 user7207170 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题