简体   繁体   English

把颜色放在div背景之上? (也就是改变div的颜色而不改变'background-color'值)

[英]Put color on top of div background? (aka change color of div without changing 'background-color' value)

As stated in the title; 如标题所述; Is it possible to put a color "on top of" the div's background? 是否可以将颜色“置于”div的背景之上? What I need to do is change the displayed color of the div without changing 'background-color' value. 我需要做的是改变div的显示颜色而不改变'background-color'值。

The reason for keeping background-color is because I'm using it to compare the clicked divs: 保持背景颜色的原因是因为我用它来比较点击的div:

var pick1 = false;
var pick2 = false;
var id1;
var id2;

$('.card').click(function(){

    if(pick1) {
        pick2 = $(this).css('background-color');
        id2 = $(this).attr('id');
        if(pick1 == pick2 && id1!=id2) alert('Correct');
        else alert('Incorrect');

        pick1 = false;
        pick2 = false;
    } else {
        pick1 = $(this).css('background-color');
        id1 = $(this).attr('id');   
    }
});

The goal is to conceal the divs with for example grey color until they are clicked: http://jsfiddle.net/94jerdaw/WJQkA/4/ 目标是隐藏div,例如灰色,直到它们被点击: http//jsfiddle.net/94jerdaw/WJQkA/4/

EDIT: 编辑:

How do I remove the grey when clicking a div? 单击div时如何删除灰色? Check: http://jsfiddle.net/94jerdaw/29TCZ/3/ 检查: http//jsfiddle.net/94jerdaw/29TCZ/3/

firstly, you cannot change div's background-color , without changing its CSS background-color property. 首先,你不能改变div的background-color ,而不改变它的CSS background-color属性。 How could you? 你怎么能?

Guessing what you want is to maintain last background-color (for some action or to swap it back), then save it in some hidden input variables. 猜测你想要的是保持最后的背景颜色(对于某些动作或将其交换回来),然后将其保存在一些隐藏的输入变量中。

And then, you can use it to display anywhere you want. 然后,您可以使用它来显示您想要的任何地方。

Also, if you want to grab the background-color property and show it as a text on the div, you can do that very easily. 此外,如果您想获取background-color属性并将其显示as a text div上的as a text ,则可以非常轻松地执行此操作。

var color = $('#selector').css('backgroundColor');

, but it will return you RGB value. ,但它会返回RGB值。 if you want hex, 如果你想要十六进制

use this handy methods: 使用这个方便的方法:

var hexDigits = new Array
        ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"); 

//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function hex(x) {
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
 }

taken from here 取自这里

Update, 更新,

currently your divs are like this: 目前你的divs是这样的:

<div id="a1" class="card"></div>
<div id="a2" class="card"></div>
<div id="a3" class="card"></div>
..
..

and since you want to assign a secret color to each of these divs update them through javascript, inside your while loop, to make them like this: 并且因为你想为这些div中的每个div分配一个秘密颜色,所以在你的while循环中通过javascript更新它们,使它们像这样:

 <div id="a1" class="card" data-color-index="1"></div>
 <div id="a2" class="card" data-color-index="2"></div>
 <div id="a3" class="card" data-color-index="3"></div>

now, you when you click on a particular div, grab its index and choose that item from the colors array of yours. 现在,当你点击一个特定的div时,抓住它的索引并从你的colors数组中选择那个项目。 since, you are splicing your original array, i had to make a copy of it, to use it later. 因为,你正在splicing原始阵列,我不得不复制它,以后再使用它。

you can grab any element's attribute data-color-index through jquery like this: 你可以通过jquery抓取任何元素的属性data-color-index ,如下所示:

   $('#selector').data('color-index');

see this fiddle . 看到这个小提琴 it does what you want to do 它做你想做的事

Not sure if I understand what you are going for... but would using :after work? 不确定我是否理解你的目的......但是会在下班后使用:

.card {
    position: relative;
}
.card:after {
    content: "";
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background: black;
}
.card:hover:after {
    display: none;
}

Example: http://jsfiddle.net/29TCZ/ 示例: http//jsfiddle.net/29TCZ/

You could replace :hover with a class and toggle it as required. 您可以替换:使用类悬停并根据需要切换它。

As Manish said, you cannot change the appearance of the div without changing the CSS property - but there are several hacky ways of doing the same thing. 正如Manish所说,你不能在不改变CSS属性的情况下改变div的外观 - 但是有几种hacky方法可以做同样的事情。 One example is to create a child div in each of the coloured squares that covers the entire area and then set the colour of that div to grey. 一个示例是在每个覆盖整个区域的彩色方块中创建子div,然后将该div的颜色设置为灰色。 You can then use the CSS display property to show and hide the overlay divs. 然后,您可以使用CSS显示属性来显示和隐藏叠加div。

However, I would recommend having a copy of the colours in JavaScript and just referring to the value associated with each square when it is clicked instead of checking the DOM every time as this keeps the logic separate from the appearance :) 但是,我建议在JavaScript中使用颜色的副本,并在单击它时仅引用与每个方块关联的值,而不是每次都检查DOM,因为这会使逻辑与外观分开:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM