简体   繁体   English

悬停在重叠的CSS3形状上

[英]Hovering on overlapping CSS3 shapes

Alright I have a potentially tricky question regarding interacting with shapes created via CSS3. 好吧,我有一个关于与通过CSS3创建的形状进行交互的潜在棘手问题。 See the following fiddle: http://jsfiddle.net/MH4LN/1/ 请参阅以下小提琴: http//jsfiddle.net/MH4LN/1/

Code example: 代码示例:

<div class="container">
    <div class="l1"></div>
    <div class="l2"></div>
    <div class="l3"></div>
</div>

with the following CSS: 使用以下CSS:

.container {
    width: 570px; height: 570px;
    position: relative;
}
.l1 {
    width: 352px;
    height: 352px;
    background: red;

    position: absolute;
    top: 109px;
    left: 109px;
    z-index: 999;

    -webkit-border-radius: 176px;
    border-radius: 176px;
}
.l2 {
    width: 464px;
    height: 464px;
    background: blue;

    position: absolute;
    top: 53px;
    left: 53px;
    z-index: 998;

    -webkit-border-radius: 232px;
    border-radius: 232px;
}
.l3 {
    width: 570px;
    height: 570px;
    background: green;

    position: absolute;
    z-index: 997;

    -webkit-border-radius: 285px;
    border-radius: 285px;
}

.l1:hover, .l2:hover, .l3:hover {
    background: #fff;   
}

My problem is this: I want to apply CSS to each shape on hover. 我的问题是:我想在悬停时将CSS应用于每个形状。 The shapes are created by setting a border radius equal to half the div width/height (creating a circle). 通过将边框半径设置为div宽度/高度的一半(创建圆)来创建形状。 However when you hover over the empty area hidden by the radius, you still trigger a hover state on that element. 但是,当您将鼠标悬停在由半径隐藏的空白区域上时,仍会在该元素上触发悬停状态。 Here's an image to illustrate what I mean: 这是一张图片来说明我的意思:

悲伤的悬停让我难过。

Is there any way to avoid this? 有什么方法可以避免这种情况吗? Will I need to use the canvas element for this instead of simple rounded divs? 我是否需要使用canvas元素而不是简单的舍入div?

Edit: Looks like this is specific to WebKit browsers as it works fine in Firefox. 编辑:看起来这是特定于WebKit浏览器,因为它在Firefox中工作正常。

Edit 2: I'm accepting Niels's solution as he's correct, the box model dictates that elements are rectangles, so the way I was going about this was wrong. 编辑2:我接受Niels的解决方案,因为他是正确的,盒子模型规定元素是矩形,所以我这样做的方式是错误的。 However I was able to accomplish what I needed with SVGs. 但是我能够用SVG完成我需要的东西。 Here's an example Fiddle in case anyone is interested: http://jsfiddle.net/uhrHX/1/ 如果有人感兴趣,这里有一个小提琴的例子: http//jsfiddle.net/uhrHX/1/

The CSS standards do not define this behaviour in level 2 nor 3. All they define is: CSS标准没有在级别2和3中定义此行为。它们定义的所有内容是:

The :hover pseudo-class applies while the user designates an element with a pointing device, but does not necessarily activate it. :hover伪类适用于用户使用指针设备指定元素,但不一定要激活它。 For example, a visual user agent could apply this pseudo-class when the cursor (mouse pointer) hovers over a box generated by the element. 例如,当光标(鼠标指针)悬停在元素生成的框上时,可视用户代理可以应用此伪类。

The CSS box model implicitly states that all block elements form rectangular boxes. CSS框模型隐含地声明所有块元素形成矩形框。 Just as text will not float in a circular way around your divs, for all intents and purposes your circles are still rectangular in layout, and to Webkit in behaviour. 就像文本不会以圆形方式在你的div周围浮动一样,出于所有意图和目的,你的圆圈在布局上仍然是矩形的,并且在行为上仍然是Webkit。 Gecko developers apparently have gone the extra mile to respect border-radius for hovers, but that is actually inconsistent since they don't do it for 'gaps' in backgrounds and the like, which are essentially also just visual changes just like border-radius. Gecko开发人员显然已经花了更多的努力来尊重悬停的边界半径,但这实际上是不一致的,因为他们没有为背景等中的“间隙”做这些,这实际上也只是像border-radius一样的视觉变化。

In short, don't expect browser behaviour to change on this, since CSS standards don't define the behaviour. 简而言之,不要指望浏览器行为会因此而改变,因为CSS标准没有定义行为。 The only way to properly implement it cross-all-browser is with Javascript and some smart Pythagoras calculations on mouse positions. 正确实现跨浏览器的唯一方法是使用Javascript和一些智能Pythagoras计算鼠标位置。

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

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