惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

一纸忘忧

ONE DAY 0310 重启 One Day 计划 成立一周年!开源的本土化中文文档知识库 - 一纸忘忧 也许世界并不美好,但也没有那么糟糕 - 一纸忘忧 如何在团队项目中白嫖 Vercel 的免费服务 - 一纸忘忧 梅开二度,土区 iCloud 再次涨价 - 一纸忘忧 解决 macOS 导入宝塔面板 SSL 证书失败 阿里云高校计划免费领取五年 ECS 服务器 - 一纸忘忧 HTML Video 在 Vue 中消失的 muted 属性 WordPress 设置文章 post_name 与 ID 同步
konva.js 阻止 click 事件冒泡到父元素 - 一纸忘忧
博主: 一纸忘忧 · 2023-03-29 · via 一纸忘忧

KonvaJS 中,事件冒泡是默认启用的,这意味着当在子元素上触发事件时,该事件将向上传播到父元素。因此,如果在图层上注册了 click 事件并在其上创建了一个组,那么单击该组也将触发该事件。

KonvaJS 并没有 stopPropagation() 方法来防止事件冒泡。相反,KonvaJS 使用了自己的事件系统来管理事件、事件传递和事件监听器。

KonvaJS 中,可以使用事件对象的 stopPropagation() 方法来阻止事件传播到上级元素。在 KonvaJS 中,事件对象是一个 Konva.Collection 类的实例,它是事件发生时所有相关节点的集合。可以使用它来访问事件目标节点和其他相关节点。

例子

以下代码演示了如何使用 KonvaJS 事件系统来防止父级元素的 click 事件触发:

const stage = new Konva.Stage({
  container: 'container',
  width: 500,
  height: 500,
});

const layer = new Konva.Layer();
stage.add(layer);

const group = new Konva.Group();
group.on('click', (event) => {
  // 阻止事件传播到父级元素
  event.cancelBubble = true;
  
  // 处理组的 click 事件
  // ...
});

layer.on('click', (event) => {
  // 在此处理父级元素的 click 事件
  // ...
});

在这个例子中,当单击组时,事件对象的 cancelBubble 属性被设置为 true,以防止事件继续传播到父级元素。这样,父级元素的 click 事件就不会触发了。

需要注意的是,由于 KonvaJS 的事件系统与浏览器的事件系统不同,因此它的用法也略有不同。具体来说,事件对象没有 stopPropagation() 方法,而是使用 cancelBubble 属性来实现相同的效果。

赞赏作者

如果觉得我的文章对你有用,请随意赞赏