我目前正在做Odin项目的蚀刻草图项目,遇到了一个问题,当我在绘图网格上单击并拖动鼠标时,光标有时会变成块图标.我现在有一个变量isPainting,它充当网格上绘图的布尔值,在mousedown上为true,在mouseup上为false

下面的按钮目前不起作用,因为我更专注于让绘图工作.附件是我的html、css和javascript代码.

let isPainting = false;
document.body.onmousedown = () => (isPainting = true);
document.body.onmouseup = () => (isPainting = false );

function populatePage(size){
let board = document.querySelector(".board");
let squares = board.querySelectorAll("div");
squares.forEach((div) => div.remove());
board.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
board.style.gridTemplateRows = `repeat(${size}, 1fr)`;

let amount= size * size;
for(var i = 0; i < amount; i++){
    let square = document.createElement('div')
    square.addEventListener('mouseover',colorSquare);
    square.addEventListener('mousedown',colorSquare);
    square.style.backgroundColor = 'grey';
    board.insertAdjacentElement('beforeend',square);
   }
}


function colorSquare(e){
    if(isPainting){
        this.style.backgroundColor = 'black';
    }
}

populatePage(16);
.board{
    width: 500px;
    height:  500px;
    display: grid;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel = "stylesheet" href="styles.css">
    <link rel = "stylesheet" href="normalize.css">
    <title>Etch-a-Sketch</title>
</head>
<body>
    <div class="flex-container">
<div class="header">
    <h1>Etch a Sketch</h1>
    <h2></h2>
</div>
<div class="content">
    <div class="board"></div>
        <div class="buttons">
        <button>Black</button>
        <button>Erase</button>
        <button>Random</button>
        <button>Reset</button>
    </div>
</div>
<button>Set Size</button>
</div>
</body>
<script src="main.js"></script>
</html>

预期结果:只有按住鼠标按钮并在网格上移动时,用户才能在蚀刻草图网格上绘制.释放鼠标按钮时,绘图将停止.

实际结果:有时当鼠标按下并移动时,光标会变成一个"块"图标,绘图将停止或仍然绘图,尽管鼠标按钮已经向上.

推荐答案

mousedown后跟mousemove可以生成一个拖动事件,在这种情况下,特别是当您try 单击已着色的黑色方块,然后移动鼠标时.

您看到的not-allowed光标是触发此事件以及浏览器试图解释如何处理您正在"拖动"的内容的结果.如果在mousedown事件上单击preventDefault,则不再生成拖动事件,因此不会出现not-allowed光标.

相关代码:

    document.body.onmousedown = (e) => {
      isPainting = true; 
      e.preventDefault();
    };

let isPainting = false;
document.body.onmousedown = (e) => {
  isPainting = true; 
  e.preventDefault();
};
document.body.onmouseup = () => (isPainting = false );

function populatePage(size){
let board = document.querySelector(".board");
let squares = board.querySelectorAll("div");
squares.forEach((div) => div.remove());
board.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
board.style.gridTemplateRows = `repeat(${size}, 1fr)`;

let amount= size * size;
for(var i = 0; i < amount; i++){
    let square = document.createElement('div')
    square.addEventListener('mouseover',colorSquare);
    square.addEventListener('mousedown',colorSquare);
    square.style.backgroundColor = 'grey';
    board.insertAdjacentElement('beforeend',square);
   }
}


function colorSquare(e){
    if(isPainting){
        this.style.backgroundColor = 'black';
    }
}

populatePage(16);
.board{
    width: 500px;
    height:  500px;
    display: grid;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel = "stylesheet" href="styles.css">
    <link rel = "stylesheet" href="normalize.css">
    <title>Etch-a-Sketch</title>
</head>
<body>
    <div class="flex-container">
<div class="header">
    <h1>Etch a Sketch</h1>
    <h2></h2>
</div>
<div class="content">
    <div class="board"></div>
        <div class="buttons">
        <button>Black</button>
        <button>Erase</button>
        <button>Random</button>
        <button>Reset</button>
    </div>
</div>
<button>Set Size</button>
</div>
</body>
<script src="main.js"></script>
</html>

Javascript相关问答推荐

如何比较嵌套对象的更改并创建更改报告

如何在alpinejs中显示dev中x-for的元素

为什么我达到了时间限制!?LeetCode链接列表循环(已解决,但需要解释!)

如何通过onClick为一组按钮分配功能;

确定MutationRecord中removedNodes的索引

如何才能拥有在jQuery终端中执行命令的链接?

google docs boldText直到按行执行应用脚本错误

函数返回与输入对象具有相同键的对象

在我的html表单中的用户输入没有被传送到我的google表单中

JS,当你点击卡片下方的绿色空间,而它是在它的背后转动时,

在执行异步导入之前判断模块是否已导入()

我创建了一个创建对象的函数,我希望从该函数创建的对象具有唯一的键.我怎么能做到这一点?

Next.js服务器端组件请求,如何发送我的cookie token?

VSCode中出现随机行

Reaction组件在本应被设置隐藏时仍显示

Google脚本数组映射函数横向输出

使用Document.Evaluate() Select 一个包含撇号的HTML元素

无法读取未定义的属性(正在读取合并)-react RTK

Cherrio JS返回父div的所有图像SRC

扩散运算符未按预期工作,引发语法错误