问题
I have created SVG circle dynamically and animated it small circle to large using JQuery. Animation was working fine in other JQuery version and throws exception "setting a property that has only a getter" in JQuery version 3.0 only. I have searched in online. It will caused due to the attribute don't have setter function.
_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;
}
}
);
}
My question is why this not working only in JQuery version 3.0 only. Please advise me on this.
Thanks, Bharathi.
回答1:
Edit, Updated
Workaround for firefox where jQuery logs error at last else
at Tween.propHooks._default.set
line 6571
else {
tween.elem[ tween.prop ] = tween.now; // logs error
}
You can create an object having property of value equal to r
value, which is an SVGAnimatedLength object, and property having value where animation should stop; at step
function of .animate()
called on the created object as parameter to jQuery()
set property using .attr("r", now)
, which appears to return same result at 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/
Substitute $.fn.attr()
for .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>
来源:https://stackoverflow.com/questions/38430148/jquery3-0-throws-error-setting-a-property-that-has-only-a-getter-firefox