【问题标题】:How to get actual position of the object?如何获得物体的实际位置?
【发布时间】:2015-07-07 15:24:27
【问题描述】:

我有以下代码 sn-p,它试图每 100 毫秒获取动画对象的实际位置。

   $scope.getPositionOfTheSpiderOnTheRope = function() {
        $interval(function () {
           var positionOfTheSpider =  $('#spiderTwo').position();
           console.log(positionOfTheSpider.top);
        }, 100);
    }

动画功能如下:

$scope.animateSpiderOne = function () {
        $('#spiderTwo').velocity({
            "margin-top": '+=20%',
        }, 4500, function () {
            console.log("MOVE DOWN END");
            $('#spiderTwo').velocity({
                "margin-top": '-=20%',
            }, 4500, function () {
                console.log("MOVE UP END");
                $scope.animateSpiderOne();
            });
        });
    };

问题是动画正在动画,但返回值仍然相同而没有变化。

请问如何解决?

【问题讨论】:

    标签: jquery css css-animations velocity.js


    【解决方案1】:

    jQuery 的.position() 方法没有考虑margin。如果您为 top 而不是 margin-top 设置动画,您将获得不同的值。

    【讨论】:

    • 您可以为 top 设置动画而不是 margin-top,只需替换 .velocity() 调用中的两个
    【解决方案2】:

    我用这个代码 sn-p 解决了它:

       $scope.getPositionOfTheSpiderOnTheRope = function() {
                   var positionOfTheSpider =  $('#spiderTwo');
                   console.log(positionOfTheSpider[0].style.marginTop);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多