简体   繁体   English

使用jQuery的CSS3效果仅适用于chrome调试

[英]CSS3 effects with jQuery only works on chrome debug

I am trying to animate a div object using jQuery and CSS3. 我试图使用jQuery和CSS3动画div对象。 But i am obtaining a weird behavior. 但我得到一个奇怪的行为。 Effect works on only chrome debug mode. 效果仅适用于chrome调试模式。 Here is the code 这是代码

var $s = $('#animateMePlease');
$s.css('transition-duration', 0+'ms');
$s.css({
opacity:0,
display:'block'
});
// debugger; // only works when i stop once code execution here, and release again
$s.css('transition-duration', d.duration+'ms');
$s.css({
opacity:1
});

Thanks for any help. 谢谢你的帮助。

Since you're already using jQuery why not just use: 由于您已经在使用jQuery,为什么不使用:

$('#animateMePlease').hide().fadeIn(d.duration);

and let jQuery handle the details in a browser-comaptible way - compare the jQuery browser support with the support for CSS3 transitions . 让jQuery以浏览器兼容的方式处理细节 - 比较jQuery浏览器支持CSS3转换支持

(Make sure that you actually have some number in d.duration ) (确保你在d.duration确实有一些数字)

Update 更新

After reading your comment I see why you don't want to use the standard jQuery way. 阅读完评论后,我明白了为什么你不想使用标准的jQuery方式。 Also if you are already using those transforms then you already depend on CSS3 support so using transition-duration is not an issue. 此外,如果您已经在使用这些转换,那么您已经依赖CSS3支持,因此使用transition-duration不是问题。

In that case you can change this: 在这种情况下,你可以改变这个:

$s.css({
  opacity: 1
});

to this: 对此:

setTimeout(function () {
  $s.css({
    opacity: 1
  });
}, 10);

so your code becomes: 所以你的代码变成:

var $s = $('#animateMePlease');
$s.css('transition-duration', 0 + 'ms');
$s.css({
  opacity: 0,
  display: 'block'
});
$s.css('transition-duration', d.duration + 'ms');
setTimeout(function () {
  $s.css({
    opacity: 1
  });
}, 10);

Adding a small timeout will make sure that the state that the DOM is in so far gets rendered before the opacity gets changed to 1. 添加一个小超时将确保在不透明度变为1之前渲染DOM到目前为止的状态。

See DEMO . DEMO

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

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