【发布时间】:2019-08-08 10:46:14
【问题描述】:
所以,我一直在尝试使用来自anime.js 的惊人动画,which is this one。
但是当我在复制代码后使用它时,这个动画不起作用,但它在 codepen 上起作用。我已经完全按照原样获取了代码。
以下是我添加anime.js和其他js文件的方法:
<script type="text/javascript" src="https://codepen.io/juliangarnier/pen/75efae7724dbc3c055e918057fc4aca5"></script>
<script src="anime-master/lib/anime.min.js"></script>
<script src="js/sphere.js"></script>
sphere.js 是包含来自 codepen 的 js 代码的文件。其他一切都完全相同。有什么我做错了还是我在这里失踪了。
非常感谢任何帮助。
查看动画的以下js代码后,错误状态:
UncaughtReferenceError 动画未定义 在
anime.set()以下代码的一部分。
function fitElementToParent(el, padding) {
var timeout = null;
function resize() {
if (timeout) clearTimeout(timeout);
anime.set(el, {scale: 1});
var pad = padding || 0;
var parentEl = el.parentNode;
var elOffsetWidth = el.offsetWidth - pad;
var parentOffsetWidth = parentEl.offsetWidth;
var ratio = parentOffsetWidth / elOffsetWidth;
timeout = setTimeout(anime.set(el, {scale: ratio}), 10);
}
resize();
window.addEventListener('resize', resize);
}
var sphereAnimation = (function() {
var sphereEl = document.querySelector('.sphere-animation');
var spherePathEls = sphereEl.querySelectorAll('.sphere path');
var pathLength = spherePathEls.length;
var hasStarted = false;
var aimations = [];
fitElementToParent(sphereEl);
var breathAnimation = anime({
begin: function() {
for (var i = 0; i < pathLength; i++) {
aimations.push(anime({
targets: spherePathEls[i],
stroke: {value: ['rgba(255,75,75,1)', 'rgba(80,80,80,.35)'], duration: 500},
translateX: [2, -4],
translateY: [2, -4],
easing: 'easeOutQuad',
autoplay: false
}));
}
},
update: function(ins) {
aimations.forEach(function(animation, i) {
var percent = (1 - Math.sin((i * .35) + (.0022 * ins.currentTime))) / 2;
animation.seek(animation.duration * percent);
});
},
duration: Infinity,
autoplay: false
});
var introAnimation = anime.timeline({
autoplay: false
})
.add({
targets: spherePathEls,
strokeDashoffset: {
value: [anime.setDashoffset, 0],
duration: 3900,
easing: 'easeInOutCirc',
delay: anime.stagger(190, {direction: 'reverse'})
},
duration: 2000,
delay: anime.stagger(60, {direction: 'reverse'}),
easing: 'linear'
}, 0);
var shadowAnimation = anime({
targets: '#sphereGradient',
x1: '25%',
x2: '25%',
y1: '0%',
y2: '75%',
duration: 30000,
easing: 'easeOutQuint',
autoplay: false
}, 0);
function init() {
introAnimation.play();
breathAnimation.play();
shadowAnimation.play();
}
init();
})();
【问题讨论】:
-
检查控制台。很可能是由于配置不同而导致的错误
-
@RoryMcCrossan 我也添加了控制台错误,抱歉编辑延迟
-
为什么要在脚本标签中编写 url?
-
@kiranvj 删除或添加不会改变任何东西,但我在一个 SO 答案中读到它是必需的,因为可能有这个文件包含我们版本中缺少的代码
标签: javascript jquery html animation anime.js