Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set options for spectrum js in case of fallback from color type input?

I am using the Spectrum JS as a fallback for browsers that does not support <input type="color>. To do so, just include the Spectrum JS library and everything is done automatically.

http://bgrins.github.io/spectrum/#modes-input

A working example: http://jsfiddle.net/ctkY3/5542/

You should see the default color input box except when using IE. (Yes, even IE11 doesn't support color input. MS only support color input in Edge)

The problem is that once using the $(element).spectrum() function, it will show the spectrum color picker for all browser, while I just want to use it as a fallback. So, how can I add options to the fallback color picker?

like image 537
cytsunny Avatar asked Feb 07 '17 08:02

cytsunny


People also ask

What is fallback color in CSS?

Fallback color is used to specify the color for a situation when the browser doesn’t support RGBA colors. Some browsers that doesn’t support fallback colors are opera 9 and below, IE 8 and below etc. Specify a solid color before a RGBA color so the solid color can still work if the browser doesn’t support the RGBA colors.

How do I set the initial color of a spectrum input?

Tip: options can be specified in an options object in the spectrum initializer, like $ (element).spectrum ( {showAlpha: true }) or on the element's markup, like <input data-show-alpha="true" /> . The initial color will be set with the color option.

Can spectrum show palettes below the colorpicker?

Spectrum can show a palette below the colorpicker to make it convenient for users to choose from frequently or recently used colors. When the colorpicker is closed, the current color will be added to the palette if it isn't there already. Check it out here: If you'd like, spectrum can show the palettes you specify, and nothing else.

How to get the value of selected option in colorparam?

Try this. When you select an option you will recieve the <select> element in colorParam. If you select the first option, you will get Red in colorParam.value, but you are using IDs in lowerCase, so you can use toLowerCase () function to convert it.


Video Answer


1 Answers

Before calling spectrum on your jQuery object, you should check if the dom is ready and also if the browser supports color input.

Just as this answer shows, when an input is not supported, it will default to text.

So, assuming you have an input with id picker, you can try the following:

$(document).ready(function() {
    var i, colorInputSupported;
    i = document.createElement("input");
    i.setAttribute("type", "color");
    colorInputSupported = i.type !== "text";

    if(!colorInputSupported){
      $("#picker").spectrum({
        // Here you put the options
        color: "#f00"
      });
    }
});

Here is a working example of this code.

As an alternative, spectrum supports setting the options of the picker in the HTML element itself, via data attributes. For instance:

<input type='color' id="picker" data-color="#f00"/>

Will generate a color picker starting in red color, which will be displayed only in browsers that do not support the default color picker.

Here is a working example. For a complete list of options, you can check them in spectrums docs

like image 188
Sam Avatar answered Sep 16 '22 19:09

Sam