我有一个div的网格。我希望每个div在每次鼠标经过时从灰色逐渐变为黑色-具体地说,我希望框在第10次鼠标悬停事件时变为黑色。我已经走到一半了。现在,我有一个实现颜色渐变的函数,但当一个框变成黑色后,我鼠标移过的所有其他框也都是黑色的。如何才能使每个框从相同的灰色阴影开始?我敢打赌,我可以通过一系列非常长的if语句来实现这一点,但我想知道是否有更简洁的解决方案。
const bigContainer = document.querySelector('.bigContainer')
window.onload = function initializeGrid() {
for(let i = 0; i < 16; i++) {
bigContainer.innerHTML+='<div class="row">';
for(let j = 0; j < 16; j++) {
bigContainer.innerHTML+='<div class="smallBox">';
}
}
}
let rbgPercentValue = 90;
bigContainer.addEventListener('mouseover', event => {
let target = event.target
let rgbColor = `rgb(${rbgPercentValue}%,${rbgPercentValue}%,${rbgPercentValue}%)`
if (target !== bigContainer) {
target.style['background'] = rgbColor;
rbgPercentValue -= 10;
}
}).smallBox {
border: 1px solid black;
width: 40px;
height: 40px;
display: inline-block;
}
.row {
margin-bottom: -5px;
}
.bigContainer {
text-align: center;
}<div class="bigContainer"></div>
发布于 2020-03-24 01:15:44
使用标题的演示(百分比也可见)。
const bigContainer = document.querySelector('.bigContainer')
window.onload = function initializeGrid() {
for(let i = 0; i < 16; i++) {
bigContainer.innerHTML+='<div class="row">';
for(let j = 0; j < 16; j++) {
bigContainer.innerHTML+='<div class="smallBox">';
}
}
}
bigContainer.addEventListener('mouseover', event => {
let target = event.target
let rbgPercentValue = parseInt(target.dataset.percent);
if (isNaN(rbgPercentValue)) rbgPercentValue = 100;
if (rbgPercentValue >= 10) {
rbgPercentValue -= 10;
target.dataset.percent = rbgPercentValue;
}
let rgbColor = `rgb(${rbgPercentValue}%,${rbgPercentValue}%,${rbgPercentValue}%)`
if (target !== bigContainer) {
target.style['background'] = rgbColor;
}
}).smallBox {
border: 1px solid black;
width: 40px;
height: 40px;
display: inline-block;
}
.row {
margin-bottom: -5px;
}
.bigContainer {
text-align: center;
}<div class="bigContainer"></div>
发布于 2020-03-24 01:24:09
这应该可以做到:
const bigContainer = document.querySelector('.bigContainer')
window.onload = function initializeGrid() {
for(let i = 0; i < 16; i++) {
bigContainer.innerHTML+='<div class="row">';
for(let j = 0; j < 16; j++) {
bigContainer.innerHTML+='<div class="smallBox" data-rgb-percent-value="90">';
}
}
}
bigContainer.addEventListener('mouseover', event => {
const target = event.target
const rgbPercentValue = target.dataset.rgbPercentValue;
const rgbColor = `rgb(${rgbPercentValue}%,${rgbPercentValue}%,${rgbPercentValue}%)`
if (target !== bigContainer) {
target.dataset.rgbPercentValue = parseInt(target.dataset.rgbPercentValue) - 10;
target.style['background'] = rgbColor;
}
})点击此处查看现场演示:https://repl.it/repls/LivelySugaryStatistics
正如其他人所建议的那样,这将存储每个单独块的颜色百分比和data attribute,而不是存储在单个全局值中。
发布于 2020-03-24 01:58:46
有点混乱,但这看起来应该对你有用。
const bigContainer = document.querySelector('.bigContainer')
var rbgPercentValue = 100;
var rbgMultiplier=.9;
var rbgbcolor=`rgb(${100}%,${100}%,${100}%)`;
var pcts =new Array(16);
for(let i=0;i<16;i++){
pcts[i] = new Array(16);
}
for(let i = 0;i<16;i++){
for(let j = 0;j<16;j++){
pcts[i][j]=100;
}
}
window.onload = function initializeGrid() {
for(let i = 0; i < 16; i++) {
bigContainer.innerHTML+='<div class="row">';
for(let j = 0; j < 16; j++) {
bigContainer.innerHTML+='<div class="smallBox" id=R'+i+'C'+j +'>';
}
}
}
bigContainer.addEventListener('mouseover', event => {
let target = event.target
var id= event.target.id;
var index = id.indexOf("C");
var row =id.substring(1,index);
var col = id.substring(index+1);
var div = document.getElementById(id);
if (row != ''){
pcts[row][col] = pcts[row][col]*rbgMultiplier;
rbgcolor=`rgb(${pcts[row][col]}%,${pcts[row][col]}%,${pcts[row][col]}%)`;
div.style['background'] = rbgcolor;
}
}).smallBox {
border: 1px solid black;
width: 40px;
height: 40px;
display: inline-block;
}
.row {
margin-bottom: -5px;
}
.bigContainer {
text-align: center;
}<body>
<div class="bigContainer"></div>
</body>
https://stackoverflow.com/questions/60817867
复制相似问题