18910140161

JavaScript-JS动态创建的SVG在IOS-Stack溢出中不工作/动画

顺晟科技

2022-10-19 13:49:06

144

我有这个代码在windows和android的chrome、edge和FF上工作得很好,但在ipad的FF、edge、chrome和Safari上却坏了。IOS上基本没有此实例的动画或符号。

在另一个函数中,我对click事件有一个关闭状态,其中这个svg元素被移除,并被替换为本质上相同的东西,新的点和to/from相反。我得到的似乎是垃圾的闪光(混淆/转置点)和没有动画。我只有一个iPad可以测试,所以调试至少可以说是很棘手的。我已经将这些属性输出到innerHTML,并且所有属性都显示正常。


顺晟科技:

不要在html中使用onclick。javascript关键字将返回目标元素,因此不需要在函数params中声明它。

var height = document.documentElement.clientHeight;
var width = document.documentElement.clientWidth;

//DYNAMICALLY CREATE SVG POLYGON

const bgSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
const bgPoly = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
const animate = document.createElementNS('http://www.w3.org/2000/svg', 'animate');

bgSvg.setAttribute('class', 'mobiNav_Poly_Close');
bgSvg.setAttribute('width', `${width}`);
bgSvg.setAttribute('height', `${height}`);
bgSvg.setAttribute('fill', 'white');
bgSvg.setAttribute('viewBox', `0 0 ${width} ${height}`);
bgPoly.setAttribute('class', 'Poly_Close');
bgPoly.setAttribute('points', `0,${height} ${width},${height} 0,0 ${width},0`);
animate.setAttribute('attributeName', 'points');
animate.setAttribute('dur', '1s');
animate.setAttribute('keyTimes', '0 ; 1');
animate.setAttribute('keySplines', '.67,.01,.16,1');
animate.setAttribute('calcMode', 'spline');
animate.setAttribute('fill', 'freeze');
animate.setAttribute('from', `${width},${height}, 0,${height}, 0,0 ${width},0`);
animate.setAttribute('to', `${width},${height}, ${width},0, 0,0 ${width},0`);

bgSvg.appendChild(bgPoly);
bgPoly.appendChild(animate);
document.querySelector('.mobiNav_overlay').appendChild(bgSvg);

对于这两个元素,请尝试

var height = document.documentElement.clientHeight;
var width = document.documentElement.clientWidth;

//DYNAMICALLY CREATE SVG POLYGON

const bgSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
const bgPoly = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
const animate = document.createElementNS('http://www.w3.org/2000/svg', 'animate');

bgSvg.setAttribute('class', 'mobiNav_Poly_Close');
bgSvg.setAttribute('width', `${width}`);
bgSvg.setAttribute('height', `${height}`);
bgSvg.setAttribute('fill', 'white');
bgSvg.setAttribute('viewBox', `0 0 ${width} ${height}`);
bgPoly.setAttribute('class', 'Poly_Close');
bgPoly.setAttribute('points', `0,${height} ${width},${height} 0,0 ${width},0`);
animate.setAttribute('attributeName', 'points');
animate.setAttribute('dur', '1s');
animate.setAttribute('keyTimes', '0 ; 1');
animate.setAttribute('keySplines', '.67,.01,.16,1');
animate.setAttribute('calcMode', 'spline');
animate.setAttribute('fill', 'freeze');
animate.setAttribute('from', `${width},${height}, 0,${height}, 0,0 ${width},0`);
animate.setAttribute('to', `${width},${height}, ${width},0, 0,0 ${width},0`);

bgSvg.appendChild(bgPoly);
bgPoly.appendChild(animate);
document.querySelector('.mobiNav_overlay').appendChild(bgSvg);
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航