English 中文(简体)
如何增加 CSS 中 HTML 输入类型颜色字段中值的透明度?
原标题:How to add transparency to a value from a HTML input type color field in CSS?
在我的 CSS 中,我需要一个基于用户所选颜色的颜色。 所选的颜色使用固定的透明度为 80%。 以下的窗体元素将使用户很容易地选择颜色 。 < input type=color value\\\ 00ff> / / # 00ffffff > / 但我如何在 CSS 中增加透明度? rbba (# 00ffff, 8) / 不工作更新 : 我具体询问如何在 CSS 中做到这一点 。 而不是在 JS 中 。 BTW 它 s 用于 PHP 驱动的 CMS 。 我不想强迫用户使用 JUS, 我更愿意在 PHP 中写入转换函数 。 可能我必须等待 CSS4( sSS/ LESS like) 颜色函数, 或输入的颜色类型元素使用 hsla/ rgba 模式属性功能增强 。
问题回答
Building upon @Abdelaziz Mokhnache s answer You can also create your own simple color input using pure javascript without having to load externally, like this: let color_picker=document.querySelector("#color_picker"); let color_wrapper=document.querySelector("#color_wrapper"); let color_picker_alpha=document.querySelector("#color_picker_alpha"); function set_color(el){ color_wrapper.style.backgroundColor=color_picker.value + (color_picker_alpha.value == 255 ? "" : parseInt(color_picker_alpha.value).toString(16).padStart(2, "0")); } #color_wrapper { background-color: black; display: inline-block; visibility: hidden; } #color_wrapper::before { content: ""; position: absolute; border-radius: 3px; outline: black solid 2px; border: white solid 3px; height: 2rem; width: 2rem; pointer-events: none; background-color: inherit; visibility: visible; box-sizing: border-box; } #color_picker { opacity: 0; height: 2rem; width: 2rem; box-sizing: border-box; pointer-events: initial; visibility: visible; } #color_picker_alpha { filter: grayscale(100%); visibility: visible; }
you can do it like this using jquery, you can apply a custom color with a custom transparency to an element. $(document).ready(function() { $("input").change(function() { var opacity = $("input[type=range]").val(); var color = $("input[type=color]").val(); var rgbaCol = rgba( + parseInt(color.slice(-6, -4), 16) + , + parseInt(color.slice(-4, -2), 16) + , + parseInt(color.slice(-2), 16) + , + opacity + ) ; $( div ).css( background-color , rgbaCol) }) })
this is a div
In CSS4 there is the 8 digits RGB hexadecimal notation: #RRGGBBAA. Last two digits are the values for the alpha channel. That makes it easy to concatenate the alpha value to the hex string. Not sure about browser support at the moment.
If you want to use this color but only knowing the HEX type, try convert it into RGB or HSL by using color picker tool, there r many online. Then just add alpha chanel ( 0 -1) for transparency. Eg. Rgb(225,10,10, 0.7) Use this: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Colors/Color_picker_tool
Use like this, background-color : rgba(0, 255, 255, 0.8); Refer this, http://www.wufoo.com/html5/types/6-color.html
function rgbA(color) { rgba=color.substring(0,7)+$( #AlphaFill )[0].value.padStart(2, 0 ).replace(100, ); return rgba; } insert an Input type="range". Send your 6 digit Hex to the above function. rgbA( #ff0000 ) returns 8 digit rgba in HEX: range 80 = #ff000080 or range:0 = #ff000000 or range:100 = #ff0000
The last 2 digits change the transparency #ffffff00 => min color opacity #ffffff83 #ffffffff => max color opacity edit : as Lazerbeak12345 said input[type=color] alpha channel has been requested but no progress yet.
I solved it by adding e6 , $( #color ).change(function(e){ $( .mask ).css( background-color , e.target.value+ e6 ) console.log(e.target.value+ e6 ) }) Try using browser console and go to elements, find your target element and pick a color there, the last 2 characters in the color is the alpha... just in my experience
i suggest a other way : might be more convenient in my case i input a color and alpha and save them in a json file to call whenever the page is loaded it reads the json file for saved settings the edit form to change the settings split up the saved color+alpha using substr() then convert to dec with hexdec() before form (php) : $value = JData[ Page]; $galHighlightColor = substr($value[ galHighlightColor ], 0, 7); $galBorderColor = substr($value[ galBorderColor ], 0, 7); $Ha = hexdec(substr($value[ galHighlightColor ], 7, 2)); $Ba = hexdec(substr($value[ galBorderColor ], 7,2)); then in the form use range or number (just do for each)(php): echo " Alpha Apha 0-255 for this color "; handle the form save the complete 8 digit color code do for each get the color and convert the dec to hex again but using dechex() now you got a color +alpha you can use in my example i save it in a json file (php): Json file entry: "galHighlightColor":"#e63333c0","galBorderColor":"#d6a8a8ff" use into html (php):
"; ?> font color, background-color, border-color.


上一篇:
下一篇:


相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签