gpt4 book ai didi

javascript - 单击 Canvas 无效

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:46 25 4
gpt4 key购买 nike

我正在创建一个 donut (以粉红色显示)以使用 Canvas 表示数据。问题是 Canvas 留下的剩余区域(图中用绿色填充)在 Windows 的 IESafari 中不可点击。我正在使用 pointer-events 点击 Canvas ,IESafari 不支持。替代解决方案是:我更改了 z-index,由于应用程序的 UI 复杂,这很难实现。

enter image description here

除了更改 z 顺序之外还有什么希望吗?提前致谢!

最佳答案

您可以通过使用与其关联的每个元素的偏移量和尺寸来解决 IE 和 Safari 中的问题。下面是一种基于this demo的方法.希望评论能很好地解释代码

纯Javascript

// Get all overlaying canvases
var canvas = document.getElementsByTagName("canvas"),
// Get all elements that you want the click to fire on
background = document.getElementsByClassName("background");

// Use click location and dimensions/positioning to fake a click through
function passThrough(e) {
// Check all background elements
for(var i = 0; i < background.length; i++) {
// check if clicked point (taken from event) is inside element
var mouseX = e.pageX;
var mouseY = e.pageY;
var obj = background[i];
var width = obj.clientWidth;
var height = obj.clientHeight;

if (mouseX > obj.offsetLeft && mouseX < obj.offsetLeft + width
&& mouseY > obj.offsetTop && mouseY < obj.offsetTop + height) {
background[i].onclick(); // Force click event if within dimensions
}
}
}

for(var i = 0; i < canvas.length; i++) {
// Force our function when clicked
canvas[i].onmousedown = passThrough;
}
for(var i = 0; i < background.length; i++) {
// Toggle background when clicked (to show it works)
background[i].onclick = function() {
if(this.style.background == "black") {
this.style.background = "red";
}
else {
this.style.background = "black";
}
}
}

jsFiddle

使用 jQuery

// Get all overlaying canvases
var canvas = $("canvas"),
// Get all elements that you want the click to fire on
background = $(".background");
// Use click location and dimensions/positioning to fake a click through
function passThrough(e) {
// Check all background elements
for(var i = 0; i < background.length; i++) {
// check if clicked point (taken from event) is inside element
var mouseX = e.pageX;
var mouseY = e.pageY;
var offset = background.eq(i).offset();
var width = background.eq(i).width();
var height = background.eq(i).height();

if (mouseX > offset.left && mouseX < offset.left + width
&& mouseY > offset.top && mouseY < offset.top + height) {
background.eq(i).click(); // Force click event if within dimensions
}
}
}

for(var i = 0; i < canvas.length; i++) {
// Force our function when clicked
canvas.eq(i).off('mousedown').on('mousedown', passThrough);
}
for(var i = 0; i < background.length; i++) {
// Toggle background when clicked (to show it works)
background.eq(i).off('click').on('click', function() {
if($(this).css("background-color") == "rgb(0, 0, 0)") {
$(this).css("background-color", "red");
}
else {
$(this).css("background-color", "black");
}
});
}

jsFiddle

希望它能解决您的问题!

关于javascript - 单击 Canvas 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com