输入颜色打开在自定义按钮

huangapple go评论70阅读模式
英文:

Input color open at custom button

问题

你可以使用HTML和CSS来打开颜色选择器,但如果要在按钮按下时获取颜色值并进行特定操作,你需要使用JavaScript。谢谢。

英文:

How can i open an input type color when i press a custom button to get the color value? I want the color-palette button to open the input color and nothing else to show like the green color box.

<div class="color-picker">
    <button id="color-palette"><i class='bx bx-palette'></i>
           <input type="color" value="#1dbbce" id="colorPicker">
    </button>
</div>

Can I do it with only HTML, CSS or I need JavaScript? Thanks.

答案1

得分: 0

如果你希望直到用户点击按钮才显示颜色选择器,我认为[这个答案][1]是你在寻找的。

    div {
      height: 100px;
      position: relative;
      background: lightgray;
      width: 200px;
      text-align: center;
      line-height: 100px;
    }
    div input {
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      width: 100%;
      opacity: 0;
      cursor: pointer;
    }

    <div>点击我
      <input type="color" />
    </div>

  [1]: https://stackoverflow.com/questions/30300146/showing-color-picker-dialog-without-showing-input-option
英文:

If you're wanting to not show the color picker at all until the user clicks on a button, I think this answer is what you're looking for.

div {
  height: 100px;
  position: relative;
  background: lightgray;
  width: 200px;
  text-align: center;
  line-height: 100px;
}
div input {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  cursor: pointer;
}

<div>CLICK ME
  <input type="color" />
</div>

答案2

得分: 0

你可以将一个标签与颜色输入元素关联起来,然后隐藏该输入元素。这样,当您点击标签时,它将打开颜色选择器。

<div class="color-picker">
  <label for="colorPicker"><i class='bx bx-palette'></i></label>
  <input type="color" value="#1dbbce" id="colorPicker">
</div>
英文:

You can associate a label with the color input element and then hide the input. This way when you click the label it will open the color picker.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.color-picker{
  font-size:24px;
}
#colorPicker {
  display: none;
}

<!-- language: lang-html -->

&lt;link href=&#39;https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css&#39; rel=&#39;stylesheet&#39;&gt;

&lt;div class=&quot;color-picker&quot;&gt;
  &lt;label for=&quot;colorPicker&quot;&gt;&lt;i class=&#39;bx bx-palette&#39;&gt;&lt;/i&gt;
    &lt;/label&gt;
  &lt;input type=&quot;color&quot; value=&quot;#1dbbce&quot; id=&quot;colorPicker&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 04:00:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定