How to position a color picker opened through HTML5's color input?

前端 未结 2 1712
春和景丽
春和景丽 2020-12-20 12:14

Today I read about HTML5\'s color input and I thought I\'d give it a try:

相关标签:
2条回答
  • 2020-12-20 12:20

    The positioning of the input of type color is browser-specific implementation, in the official documentation there is no given rule for user-agents (i.e. browsers) how to position it over the page's element. This makes custom positioning via CSS for example, or JavaScript not possible.

    However, there are some other rules (for example, there is always a color picked, and there is no way to set the value to the empty string.)

    Keep in mind when using the input of type color, that Internet Explorer and Safari browsers do not support it yet.

    0 讨论(0)
  • 2020-12-20 12:41

    I had this same question. I wanted to create a Theme editor and wanted to do this. Like the VS Code when editing a CSS file. I figured out some strategies for solving this problem:

    Method A using positioned iframe and signalling changes between iframe and parent.

    1. Figure out the absolute screen location where you want your picker to open.
    2. Create a <input type=hidden with an id like signalColor. And monitor this id for changes.
    3. Move a hidden <div with the absolute position and size where you need the color picker. Place an iframe in the <div with the code to create a colour picker. Also, in the Iframe you will need an input with your initial color.
    4. Set the color of the initial color within the iframe and then show the div.
    5. Use the following post to figure out when to signal the new color or if cancelled. https://lugolabs.com/articles/how-to-use-a-color-picker-in-javascript

    Method B using window.open(…)+ Ajax

    1. Figure out the absolute screen location where you want your picker to open.
    2. Generate a random token file name.
    3. Open a new Window with needed position and size, loading in any into the HTML you will need. Embed in the script the token file name and pass ajax credentials you will be using. Add references to JQuery, etc. Create a loop in the parent DOM to detect when the window is closed.
    4. When the operator clicks on a new selection detect the click and capture the new colour value.
    5. Send an ajax message with the new colour to the host. Saving the value in the token file.
    6. Then close the window which then triggers the parent to use ajax to request the token file.
    0 讨论(0)
提交回复
热议问题