简体   繁体   English

jQuery:动画元素位置和不透明度在一起

[英]jQuery: animate element position and opacity toghether

I have div element with CSS style: 我有CSS样式的div元素:

.somediv {
    display: block;
    width: 100px;
    height: 100px;
    background: #4679BD;
    position: absolute;
    top: 10px;
    left: 100px;
    opacity: 0;
}

and I want to change div's position-top with jQuery animation. 我想用jQuery动画更改div的位置。 Also within this animation process I need to change opacity. 同样在此动画过程中,我需要更改不透明度。

I tried to do it with this jQuery script: 我试图用这个jQuery脚本来做到这一点:

$('.somediv').animate({
  'top':'150px',
  'opacity':'1'
}, 1000);

It works as I need in Firefox, but in Chrome position animation doesn't work. 它可以在Firefox中按我的需要工作,但在Chrome位置动画无法正常工作。 Div changes position immediately after animation complete. 动画制作完成后,Div立即更改位置。

PS: If I change to position:relative then it works good in both browsers, but I need to do it with position:absolute; PS:如果我更改为position:relative则在两种浏览器中都可以正常工作,但是我需要使用position:absolute;来完成position:absolute;

How to fix this? 如何解决这个问题?

Here is the fiddle. 这是小提琴。 Try in both browsers and you'll see the difference: http://jsfiddle.net/eJXLf/ 在两种浏览器中尝试,您将看到不同之处: http : //jsfiddle.net/eJXLf/

.

-----UPDATE----- ----- -----更新

This is how it looks in Chrome and Firefox: 这是在Chrome和Firefox中的外观:

Chrome: ............................................... Firefox: Chrome: ............................................... Firefox :

在此处输入图片说明 ...................................... ...................................... 在此处输入图片说明

This is hard for me to test because I can't replicate it in my version of chrome, but you could try using marginTop instead? 这很难测试,因为我无法在chrome版本中复制它,但是您可以尝试使用marginTop代替吗?

$('.somediv').animate({
  marginTop: '150px',
  opacity: 1
}, 1000);

Looks like this is Chrome bug. 看来这是Chrome错误。

I used Chrome 33.0.1750.46 beta-m but when I tested in Version 34.0.1812.0 it works ok. 我使用的是Chrome 33.0.1750.46 beta-m,但是当我在34.0.1812.0版中进行测试时,它可以正常运行。

Thanks for replies. 感谢您的答复。

Works fine in Chrome 32.0.1700.76 for me. 对我来说,在Chrome 32.0.1700.76中可以正常工作。 Both position and opacity animate smoothly. 位置和不透明度都可以平滑地动画。

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

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