CSS3 Rotate on animated element causing click event not to invoke

人走茶凉 提交于 2019-12-06 23:41:36

问题


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.

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:

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

css:

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

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?

Update:

Using the CSS property 'float:left' on the element seems to allow it to detect click events properly *while not in motion. Does anyone know why the float property fixes this?

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.


回答1:


I cannot seem to replicate the problem, it seems to be working correctly after adding a float:left;.

Sometimes browsers act funny while rendering the actual width of an HTML element. float:left seems to make the browsers calculate better.

This is the 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/

(I added console.log on hover to make life simpler ;) )



来源:https://stackoverflow.com/questions/13833227/css3-rotate-on-animated-element-causing-click-event-not-to-invoke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!