如何在jQuery动画中获得X和Y位置

时间:2015-05-07 15:20:02

标签: jquery animation

我熟悉使用鼠标位置获取X和Y坐标,但是,我使用CSS转换“translate”“rotate”函数进行动画制作。当您不使用鼠标事件时,是否可以获得动画图像的X和Y位置?图像向下移动并同时旋转我希望看到它的X和Y位置,因为我想在特定坐标处添加fadeout()函数。 X位置在这里为零,但我也将改变它。这是我到目前为止所拥有的

$(document).ready(function(){
    var logoVar = $('#logo');
    var rotateVar =0;

    $({translateVar: 0, rotateVar: 0}).animate( 
    {
        translateVar: 200,
        rotateVar: 360 
    },
    { // animate method options below 
        duration: 5000,  // 5 seconds for the entire animation
        step: function(now, abc){  

            if(abc.prop == 'translateVar')
                translateVar = now;
            else if(abc.prop =='rotateVar')
                rotateVar =now; 

            logoVar.css('transform', 'translate(0px, ' + now + 'px) rotate(' + rotateVar + 'deg)' );        
        }
    });

});

1 个答案:

答案 0 :(得分:0)

你可以使用offset(),这样你就可以了:

logoVar.offset().top

然后对于X,你使用:

logoVar.offset().left

因为每个人都喜欢API文档作为来源:http://api.jquery.com/offset/