简体   繁体   English

当画布未定位为页面上的顶部元素时,菜单位置出现偏离

[英]Menu position appearing way off when the canvas it not positionned as top element on the page

Using Draw2d and the menu selection code from the demo but I don't get the expected result...使用Draw2d演示中的菜单选择代码但我没有得到预期的结果......

In the demo, the menu appears on the right side of the clicked element.在演示中,菜单出现在单击元素的右侧。 In my version the menu appears way off to the top.在我的版本中,菜单出现在顶部。

This seems to be caused by the fact that I have some HTML directly above the canvas (header, etc...).这似乎是由于我在画布(标题等)正上方有一些 HTML 的事实造成的。 On the contrary if the canvas is at the very top of the page it works well.相反,如果画布位于页面的最顶部,则效果很好。

Found a way to fix this.找到了解决这个问题的方法。

It's all about where you append the menu's HTML and relative positionning这都是关于你在何处附加菜单的 HTML 和相对定位

In the demo there isn't any HTML above the canvas (on the website it's an iframe so what you see above is not really there from the canvas perspective) so it works.在演示中,画布上方没有任何 HTML(在网站上它是一个 iframe,所以从画布的角度来看,您在上面看到的并不是真正存在的),因此它可以工作。

The demo is misleading because in the code they add the HTML menu in the body tag.该演示具有误导性,因为在代码中,他们在body标记中添加了 HTML 菜单。 They can do that simply because their page is composed of only two elements : the body and the canvas .他们可以这样做仅仅是因为他们的页面仅由两个元素组成:主体画布

In my case and probably yours too, doing this results in adding the HTML menu far far far far away from the canvas itself resulting in a position that is wayoff !在我的情况下,也可能是你的情况,这样做会导致添加远离画布本身的 HTML 菜单,从而导致位置偏离!

What they do is :他们所做的是:

$("body").append(this.overlay);

What you should do is append the HTML menu ( this.overlay ) as a sibling of the canvas.您应该做的是将 HTML 菜单( this.overlay )附加为画布的兄弟。 Do NOT add it in the canvas itself.不要将它添加到画布本身中。 If you do, you won't catch click events anymore.如果这样做,您将不再捕获点击事件。

Your HTML should look like this :您的 HTML 应如下所示:

<div id="some-parent">
  <div id="gfx_holder">THE CANVAS</div>
</div>

And the code updated to并且代码更新为

$("#some-parent").append(this.overlay);

But it's not finished yet.但它还没有完成。 As the menu is added using position: absolute you'll need your parent containers set to position: relative so the the child's absolute position would become relative to the parent and not web page.当使用position: absolute添加菜单时,您需要将父容器设置为position: relative这样子容器的绝对位置将成为相对于父级而不是网页的相对位置。 It's CSS... You know...这是 CSS... 你知道...
Also, the parent should be the exact same size as the child canvas !此外,父画布的大小应该与子画布完全相同!

So the HTML should evolve to this :所以 HTML 应该演变成这样:

<div id="some-parent" style="position: relative; height: 800px">
  <div id="gfx_holder" style="height: 800px">THE CANVAS</div>
</div>

And when the menu's HTML is added it should look like that at runtime :添加菜单的 HTML 后,它在运行时应如下所示:

<div id="some-parent" style="position: relative; height: 800px">
  <div id="gfx_holder" style="height: 800px">THE CANVAS</div>
  <div class="overlayMenu" style="top: 230px; left: 197.391px;">⊕</div>
</div>

See ?看 ? The overlayMenu has position: absolute which allows it to be rendered at a correct position... overlayMenu具有position: absolute允许它在正确的位置呈现......

hf高频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM