简体   繁体   English

CSS3转换具有随机的z-index / depth

[英]CSS3 Transform has random z-index/depth

Perhaps I have found a Bug in CSS3, or it's a simple mistake... 也许我在CSS3中发现了一个Bug,或者这是一个简单的错误...

Here is the page I'm working on: http://robocup2014.herokuapp.com/views/view/52e9bbb2e22d2402003d1ef1 这是我正在处理的页面: http : //robocup2014.herokuapp.com/views/view/52e9bbb2e22d2402003d1ef1

I might not be able to show the error, because its behavior is completely random... But here is what happens: 我可能无法显示该错误,因为它的行为是完全随机的...但是,这是发生的情况:

The flag should be ALWAYS behind that purple layout. 该标志应始终位于该紫色布局的后面。 However, it is sometimes on top, sometimes below. 但是,它有时在顶部,有时在下面。 Sometimes it is on top and goes below (without any code running on javascript), or perhaps it goes on top, out of the blue. 有时,它位于顶部而位于其下方(没有在javascript上运行的任何代码),或者它位于顶部而出乎意料。 Sometimes when it animates, it is below, and then goes on top on the end of the animation, but later, it goes below. 有时,在进行动画处理时,它位于下方,然后在动画的结尾处位于顶部,但是后来,它位于下方。

What I mean is: It's RANDOM, and it's a boring bug, without any pattern. 我的意思是:它是随机的,它是一个无聊的错误,没有任何模式。 (I have counted time, counted animations iterations...). (我已经计算了时间,计算了动画的迭代次数...)。

So, does anyone know how to prevent this? 那么,有谁知道如何防止这种情况发生? Here is a image, in case in your window it doesn't bug. 这是一张图片,以防在您的窗口中出现错误。 (It happens on Safari and Chrome) (在Safari和Chrome上发生) 在此处输入图片说明在此处输入图片说明

I had to figure out the same problem recently and the transform: translate3d(0,0,0); 我最近不得不找出相同的问题和transform: translate3d(0,0,0); solution didn't worked for me. 解决方案对我不起作用。

The fact is that z-index just stack elements in a 2D world, so since you use 3D transformations z-index cannot apply. 事实是z-index只是将元素堆叠在2D世界中,因此,由于您使用3D转换,因此z-index无法应用。

So the real question is IMHO : 所以真正的问题是恕我直言:

How to stack elements in a 3D world ? 如何在3D世界中堆叠元素?

Answer : 答:

Use the Z axis ! 使用Z轴!

This is simple as it, but not intuitive because of browser's rendering. 这样做很简单,但是由于浏览器的呈现而不直观。 I made a JSFiddle to try to illustrate it. 我做了一个JSFiddle试图说明它。 I tried to match as much as possible to your situation, which is hard, just by having your code through your page. 我试图通过您的页面中的代码来尽可能地匹配您的情况,这很难。 In this fiddle, you have four (2 on 2 lines) flags like. 在这个小提琴中,您有四个(2行2行)标记。 I made 2 other div (score) representing an element that should be over the others. 我做了另外2个div(分数),表示应位于其他元素之上的元素。 The first line is buggy, while the second isn't. 第一行不是越野车,而第二行不是。

To see the bug, you can simply move your cursor over an element, knowing that cursor is css pointer on the flag, and css col-resize . 要查看该错误,只需将光标移到某个元素上,就知道光标是该标志上的css pointer ,并且是css col-resize You can see that when you hover the green element from left to right, your cursor changes. 您会看到,当您将绿色元素从左向右悬停时,光标会发生变化。

I made some schemas (yes, they are ugly ;)) to illustrate the problem. 我提出了一些模式(是的,它们很丑陋;))来说明问题。 Here are our elements represented as they should be (to my mind) by the browser : 这是我们浏览器所表示的元素(在我看来):

蓝色超过绿色!

I think the problem is now easier to see. 我认为问题现在更容易看到。 On the right, the green element in over the blue, this is what we want. 在右边,绿色就是蓝色,这就是我们想要的。 But, on the left, the rotateY() caused the blue element to "go out of the screen", passing over the green element. 但是,在左侧, rotateY()导致蓝色元素“移出屏幕”,越过绿色元素。 See this view "from above" if this is not really clear : 如果不清楚,请从上方查看此视图​​:

从上面看

We can think that the blue element go out of the screen by half of its size. 我们可以认为蓝色元素超出了屏幕尺寸的一半。 So, the solution I found (I think this is probably NOT the best solution, but this work, and is cross-browser since you use browser's own implementations of transform , such as -webkit-transform . 因此,我找到了解决方案(我认为这可能不是最佳解决方案,但这项工作可行,并且是跨浏览器的,因为您使用浏览器自己的transform实现,例如-webkit-transform

Scroll to the second line of the fiddle, hover the green element, and see that the cursor stays the same BUT we lose totally the cursor pointer over the blue element. 滚动到小提琴的第二行,将鼠标悬停在绿色元素上,然后看到光标保持不变,但是我们完全失去了蓝色元素上的光标pointer I "just" translated the blue element on the Z axis to -10000px, to be sure that it is far away and wont "go out of the screen" when it will rotate. 我“只是”将Z轴上的蓝色元素转换为-10000px,以确保它很远并且旋转时不会“离开屏幕”。 Note the order which is important here, first the translation translate3d(0, 0, -10000px) , then the perspective and finally the rotation giving it : transform: translate3d(0, 0, -10000px) perspective(1200px) rotateY(-45deg); 注意这里重要的顺序,首先是平移translate3d(0, 0, -10000px) ,然后是透视图,最后是旋转: transform: translate3d(0, 0, -10000px) perspective(1200px) rotateY(-45deg); . As you can see on my last schema (from above) : 正如您在我的上一个架构(从上方)看到的:

蓝色元素现在距离很远!

The blue element is now far away, and wont go over the green one. 现在,蓝色元素距离较远,不会越过绿色元素。 The last problem we can see , is that if the blue element is larger than 10000 pixels, it will go over the green element again. 我们可以看到的最后一个问题是,如果蓝色元素大于10000像素,它将再次超过绿色元素。

In conclusion, I am sure this solution is more a workaround than a real solution. 总之,我确信此解决方案比实际解决方案更能解决问题。 But it was usefull in my case, and I hope I wont fall away from your initial problem and this will help you. 但这对我来说很有用,希望我不会摆脱您最初的问题,对您有帮助。

Can you share the browser versions with us? 您可以与我们共享浏览器版本吗? I'm not seeing it on the most recent versions of Chrome or Safari or Firefox. 我没有在最新版本的Chrome或Safari或Firefox上看到它。

It might be related to this bug: https://bugs.webkit.org/show_bug.cgi?id=61824 可能与此错误有关: https : //bugs.webkit.org/show_bug.cgi?id=61824

You could try adding this to the flat items: 您可以尝试将其添加到平面项目中:

transform: translate3d(0,0,0);

That way they will all be treated at 3D elements and fewer chances of seeing a bug. 这样一来,所有对象都将以3D元素进行处理,并且发现错误的机会更少。

Also, you should be using all the browser prefixes. 另外,您应该使用所有浏览器前缀。 I'm not seeing the transform on Firefox at all, so I can't tell if it is an issue on that browser, as well. 我完全看不到Firefox上的转换,因此也无法确定在该浏览器上是否也存在问题。

I can't reproduce the bug in Chrome, Safari or Firefox. 我无法在Chrome,Safari或Firefox中重现该错误。 I have found a similar question in stackoverflow that may apply to your case ( Complex Webkit Bug? Relative Position + Z-Index + Overflow Hidden + CSS3 Transform ) 我在stackoverflow中发现了类似的问题,可能适用于您的情况( Complex Webkit Bug?相对位置+ Z-Index +隐藏的溢出+ CSS3转换

I would suggest though to use some Javascript before firing the sliding event in order to calculate z-index and apply a higher value on the "purple blocks" or even discard CSS transitions at all and use Javascript transitions only. 我建议尽管在触发滑动事件之前使用一些Javascript,以便计算z-index并在“紫色块”上应用更高的值,甚至完全放弃CSS转换,而仅使用Javascript转换。

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

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