【问题标题】:How to disable mouse event on certain elements?如何在某些元素上禁用鼠标事件?
【发布时间】:2018-12-24 22:47:41
【问题描述】:

我从头开始编写了一个画布 js,它在鼠标事件上创建气泡。现在我的页面几乎没有元素/按钮。即使当我将它们悬停在画布上时,我的画布也会产生气泡,我如何才能停止在特定元素上发送鼠标事件。请注意,我对这些按钮有悬停效果,但不确定它是否重要。非常感谢任何帮助,因为我找不到解决方案。

【问题讨论】:

  • 您能否在这里分享您的代码以了解您到目前为止所做的工作?

标签: javascript html css dom-events


【解决方案1】:

您可能正在使用相同的类或其他东西。你可以使用 event.stopPropagation();

【讨论】:

    【解决方案2】:

    您能否提供一个最小的示例,以便我们确切地知道您在说什么。最好使用工具栏中的<> 按钮(在图像旁边),这样您就可以编辑 HTML、CSS 和 JS,同时还可以在 iframe 中生成结果。

    完成此操作后,我会回来编辑此答案,这是我们需要查看的示例(点击“运行代码片段”以查看 iframe):

    //Preparing canvas
    const canvas = document.querySelector("#myCanvas");
    let c = canvas.getContext("2d");
    let fullWidth = canvas.width;
    let fullHeight = canvas.height;
    
    //utiliy functions
    function isArray(object) {
        return Object.prototype.toString.call(object) == "[object Array]";
    }
    
    function isNumber(object) {
        return typeof object == "number";
    }
    
    function random(min, max) {
        if (isArray(min)) return min[~~(Math.random() * min.length)];
        if (!isNumber(max)) (max = min || 1), (min = 0);
        return min + Math.random() * (max - min);
    }
    
    //Circle class for circle objects
    class Circle {
        constructor(x, y, radius) {
            this.alive = true;
    
            this.radius = radius;
            this.wander = 0.15;
            this.theta = random(2 * Math.PI);
            this.drag = 0.92;
            this.color = "#092140";
    
            this.x = x || 0.0;
            this.y = y || 0.0;
    
            this.vx = 0.0;
            this.vy = 0.0;
        }
    
        draw() {
            c.beginPath();
            c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
            c.fillStyle = this.color;
            c.fill();
        }
    
        move() {
            this.x += this.vx;
            this.y += this.vy;
    
            this.vx *= this.drag;
            this.vy *= this.drag;
    
            this.theta += random(-0.5, 0.5) * this.wander;
            this.vx += Math.sin(this.theta) * 0.1;
            this.vy += Math.cos(this.theta) * 0.1;
    
            this.radius *= 0.95;
            this.alive = this.radius > 0.5;
            this.draw();
        }
    }
    
    window.addEventListener("mousemove", function(event) {
        console.log(event.target.classList[0])
        if (event.target.classList[0] !== "fab"
            && event.target.id !== "social"
            && event.target.classList[0] !== "buttons-ul"
            && event.target.classList[0] !== "resume-li"
            && event.target.classList[0] !== "portf-btn") {
            
            let nx = event.x;
            let ny = event.y;
            console.log(nx + ", " + ny);
    
            init(nx, ny, 40);
            init(nx + random(5, 10), ny+random(5, 10), 40);
            init(nx + random(5, 15), ny+random(5, 15), 35);
        }
    });
    
    const circles = {};
    
    // const COLORS = ["#004B8D", "#0074D9", "#4192D9", "#7ABAF2", "#D40D12", "#FF1D23"];
    
    const COLORS = [
      '#E3F2FD', '#BBDEFB', '#90CAF9', '#64B5F6', '#42A5F5', '#2196F3', '#1E88E5', '#1976D2', '#1565C0', '#0D47A1', // Blue 50->900
      '#FFF8E1', '#FFECB3', '#FFE082', '#FFD54F', '#FFCA28', '#CC3B58', '#C93A29', '#FF3932', '#ED2938', '#C03A4C' // Amber 50->900
    ];
    
    let key = 0;
    
    function updateKey() {
        key++;
        key = key % 3000;
    }
    
    function init(x, y, maxRadius) {
        let circle, theta, force;
    
        circle = new Circle(x, y, random(10, maxRadius));
        circle.wander = random(0.5, 2.0);
        circle.drag = random(0.9, 0.99);
        theta = random(2 * Math.PI);
        force = random(2, 8);
        circle.vx = Math.sin(theta) * force;
        circle.vy = Math.cos(theta) * force;
        circle.color = random(COLORS);
        circles[key] = circle;
        updateKey();
    }
    
    function animate() {
        requestAnimationFrame(animate);
        c.clearRect(0, 0, innerWidth, innerHeight);
        for (let k in circles) {
            if (!circles[k].alive) {
                delete circles[k];
            } else {
                circles[k].move();
            }
        }
    }
    
    animate();
    #root {
      background-color: black;
      padding: 15px;
    }
    
    #myCanvas {
      border: 2px solid white;
      width: 100px;
      height: 100px;
    }
    <div id="root" class="fab">
      <canvas id="myCanvas" class="drawable" width="200" height="200">
      
      </canvas>
    </div>

    【讨论】:

    • 您好,感谢您的回复。当然,这是一个例子。对不起,我是 StackOverflow 的新手。
    • 感谢您的回复。我实际上已经用 JS 解决了它,但如果有任何其他方法,我将不胜感激。请查看我的 github 链接,这是我的 JS 画布动画。现在想象一下我在页面中有其他元素,例如按钮和一些文本。当我在这些元素之上时,我想停止那个动画。 rpaltayev.github.io/js-canvas 现在,在我的解决方案中,mouselistener 调用函数中有一个 if 条件,用于检查我是否在该元素上。有没有其他办法解决?
    • 我不能确切地告诉你如何解决这个问题,特别是因为你已经有很多代码了。肯定有很多方法可以改进您的代码,其中一种方法是在您的画布上寻找一个类,而不是像您目前正在做的那样寻找某些类的缺失。我在演示的根元素中添加了“fab”,但是如果您打算拥有大量不同的类和元素,您肯定只想检查 if 语句,滚动的东西是否有某个类,而不是它是否没有,因为此解决方案无法很好地扩展。
    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 2013-04-23
    • 2021-01-07
    • 2017-01-19
    相关资源
    最近更新 更多