3

我已经动态地创建了 SVG 圆圈,并使用 JQuery 将其从小圆圈变为大圆圈。动画在其他 JQuery 版本中运行良好,并且仅在 JQuery 3.0 版中引发异常“设置只有一个 getter 的属性”。我在网上搜索过。这会由于属性没有setter功能而导致。

_animateCircle: function (element, delayInterval) {
            var radius = element.getAttribute("r");
            var scaleVal;
            var $ele = $(element);
            var layer = this;
            $ele.delay(delayInterval).each(function () { }).animate(
                {   
                    r: radius // if i comment this line, exception not occur. But animation not working
                },
                {
                    duration: 700,

                    step: function (now) {
                        scaleVal = now;
                    }
                }
            );
        }

我的问题是为什么这仅在 JQuery 3.0 版中不起作用。请就此给我建议。

谢谢,巴拉蒂。

4

1 回答 1

2

编辑,更新

Firefox 的解决方法,其中 jQuery 在最后else一行Tween.propHooks._default.set记录错误6571

else {
      tween.elem[ tween.prop ] = tween.now; // logs error
}

您可以创建一个具有 value 属性等于rvalue 的对象,这是一个SVGAnimatedLength对象,以及具有动画应该停止的 value 的属性;atstep函数.animate()调用创建的对象作为参数来jQuery()设置属性 using .attr("r", now),这似乎在 firefox、chromium 上返回相同的结果

var fx = {
  _animateCircle: function(element, delayInterval) {
    var r = element.attr("r");
    var radius = {from:r, to:r * 2}; // set `r` to `radius.to` value
    $(radius).delay(delayInterval).animate({
      from: radius.to
    }, {
      duration: 700,
      step: function(now) {
        element.attr("r", now);
      }
    });
  }
}

fx._animateCircle($("circle"), 500)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <circle cx="200" cy="100" r="50" stroke-width="1" fill="blue" />
</svg>

jsfiddle https://jsfiddle.net/bxmgqnq3/3/


代替$.fn.attr()_.getAttribute()

var fx = {
  _animateCircle: function(element, delayInterval) {
    var radius = element.attr("r") * 2;
    console.log(radius);
    var scaleVal;
    element.delay(delayInterval).animate({
      r: radius
    }, {
      duration: 700,
      step: function(now) {
        scaleVal = now;
      }
    });
  }
}

fx._animateCircle($("circle"), 500)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <circle cx="200" cy="100" r="50" stroke-width="1" fill="blue" />
</svg>

于 2016-07-18T07:03:40.243 回答