js中动画事件:requestAnimationFrame、transitionend、animation...
前端  /  管理员 发布于 1个月前   64
js中有以下几种与动画相关的事件介绍
requestAnimationFrame事件:
在浏览器重绘之前触发,通常用于制作高性能动画。
下面是一个使用 requestAnimationFrame 来制作简单动画的例子
// 获取要动画的元素
let element = document.getElementById("my-element");
// 初始化动画变量
let x = 0;
let y = 0;
let stepX = 2;
let stepY = 3;
// 定义动画函数
function animate() {
// 更新元素的位置
x += stepX;
y += stepY;
element.style.left = x + "px";
element.style.top = y + "px";
// 使用 requestAnimationFrame 重复调用动画函数
requestAnimationFrame(animate);
}
// 开始动画
animate();
transitionend事件:
当 CSS transition 完成时触发此事件。
animationstart, animationiteration, animationend
animationstart 事件会在 CSS animation 开始播放时触发。
animationiteration 事件会在 CSS animation 每次迭代时触发。
animationend 事件会在 CSS animation 结束时触发。
如果你想在 JavaScript 中监听这些事件,
可以使用 addEventListener 或 attachEvent 方法:
let element = document.getElementById("my-element");
// 监听 animationstart 事件
element.addEventListener("animationstart", function(){
console.log("animation started");
});
// 监听 animationiteration 事件
element.addEventListener("animationiteration", function(){
console.log("animation iteration");
});
// 监听 animationend 事件
element.addEventListener("animationend", function(){
console.log("animation ended");
});
最后
注意在使用这些事件之前,需要确保元素上有一个有效的 CSS animation
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号