javascript - Snap SVG .animate 回调立即触发

标签 javascript jquery animation svg snap.svg

我正在尝试将一组参数传递给 Snap SVG 中的动画函数,包括回调函数。但是,回调(在本例中是删除元素)会在单击时立即触发,而不是在动画完成后触发。就像现在一样,该元素被删除,然后 animate 函数出错,因为该元素不再存在。有什么想法吗?

    var theCallback = theApp.destroyAnElement("#"+ theParentID); 

//The function call
theAnimationFunctions.animateSingleSVGElementWithSnap('.stopped','#svg-container',{transform: 's1,0'}, 500, mina.backin, 0,0,theCallback);

// The animate function
    animateSingleSVGElementWithSnap: function(element, parentSVG, animationValues, duration, easing, initialDelay, callback) {
       var s = Snap.select("#"+ parentSVG),
           theElement = s.select(element);

       setTimeout(function() {
         theElement.stop().animate(animationValues, duration, easing, function() {
             callback;
         });
      }, initialDelay);
    }

更新

theCallback = function () { theApp.destroyAnElement("#"+ theElementID); };

theAnimationFunctions.animateSingleSVGElementWithSnap('.stopped','#svg-container',{transform: 's1,0'}, 500, mina.backin, 0,0,theCallback);

theAnimationFunctions = {
    animateSingleSVGElementWithSnap: function(element, parentSVG, animationValues, duration, easing, initialDelay, callback) {
    var s = Snap.select("#"+ parentSVG),
    theElement = s.select(element);

    setTimeout(function() {
       theElement.stop().animate(animationValues, duration, easing, function() {
            callback();
       });
    }, initialDelay);
}

通过上述更新,我现在在动画完成时收到错误消息:

"Uncaught TypeError: callback is not a function"

最佳答案

您必须将回调包装在匿名函数中以防止它立即触发:

var theCallback = function () { theApp.destroyAnElement("#"+ theParentID); };

然后将其作为函数调用:

theElement.stop().animate(animationValues, duration, easing, function() {
         theCallback(); // <-- notice the parenthesis
     });

关于javascript - Snap SVG .animate 回调立即触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30982667/

相关文章:

javascript - 阐明 Javascript 回调流程

javascript - 使对象的所有属性可变

javascript - React Form onSubmit 方法未触发

jquery - 使用AJAX加载方法加载jsp页面

javascript - 窗口加载选择div并淡出

matlab - 沿曲线移动点(3D 动画图)

javascript - 如果 IF 语句为假,如何让 if/else 语句运行一堆函数?

javascript - 如何在参数中返回 next() 和 prev() ?

css - 使用 css 对进入中心图形然后环绕中心图形进行动画处理

css - 我可以使用 CSS 使边框在单击后出现然后消失吗?