【问题标题】:JQuery3.0 throws error setting a property that has only a getter firefoxJQuery3.0 抛出错误设置一个只有 getter firefox 的属性
【发布时间】:2016-11-20 16:09:23
【问题描述】:

我已经动态创建了 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 版中不起作用。请给我建议。

谢谢, 婆罗洲。

【问题讨论】:

    标签: javascript jquery svg jquery-3


    【解决方案1】:

    编辑、更新

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

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

    您可以创建一个属性值等于r 值的对象,它是一个SVGAnimatedLength 对象,以及具有动画应该停止的值的属性;在.animate()step 函数上调用创建的对象作为jQuery() 的参数使用.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>

    【讨论】:

    • 我已经尝试过你的解决方案,但我仍然在 JQuery3.0 中遇到这个问题
    • 什么问题?您是否将.attr() 替换为.getAttribute()
    • 不。我正在使用 .attr()。问题不在于使用 .getAttribute()。如果我在 jquery animate 方法中使用半径(r:radius),则该问题仅在 Firefox 中重现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2019-02-24
    • 2021-05-31
    • 2021-04-04
    • 2019-06-30
    • 2022-07-27
    相关资源
    最近更新 更多