简体   繁体   English

CSS3在动画元素上旋转,导致点击事件无法调用

[英]CSS3 Rotate on animated element causing click event not to invoke

Okay this one is causing me a lot of problems. 好的,这给我带来了很多问题。

When using the css3 -webkit-transform style with any kind of 3d rotation (such as rotateY(30deg) ), it is extremely unreliable to bind a click event to this rotated object. 当将css3 -webkit-transform样式用于任何类型的3d旋转(例如rotateY(30deg) )时,将click事件绑定到此旋转对象是极其不可靠的。

See the example code below or look at this fiddle (to see the unreliability in fiddle, click on the right side of the element). 请参阅下面的示例代码或查看此小提琴 (要查看小提琴中的不可靠性,请单击元素的右侧)。 This example does not have animation but still is affected. 此示例没有动画,但仍然会受到影响。

html: 的HTML:

<div id='box1'>Click this box.</div>

css: CSS:

#box1{
    -webkit-transform: rotateY(30deg);
}

jquery: jQuery的:

$('#box1').click(function(){
    alert('clicked box 1');
});

What I mean by 'unreliable' is that the click event is not always thrown (depending on where you click it), and sometimes the event is not thrown at all (no matter where you click it). 我所说的“不可靠”是指单击事件并不总是抛出(取决于您单击它的位置),有时根本不抛出该事件(无论您在何处单击)。

Is there a way to fix or avoid this so it works with CSS3 elements while rotated and animated? 有没有办法解决或避免这种情况,以便在旋转和动画处理时与CSS3元素一起使用?

Update: 更新:

Using the CSS property 'float:left' on the element seems to allow it to detect click events properly *while not in motion . 在元素上使用CSS属性'float:left'似乎允许它*不运动时正确检测点击事件。 Does anyone know why the float property fixes this? 有谁知道为什么float属性可以解决此问题?

My ultimate goal is to have the object receive click events while in motion, however, when I apply a new CSS3 rotate3d property (live, upon another jquery event) to give the object animation, the click event starts failing again. 我的最终目标是让对象在运动时接收点击事件,但是,当我应用新的CSS3 rotation3d属性(在另一个jquery事件上生效)以使对象具有动画效果时,点击事件再次开始失败。

I cannot seem to replicate the problem, it seems to be working correctly after adding a float:left; 我似乎无法复制该问题,添加float:left;之后似乎可以正常工作float:left; .

Sometimes browsers act funny while rendering the actual width of an HTML element. 有时,浏览器在渲染HTML元素的实际宽度时会表现得很滑稽。 float:left seems to make the browsers calculate better. float:left似乎使浏览器的计算效果更好。

This is the JavaScript : 这是JavaScript:

$(document).ready(function (){
  $('#box1').bind({
    'click' : function () {
      alert('clicked box 1');
    },
    'hover' : function () {
      console.log('Over Box 1');
    }
  });
  $('#box2').click(function(){
    alert('clicked box 2');
  });

  window.angle = 0;
  setInterval(function () {
    if (window.angle >= 360) {
      window.angle = 0;
    } else {
      window.angle += 5;
    }
    $('#box1').css('-webkit-transform', 'rotateY(' + window.angle + 'deg)');
    }, 100);
});

Here is an updated fiddle with the animation, it seems to work fine : http://jsfiddle.net/RyvtZ/9/ 这是动画的更新小提琴,似乎可以正常使用: http : //jsfiddle.net/RyvtZ/9/

(I added console.log on hover to make life simpler ;) ) (我在hover添加了console.log ,使生活更简单;))

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

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