Home >Web Front-end >CSS Tutorial >Why Does My JavaScript `animate` Function Work in IE but Fail in Chrome?
Problem:
An animate function, invoked via an event handler content attribute, fails to execute in Chrome, while it functions normally in Internet Explorer.
Explanation:
In Chrome, the global animate function is shadowed by the recently introduced Element.prototype.animate in Web Animations. This shadowing arises due to the lexical environment scope of event handlers, which prioritizes the scope of the target element over the global scope.
Solution:
To resolve the issue, you can either:
function animate__() { var div = document.getElementById('demo'); div.style.left = "200px"; div.style.color = "red"; }
document.getElementById('demo').addEventListener('click', function() { animate().bind(window); });
The above is the detailed content of Why Does My JavaScript `animate` Function Work in IE but Fail in Chrome?. For more information, please follow other related articles on the PHP Chinese website!