- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个 Etch-a-Sketch for The Odin Project使用 HTML、CSS 和 JavaScript。我在使用“着色笔”的 JavaScript 时遇到了一些困难。
注意:这个问题中描述的“笔”不要与代码笔混淆。
有两支笔:黑色笔将单元格变成黑色,着色笔在第一次通过时将单元格变成黑色,但将不透明度更改为 0.1,使其几乎透明。这会产生轻微变暗的错觉,因为单元格后面的背景与单元格的原始颜色相同。在随后每次使用着色笔通过后,不透明度都会增加 0.1。
着色器笔是默认笔。起初它工作正常,但是当我单击黑色笔然后返回着色笔时,着色笔会增加一次不透明度并停止。它还会覆盖黑色单元格。
这是我设置笔的方法:
makeGrid(16); // generate grid
pen("shader"); // default pen: shader
document.querySelector('#black').addEventListener("click", e => pen("black"));
document.querySelector('#shader').addEventListener("click", e => pen("shader"));
function pen(selected) {
let cells = document.querySelectorAll('div.cell');
cells.forEach(cell => {
cell.addEventListener('mouseover', function(e) {
if (selected == "black") {
// turns cell black
cell.classList.remove('shade');
cell.style.backgroundColor = '#101010';
cell.style.opacity = '1';
console.log('Make cell black')
} else if (selected == "shader") {
let opacity = cell.style.opacity;
if (cell.classList.contains("shade")) {
// increases opacity by 0.1
cell.style.opacity = (Number(opacity) + 0.1);
console.log('If there is shade, increase opacity.')
} else {
// turns cell to 0.1 opacity
cell.classList.add('shade');
cell.setAttribute('style', 'opacity:0.1');
cell.style.backgroundColor = '#101010';
console.log('Else, add shade class.')
}
}
})
});
}
我在每个条件 block 中使用 console.log
语句运行此代码,当我返回使用着色器笔时,看起来黑笔仍在运行。我最好的猜测是这就是导致问题的原因,但我不确定如何解决它。
我创建了一个 CodePen与其余代码一起使用,以便您可以看到它的实际效果。如前所述,默认笔是着色器。一开始它按预期工作,但在选择黑色笔后它就停止了。
如有任何帮助或指导,我们将不胜感激。
最佳答案
你的代码有很多问题,主要的一个是当你改变笔类型时(当你调用 pen()
函数时)你添加了一个新的 mouseover
事件监听器。
让我们来看看
您的代码首先调用 pen('shader')
使 shader
成为默认笔类型
pen("shader");
让我们看看pen()
定义
function pen(selected) {
let cells = document.querySelectorAll('div.cell');
cells.forEach(cell => {
cell.addEventListener('mouseover', function(e){
...
})
});
}
事件监听器获取字符串 shader
并在其上关闭 (这称为 closure ) 现在每当您将鼠标悬停在单元格上时笔类型将是 shader
现在,当您选择黑笔时,您会再次调用函数 pen()
,添加一组新的事件监听器。
这不会用新的替换旧的事件监听器,它会将它们堆叠在一起,就像一个接一个地调用两个函数一样。
现在,当您将鼠标悬停在一个单元格上时,第一个带有笔类型 shader
的事件监听器将触发,然后第二个带有黑色笔的事件监听器将在它之后触发,您可以在控制台中看到这一点看到从两个 if 语句条件打印的两条消息。
现在,当您再次选择 shader
时,您添加了一组新的带有着色笔类型的事件监听器,第一个事件监听器将触发设置不透明度为 0.1,然后是黑色事件监听器删除 shade 类然后第三个监听器再次检查单元格是否具有类 shade if(cell.classList.contains("shade"))
,如果为假,则下降到最后一个 else 语句。
有很多方法可以解决这个问题,但鉴于您是初学者,我会给您一些简单的解决方案
首先让笔类型成为一个全局变量,然后在每个按钮上单击您更改它
其次在代码的开头向所有 cel 添加一个事件监听器,并让它们检查全局变量
演示
let penType = 'shader';
makeGrid(4); // generate grid
document.querySelector('#black').addEventListener("click", e => penType = "black");
document.querySelector('#shader').addEventListener("click", e => penType = "shader");
// generate grid
function makeGrid(dimension) {
const canvas = document.querySelector('#canvas');
const canvasWidth = document.getElementById("canvas").offsetWidth;
const cellWidth = canvasWidth / dimension;
for (let x = 1; x <= dimension * dimension; x++) {
const makeCell = document.createElement('div');
makeCell.classList.add('cell');
canvas.appendChild(makeCell);
};
canvas.style.gridTemplateRows = `repeat(${dimension}, ${cellWidth}px [row-start]`;
canvas.style.gridTemplateColumns = `repeat(${dimension}, ${cellWidth}px [column-start]`;
// after all cells have been generated add the event listener
// this is not the most optimize version of this but it gets the job done
let cells = document.querySelectorAll('div.cell');
cells.forEach(cell => {
cell.addEventListener('mouseover', function(e) {
console.log(penType)
if (penType == "black") { // turns cell black
cell.classList.remove('shade');
cell.style.backgroundColor = '#101010';
cell.style.opacity = '1';
console.log('Make cell black')
} else if (penType == "shader") { // turns cell 0.1
let opacity = cell.style.opacity;
if (cell.classList.contains("shade")) {
cell.style.opacity = (Number(opacity) + 0.1);
console.log('If there is shade, increase opacity.')
} else {
cell.classList.add('shade');
cell.setAttribute('style', 'opacity:0.1');
cell.style.backgroundColor = '#101010';
console.log('Else, add shade class.')
}
}
})
});
}
body {
margin: 0;
background-color: #514c53;
color: #fff;
font-size: 18pt;
}
/* DIV STYLING */
#container {
margin: 20px auto;
max-width: 400px;
display: flex;
flex-wrap: wrap;
}
.full {
width: 100%;
}
.left {
width: 75%;
margin-top: 15px;
}
.right {
width: 25%;
text-align: right;
}
#canvas {
display: grid;
flex-wrap: wrap;
grid-template-rows: repeat(16, 30px [row-start]);
grid-template-columns: repeat(16, 30px [col-start]);
background: #e8dfd6;
}
.cell {
background: #e8dfd6;
}
/* CELL SHADING */
.black {
background-color: #101010;
}
.shade {
background-color: #101010;
width: 100%;
height: 100%;
}
/* ELEMENT STYLING */
button {
font-weight: 700;
margin: 10px 0;
background-color: #b9967d;
color: #fff;
padding: 0px 15px;
text-shadow: 1px 1px #2e2e2e;
box-shadow: 3px 3px #2e2e2e;
border-radius: 5px;
border: 0px;
text-transform: uppercase;
height: 30px;
outline: none;
transition: 0.3s;
}
button:hover {
cursor: pointer;
background-color: #aa8062;
box-shadow: 0px -3px #2e2e2e;
}
button:active {
background-color: #997963;
}
.circle {
float: left;
height: 15px;
width: 15px;
border: 1px solid #fff;
border-radius: 50%;
margin: 1px 5px 0 0;
}
.circle:hover {
cursor: pointer;
/*transition: 0.3s;
border: 2px solid #fff;*/
}
.meaning {
float: left;
font-size: .542em;
text-transform: uppercase;
padding-top: 2px;
margin-right: 10px;
}
#black {
background-color: #101010;
}
#shader {
background-image: linear-gradient(#807b76, #dfd8d0);
}
<div id="container">
<div class="left" style="margin-bottom:10px">
<div class="circle" id="black"></div>
<div class="meaning">Black</div>
<div class="circle" id="shader"></div>
<div class="meaning">Shader</div>
</div>
<div id="canvas" class="full">
<!--grid generates here-->
</div>
</div>
<!--container-->
您的逻辑有一个小问题,当您使用黑色笔时,您不必删除阴影类,因为当您返回阴影笔并将鼠标悬停在黑色单元格上时,它会将不透明度设置回 0.1
关于单击另一支笔后,JavaScript Etch-a-Sketch 着色笔停止增加不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58511950/
我需要从 1024 增加 FD_SETSIZE 值至 4096 .我知道最好使用 poll()/epoll()但我想了解什么是优点/缺点。主要问题是:我要重新编译glibc吗? ?我读了几个线程,其中
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我在 HTML 文件中有这样的内容: var value = 0; add(x){ x++; do
有没有办法在用户向上滚动时增加变量,并在用户使用 JavaScript 向下滚动时减少变量?变量没有最大值或最小值,如果能够调整灵敏度就好了。我不知道从哪里开始,感谢您的帮助! 编辑:没有滚动条,因为
我是 ios 新手,遇到以下问题。 我想根据表格 View 中元素的数量增加和减少表格 View 的高度大小。如果在输入时客户端在输出时给出 3 个或超过 3 个元素,我希望看到一个比默认行大 2 行
所以我一直在四处搜索,似乎大多数人认为以下列方式递增 indexPath 是正确的方法: NSIndexPath *newIndexPath = [NSIndexPath indexPathForRo
我有一个关于 connSupervisionTimeout 的问题。 我正在使用 CoreBluetooth 编写应用程序。我检查了连接参数和 connSupervisionTimeout = 720
我正在尝试根据页面的滚动位置更改元素的填充;当用户向下滚动页面时,填充会增加,而当他们向上滚动时,填充会减少。 我的主要问题是滚动不是很流畅,有时如果我滚动到页面顶部太快,每次元素的填充大小都不一样。
我正在尝试计算 18456 个基因的相关性度量,但编译器 (Dev C) 在将宏 GENE 或 INDEX 增加到 4000 到 5000 之间的值后退出或大。例如,它适用于: # define GE
我有一个带有 position: absolute 和 CSS3 过渡的圆形元素(a 元素)。在 hover 事件中,我想增加圆的高度和宽度,但我想在所有边上添加像素,而不仅仅是在左侧或右侧。 示例如
为了改善用户体验,我计划在我网站的所有页面(A-、A、A+)上增加/减少/重置字体大小 我面临的问题是页面上不同元素使用的字体大小不统一。有些是 14px,有些是 18px,有些是 12px,有些是
本文实例讲述了Yii框架数据库查询、增加、删除操作。分享给大家供大家参考,具体如下: Yii 数据库查询 模型代码: ?
sql替换语句,用该命令可以整批替换某字段的内容,也可以批量在原字段内容上加上或去掉字符。 命令总解:update 表的名称 set 此表要替换的字段名=REPLACE(此表要替换的字段名, '原
sql不常用函数总结以及事务,增加,删除触发器 distinct 删除重复行 declare @x 申明一个变量 convert(varchar(20),t
要增加我使用的最大可用内存: export SPARK_MEM=1 g 或者我可以使用 val conf = new SparkConf() .setMaster("loca
我正在尝试将文本(自定义文本按钮)放入 AppBar 的前导属性中。但是,当文本太长时,文本会变成多行 Scaffold( appBar: AppBar( centerTi
我正在使用最新版本的 NetBeans,我需要增加输出和菜单的字体大小(不是代码部分)。我试过: netbeans_default_options=".... --fontsize 16" 但是当我将
我必须将 180000 个点绘制到一个 EPS 文件中。 使用标准 gnuplot 输出尺寸点彼此太接近,这使得它们无法区分。有没有办法增加图像的宽度和高度? 最佳答案 是的。 set termina
我有一个带有输入字段的 twitter bootstrap 3 导航栏。我想增加输入字段的宽度。我已尝试设置 col 大小,但它不起作用。 html比较长,请引用bootply http://www.
我正在尝试增加 ggplot 标题中下划线的大小/宽度/厚度。我曾尝试使用大小、宽度和长度,但没有成功。 这是我所做的一个例子。 test <- tibble(x = 1:5, y = 1, z =
我是一名优秀的程序员,十分优秀!