在下面的测试代码中,我想根据 canvas.offsetLeft 更新 mbox.style.left,但是由于布局更改而重新定位画布时,不会调用函数 repositionBox()。关于如何检测 canvas.offsetLeft 变化的任何提示?(同样的问题也适用于 canvas.offsetTop。)
DivElement createMessagebox(String id, CanvasElement c) {
DivElement mbox = new DivElement();
mbox.id = id;
int left = 10 + canvas.offsetLeft;
int top = 28 + canvas.offsetTop;
mbox.style.border = '2px solid #FFF';
mbox.style.zIndex = "1";
mbox.style.position = "absolute";
mbox.style.width = "300px";
mbox.style.color = "lightgreen";
mbox.style.background = "rgba(50,50,50,0.7)";
mbox.style.textAlign = "left";
mbox.style.padding = "2px";
mbox.style.fontSize = 'x-small';
void repositionBox(Event e) {
int left = 10 + canvas.offsetLeft;
int top = 28 + canvas.offsetTop;
mbox.style.left = "${left}px";
mbox.style.top = "${top}px";
print("repositionBox: event=$e: left=${mbox.style.left} top=${mbox.style.top}");
}
repositionBox(null);
c.onChange.listen(repositionBox);
return mbox;
}